Class: Onebox::Engine::AllowlistedGenericOnebox
Instance Attribute Summary
#errors, #options, #timeout, #uri, #url
Class Method Summary
collapse
Instance Method Summary
collapse
#layout, max_text
add_oembed_provider, add_opengraph_provider, #always_https?, oembed_providers, opengraph_providers, #raw
all_iframe_origins, engines, included, #initialize, origins_to_regexes
Class Method Details
.===(other) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 57
def self.===(other)
if other.is_a?(URI)
(
begin
IPAddr.new(other.hostname)
rescue StandardError
nil
end
).nil?
else
true
end
end
|
53
54
55
|
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 53
def self.
["brand", "price", "usd", "cad", "reading time", "likes"]
end
|
.article_html_hosts ⇒ Object
45
46
47
|
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 45
def self.article_html_hosts
%w[imdb.com]
end
|
.default_html_providers ⇒ Object
Often using the ‘html` attribute is not what we want, like for some blogs that include the entire page HTML. However for some providers like Flickr it allows us to return gifv and galleries.
20
21
22
|
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 20
def self.default_html_providers
%w[Flickr Meetup]
end
|
.host_matches(uri, list) ⇒ Object
49
50
51
|
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 49
def self.host_matches(uri, list)
!!list.find { |h| /(^|\.)#{Regexp.escape(h)}$/.match(uri.host) }
end
|
.html_providers ⇒ Object
24
25
26
|
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 24
def self.html_providers
@html_providers ||= default_html_providers.dup
end
|
.html_providers=(new_provs) ⇒ Object
28
29
30
|
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 28
def self.html_providers=(new_provs)
@html_providers = new_provs
end
|
.https_hosts ⇒ Object
41
42
43
|
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 41
def self.https_hosts
%w[slideshare.net dailymotion.com livestream.com imgur.com flickr.com]
end
|
.priority ⇒ Object
13
14
15
|
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 13
def self.priority
200
end
|
.rewrites ⇒ Object
A re-written URL converts http:// -> https://
33
34
35
|
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 33
def self.rewrites
@rewrites ||= https_hosts.dup
end
|
.rewrites=(new_list) ⇒ Object
37
38
39
|
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 37
def self.rewrites=(new_list)
@rewrites = new_list
end
|
Instance Method Details
#data ⇒ Object
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 91
def data
@data ||=
begin
html_entities = HTMLEntities.new
d = { link: link }.merge(raw)
if d[:title].present?
d[:title] = html_entities.decode(Onebox::Helpers.truncate(d[:title], 80))
end
d[:description] ||= d[:summary]
if d[:description].present?
d[:description] = html_entities.decode(Onebox::Helpers.truncate(d[:description], 250))
end
if d[:site_name].present?
d[:domain] = html_entities.decode(Onebox::Helpers.truncate(d[:site_name], 80))
elsif d[:domain].present?
d[:domain] = "http://#{d[:domain]}" unless d[:domain] =~ %r{^https?://}
d[:domain] = begin
URI(d[:domain]).host.to_s.sub(/^www\./, "")
rescue StandardError
nil
end
end
d[:image] = d[:image_secure_url] || d[:image_url] || d[:thumbnail_url] || d[:image]
d[:image] = Onebox::Helpers.get_absolute_image_url(d[:image], @url)
d[:image] = Onebox::Helpers.normalize_url_for_output(html_entities.decode(d[:image]))
d[:image] = nil if d[:image].blank?
d[:video] = d[:video_secure_url] || d[:video_url] || d[:video]
d[:video] = nil if d[:video].blank?
d[:published_time] = d[:article_published_time] if d[:article_published_time].present?
if d[:published_time].present?
d[:article_published_time] = Time.parse(d[:published_time]).strftime("%-d %b %y")
d[:article_published_time_title] = Time.parse(d[:published_time]).strftime(
"%I:%M%p - %d %B %Y",
)
end
if d[:label1].present? && d[:data1].present? &&
!!AllowlistedGenericOnebox..find { |l|
d[:label1] =~ /#{l}/i
}
d[:label_1] = Onebox::Helpers.truncate(d[:label1])
d[:data_1] = Onebox::Helpers.truncate(d[:data1])
end
if d[:label2].present? && d[:data2].present? &&
!!AllowlistedGenericOnebox..find { |l|
d[:label2] =~ /#{l}/i
}
if d[:label_1].blank?
d[:label_1] = Onebox::Helpers.truncate(d[:label2])
d[:data_1] = Onebox::Helpers.truncate(d[:data2])
else
d[:label_2] = Onebox::Helpers.truncate(d[:label2])
d[:data_2] = Onebox::Helpers.truncate(d[:data2])
end
end
if d[:label_1].blank? && d[:price_amount].present? && d[:price_currency].present?
d[:label_1] = "Price"
d[:data_1] = Onebox::Helpers.truncate(
"#{d[:price_currency].strip} #{d[:price_amount].strip}",
)
end
skip_missing_tags = [:video]
d.each do |k, v|
next if skip_missing_tags.include?(k)
if v == nil || v == ""
errors[k] ||= []
errors[k] << "is blank"
end
end
d
end
end
|
#placeholder_html ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 75
def placeholder_html
return article_html if (is_article? || force_article_html?)
return image_html if is_image?
if !SiteSetting.enable_diffhtml_preview? && (is_video? || is_card?)
return Onebox::Helpers.video_placeholder_html
end
if !SiteSetting.enable_diffhtml_preview? && is_embedded?
return Onebox::Helpers.generic_placeholder_html
end
to_html
end
|
#to_html ⇒ Object
71
72
73
|
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 71
def to_html
rewrite_https(generic_html)
end
|
#verified_data ⇒ Object
87
88
89
|
# File 'lib/onebox/engine/allowlisted_generic_onebox.rb', line 87
def verified_data
data
end
|