Class: Onebox::Engine::WhitelistedGenericOnebox

Inherits:
Object
  • Object
show all
Includes:
Onebox::Engine, StandardEmbed, LayoutSupport
Defined in:
lib/onebox/engine/whitelisted_generic_onebox.rb

Constant Summary

Constants included from Onebox::Engine

DEFAULT

Instance Attribute Summary

Attributes included from Onebox::Engine

#cache, #timeout, #uri, #url

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LayoutSupport

#layout, max_text

Methods included from StandardEmbed

add_oembed_provider, add_opengraph_provider, #always_https?, oembed_providers, opengraph_providers, #raw

Methods included from Onebox::Engine

engines, included, #initialize, #options, #options=

Class Method Details

.===(other) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 176

def self.===(other)
  if other.kind_of?(URI)
    return WhitelistedGenericOnebox.host_matches(other, WhitelistedGenericOnebox.whitelist) ||
           WhitelistedGenericOnebox.probable_wordpress(other) ||
           WhitelistedGenericOnebox.probable_discourse(other)
  else
    super
  end
end

.default_html_providersObject

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.



139
140
141
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 139

def self.default_html_providers
  ['Flickr', 'Meetup']
end

.default_whitelistObject



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
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 18

def self.default_whitelist
  %w(23hq.com
    500px.com
    8tracks.com
    abc.net.au
    about.com
    answers.com
    arstechnica.com
    ask.com
    battle.net
    bbc.co.uk
    bbs.boingboing.net
    bestbuy.ca
    bestbuy.com
    blip.tv
    bloomberg.com
    businessinsider.com
    clikthrough.com
    cnet.com
    cnn.com
    collegehumor.com
    consider.it
    coursera.org
    codepen.io
    cracked.com
    dailymail.co.uk
    dailymotion.com
    deadline.com
    dell.com
    deviantart.com
    digg.com
    dotsub.com
    ebay.ca
    ebay.co.uk
    ebay.com
    ehow.com
    espn.go.com
    etsy.com
    findery.com
    flickr.com
    folksy.com
    forbes.com
    foxnews.com
    funnyordie.com
    gfycat.com
    groupon.com
    howtogeek.com
    huffingtonpost.com
    huffingtonpost.ca
    hulu.com
    ign.com
    ikea.com
    imdb.com
    indiatimes.com
    instagr.am
    instagram.com
    itunes.apple.com
    khanacademy.org
    kickstarter.com
    kinomap.com
    liveleak.com
    livestream.com
    lessonplanet.com
    mashable.com
    medium.com
    meetup.com
    mixcloud.com
    mlb.com
    myshopify.com
    myspace.com
    nba.com
    nytimes.com
    npr.org
    photobucket.com
    pinterest.com
    reference.com
    revision3.com
    rottentomatoes.com
    samsung.com
    screenr.com
    scribd.com
    slideshare.net
    sourceforge.net
    speakerdeck.com
    spotify.com
    squidoo.com
    techcrunch.com
    ted.com
    thefreedictionary.com
    theglobeandmail.com
    thenextweb.com
    theonion.com
    thestar.com
    thesun.co.uk
    thinkgeek.com
    tmz.com
    torontosun.com
    tumblr.com
    twitch.tv
    twitpic.com
    usatoday.com
    viddler.com
    videojug.com
    vimeo.com
    vine.co
    walmart.com
    washingtonpost.com
    wikia.com
    wikihow.com
    wired.com
    wistia.com
    wi.st
    wonderhowto.com
    wsj.com
    zappos.com
    zillow.com)
end

.host_matches(uri, list) ⇒ Object



164
165
166
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 164

def self.host_matches(uri, list)
  !!list.find {|h| %r((^|\.)#{Regexp.escape(h)}$).match(uri.host) }
end

.html_providersObject



143
144
145
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 143

def self.html_providers
  @html_providers ||= default_html_providers.dup
end

.html_providers=(new_provs) ⇒ Object



147
148
149
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 147

def self.html_providers=(new_provs)
  @html_providers = new_provs
end

.https_hostsObject



160
161
162
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 160

def self.https_hosts
  %w(slideshare.net dailymotion.com livestream.com)
end

.probable_discourse(uri) ⇒ Object



168
169
170
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 168

def self.probable_discourse(uri)
  !!(uri.path =~ /\/t\/[^\/]+\/\d+(\/\d+)?(\?.*)?$/)
end

.probable_wordpress(uri) ⇒ Object



172
173
174
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 172

def self.probable_wordpress(uri)
  !!(uri.path =~ /\d{4}\/\d{2}\//)
end

.rewritesObject

A re-written URL coverts http:// -> https://



152
153
154
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 152

def self.rewrites
  @rewrites ||= https_hosts.dup
end

.rewrites=(new_list) ⇒ Object



156
157
158
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 156

def self.rewrites=(new_list)
  @rewrites = new_list
end

.whitelistObject



14
15
16
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 14

def self.whitelist
  @whitelist ||= default_whitelist.dup
end

.whitelist=(list) ⇒ Object



10
11
12
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 10

def self.whitelist=(list)
  @whitelist = list
end

Instance Method Details

#article_type?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 191

def article_type?
  data[:type] == "article"
end

#dataObject



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 234

def data
  if raw.is_a?(Hash)
    raw[:link] ||= link
    raw[:title] = HTMLEntities.new.decode(raw[:title])
    return raw
  end

  data_hash = { link: link, title: HTMLEntities.new.decode(raw.title), description: raw.description }
  data_hash[:image] = raw.images.first if raw.images && raw.images.first
  data_hash[:type] = raw.type if raw.type

  if raw. && raw.[:"video:secure_url"] && raw.[:"video:secure_url"].first
    data_hash[:video] = raw.[:"video:secure_url"].first
  elsif raw. && raw.[:video] && raw.[:video].first
    data_hash[:video] = raw.[:video].first
  end

  if raw. && raw.[:"twitter:label1"] && raw.[:"twitter:data1"]
    data_hash[:twitter_label1] = raw.[:"twitter:label1"].first
    data_hash[:twitter_data1] = raw.[:"twitter:data1"].first
  end

  if raw. && raw.[:"twitter:label2"] && raw.[:"twitter:data2"]
    data_hash[:twitter_label2] = raw.[:"twitter:label2"].first
    data_hash[:twitter_data2] = raw.[:"twitter:data2"].first
  end

  data_hash
end

#generic_htmlObject



213
214
215
216
217
218
219
220
221
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 213

def generic_html
  return add_thumbnail_class(data[:html]) if html_type?
  return layout.to_html if article_type?
  return html_for_video(data[:video]) if data[:video]
  return image_html if photo_type?
  return nil if data[:title].nil? || data[:title].empty?

  layout.to_html
end

#html_type?Boolean

Returns:

  • (Boolean)


204
205
206
207
208
209
210
211
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 204

def html_type?
  return data &&
         data[:html] &&
         (
           (data[:html] =~ /iframe/) ||
           WhitelistedGenericOnebox.html_providers.include?(data[:provider_name])
         )
end

#photo_type?Boolean

Generates the HTML for the embedded content

Returns:

  • (Boolean)


187
188
189
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 187

def photo_type?
  ( (data[:type] =~ /photo/ || data[:type] =~ /image/) && data[:type] !~ /photostream/ )
end

#placeholder_htmlObject



227
228
229
230
231
232
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 227

def placeholder_html
  result = nil
  return to_html if article_type?
  result = image_html if (data[:html] && data[:html] =~ /iframe/) || data[:video] || photo_type?
  result || to_html
end

#rewrite_https(html) ⇒ Object



195
196
197
198
199
200
201
202
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 195

def rewrite_https(html)
  return html unless html
  uri = URI(@url)
  if WhitelistedGenericOnebox.host_matches(uri, WhitelistedGenericOnebox.rewrites)
    html.gsub!(/http:\/\//, 'https://')
  end
  html
end

#to_htmlObject



223
224
225
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 223

def to_html
  rewrite_https(generic_html)
end