Class: Onebox::Engine::WhitelistedGenericOnebox

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

Instance Attribute Summary

Attributes included from Onebox::Engine

#cache, #timeout, #url

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LayoutSupport

#layout, max_text

Methods included from StandardEmbed

#raw

Methods included from Onebox::Engine

engines, included, #initialize

Class Method Details

.===(other) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 142

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_rewritesObject



126
127
128
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 126

def self.default_rewrites
  %w(slideshare.net)
end

.default_whitelistObject



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

def self.default_whitelist
  %w(500px.com
    about.com
    answers.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
    coursera.org
    codepen.io
    cracked.com
    dailymail.co.uk
    dailymotion.com
    deadline.com
    dell.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
    groupon.com
    howtogeek.com
    huffingtonpost.com
    huffingtonpost.ca
    hulu.com
    ign.com
    ikea.com
    imgur.com
    indiatimes.com
    instagr.am
    instagram.com
    itunes.apple.com
    khanacademy.org
    kickstarter.com
    kinomap.com
    liveleak.com
    lessonplanet.com
    mashable.com
    mlb.com
    myspace.com
    nba.com
    npr.org
    photobucket.com
    pinterest.com
    reference.com
    revision3.com
    rottentomatoes.com
    samsung.com
    screenr.com
    scribd.com
    slideshare.net
    soundcloud.com
    sourceforge.net
    spotify.com
    squidoo.com
    techcrunch.com
    ted.com
    thefreedictionary.com
    theglobeandmail.com
    theonion.com
    thestar.com
    thesun.co.uk
    thinkgeek.com
    tmz.com
    torontosun.com
    tumblr.com
    twitpic.com
    usatoday.com
    vimeo.com
    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



130
131
132
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 130

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

.probable_discourse(uri) ⇒ Object



134
135
136
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 134

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

.probable_wordpress(uri) ⇒ Object



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

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

.rewritesObject

A re-written URL coverts https:// -> // - it is useful on HTTPS sites that embed youtube for example



118
119
120
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 118

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

.rewrites=(new_list) ⇒ Object



122
123
124
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 122

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

.whitelistObject



12
13
14
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 12

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

.whitelist=(list) ⇒ Object



8
9
10
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 8

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

Instance Method Details

#article_type?Boolean

Returns:

  • (Boolean)


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

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

#dataObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 190

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

  data_hash = { link: link, title: 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] && raw.[:video].first
    data_hash[:video] = raw.[:video].first
  end

  data_hash
end

#generic_htmlObject



170
171
172
173
174
175
176
177
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 170

def generic_html
  return data[:html] if data[:html] && data[:html] =~ /iframe/
  return layout.to_html if article_type?
  return html_for_video(data[:video]) if data[:video]
  return image_html if photo_type?
  return nil unless data[:title]
  layout.to_html
end

#photo_type?Boolean

Generates the HTML for the embedded content

Returns:

  • (Boolean)


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

def photo_type?
  data[:type] =~ /photo/ || data[:type] =~ /image/
end

#placeholder_htmlObject



183
184
185
186
187
188
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 183

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_agnostic(html) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 161

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

#to_htmlObject



179
180
181
# File 'lib/onebox/engine/whitelisted_generic_onebox.rb', line 179

def to_html
  rewrite_agnostic(generic_html)
end