-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.rb
140 lines (117 loc) · 3.31 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# name: hummingbird-onebox
# about: Onebox plugin for hummingbird
# version: 1.0
# authors: Hummingbird Media
register_asset "stylesheets/hummingbird_onebox.scss"
register_asset 'stylesheets/hummingbird_onebox_mobile.scss', :mobile
class Onebox::Engine::HummingbirdOnebox
include Onebox::Engine
include Onebox::Engine::JSON
TYPES = {
'anime' => 'anime',
'a' => 'anime',
'manga' => 'manga',
'm' => 'manga'
}
HOST_REGEX = %r{https?://(?:www\.)?hummingbird\.me}
TYPE_REGEX = %r{(?<type>#{TYPES.keys.join('|')})}
SLUG_REGEX = %r{(?<slug>[A-Za-z0-9\-]+)}
MATCH_REGEX = %r{^#{HOST_REGEX}/#{TYPE_REGEX}/#{SLUG_REGEX}$}
matches_regexp MATCH_REGEX
always_https
def url
# TODO: switch to APIv16
"https://hummingbird.me/full_#{type}/#{slug}.json"
end
def link
super.downcase
end
def to_html
return "<a href=\"#{link}\">#{link}</a>" if media.nil?
<<-HTML
<aside class="onebox onebox-result hb-onebox hb-onebox-#{type}"
data-media-type="#{type}" data-media-slug="#{slug}">
#{poster_html}
<div class="hb-onebox-info">
<h1 class="hb-onebox-header">
<a href="#{link}" target="_blank" class="track-link">
#{media['romaji_title']}
</a>
</h1>
<div class="hb-onebox-rating" title="#{media['bayesian_rating']}">
#{stars_html}
</div>
<div class="hb-onebox-synopsis">
#{media['synopsis']}
#{readmore_html}
</div>
<div class="hb-onebox-genres">
#{genres_html}
</div>
<a class="hb-onebox-library-entry" href="https://hummingbird.me/sign-up" target="_blank">
<span>Track this #{type} with Hummingbird</span>
</a>
</div>
</aside>
HTML
end
private
def type
TYPES[MATCH_REGEX.match(@url)['type']]
end
def slug
MATCH_REGEX.match(@url)['slug'].downcase
end
def media
raw["full_#{type}"]
end
def media_type
case type
when 'anime'; media['show_type']
when 'manga'; media['manga_type']
end
end
def title
media['romaji_title']
end
def poster_image
image = media['poster_image']
(image == '/assets/missing-anime-cover.jpg') ? nil : image
end
def poster_html
poster_image.nil? ? '' : <<-HTML
<div class="hb-onebox-poster">
<img src="#{poster_image}">
</div>
HTML
end
def average_rating
media['bayesian_rating']
end
def stars_html
return '' if average_rating.blank?
rating = (media['bayesian_rating'] / 0.5).round * 0.5
whole_stars = '<i class="fa fa-star"></i>' * rating.floor
half_stars = '<i class="fa fa-star-half-o"></i>' * (rating % 1 / 0.5)
empty_stars = '<i class="fa fa-star-o"></i>' * (5 - rating).floor
"#{whole_stars}#{half_stars}#{empty_stars}"
end
def genres_html
media['genres'].sort.map do |g|
"<div class=\"hb-onebox-genre\">#{g}</div>"
end.join
end
# Bigger than a tweet, smaller than a bread basket
def readmore?
media['synopsis'].length > 350
end
def readmore_html
!readmore? ? '' : <<-HTML
<a class="hb-onebox-readmore">
read
<span class="hb-onebox-readmore-more">more</span>
<span class="hb-onebox-readmore-less">less</span>
</a>
HTML
end
end