onebox 1.8.68 → 1.8.69

Sign up to get free protection for your applications and to get access to all the features.
@@ -53,77 +53,77 @@ module Onebox
53
53
 
54
54
  protected
55
55
 
56
- def html_doc
57
- return @html_doc if @html_doc
56
+ def html_doc
57
+ return @html_doc if @html_doc
58
58
 
59
- headers = nil
60
- headers = { 'Cookie' => options[:cookie] } if options[:cookie]
59
+ headers = nil
60
+ headers = { 'Cookie' => options[:cookie] } if options[:cookie]
61
61
 
62
- @html_doc = Onebox::Helpers.fetch_html_doc(url, headers)
63
- end
62
+ @html_doc = Onebox::Helpers.fetch_html_doc(url, headers)
63
+ end
64
64
 
65
- def get_oembed
66
- oembed_url = nil
65
+ def get_oembed
66
+ oembed_url = nil
67
67
 
68
- StandardEmbed.oembed_providers.each do |regexp, endpoint|
69
- if url =~ regexp
70
- oembed_url = "#{endpoint}?url=#{url}"
71
- break
72
- end
68
+ StandardEmbed.oembed_providers.each do |regexp, endpoint|
69
+ if url =~ regexp
70
+ oembed_url = "#{endpoint}?url=#{url}"
71
+ break
73
72
  end
73
+ end
74
74
 
75
- if html_doc
76
- if Onebox::Helpers.blank?(oembed_url)
77
- application_json = html_doc.at("//link[@type='application/json+oembed']/@href")
78
- oembed_url = application_json.value if application_json
79
- end
75
+ if html_doc
76
+ if Onebox::Helpers.blank?(oembed_url)
77
+ application_json = html_doc.at("//link[@type='application/json+oembed']/@href")
78
+ oembed_url = application_json.value if application_json
79
+ end
80
80
 
81
- if Onebox::Helpers.blank?(oembed_url)
82
- text_json = html_doc.at("//link[@type='text/json+oembed']/@href")
83
- oembed_url ||= text_json.value if text_json
84
- end
81
+ if Onebox::Helpers.blank?(oembed_url)
82
+ text_json = html_doc.at("//link[@type='text/json+oembed']/@href")
83
+ oembed_url ||= text_json.value if text_json
85
84
  end
85
+ end
86
86
 
87
- return {} if Onebox::Helpers.blank?(oembed_url)
87
+ return {} if Onebox::Helpers.blank?(oembed_url)
88
88
 
89
- json_response = Onebox::Helpers.fetch_response(oembed_url) rescue "{}"
90
- oe = Onebox::Helpers.symbolize_keys(::MultiJson.load(json_response))
89
+ json_response = Onebox::Helpers.fetch_response(oembed_url) rescue "{}"
90
+ oe = Onebox::Helpers.symbolize_keys(::MultiJson.load(json_response))
91
91
 
92
- # never use oembed from WordPress 4.4 (it's broken)
93
- oe.delete(:html) if oe[:html] && oe[:html]["wp-embedded-content"]
92
+ # never use oembed from WordPress 4.4 (it's broken)
93
+ oe.delete(:html) if oe[:html] && oe[:html]["wp-embedded-content"]
94
94
 
95
- oe
96
- rescue Errno::ECONNREFUSED, Net::HTTPError, Net::HTTPFatalError, MultiJson::LoadError
97
- {}
98
- end
95
+ oe
96
+ rescue Errno::ECONNREFUSED, Net::HTTPError, Net::HTTPFatalError, MultiJson::LoadError
97
+ {}
98
+ end
99
99
 
100
- def get_opengraph
101
- ::Onebox::Helpers.extract_opengraph(html_doc)
102
- end
100
+ def get_opengraph
101
+ ::Onebox::Helpers.extract_opengraph(html_doc)
102
+ end
103
103
 
104
- def get_twitter
105
- return {} unless html_doc
104
+ def get_twitter
105
+ return {} unless html_doc
106
106
 
107
- twitter = {}
107
+ twitter = {}
108
108
 
109
- html_doc.css('meta').each do |m|
110
- if (m["property"] && m["property"][/^twitter:(.+)$/i]) || (m["name"] && m["name"][/^twitter:(.+)$/i])
111
- value = (m["content"] || m["value"]).to_s
112
- twitter[$1.tr('-:' , '_').to_sym] ||= value unless Onebox::Helpers::blank?(value)
113
- end
109
+ html_doc.css('meta').each do |m|
110
+ if (m["property"] && m["property"][/^twitter:(.+)$/i]) || (m["name"] && m["name"][/^twitter:(.+)$/i])
111
+ value = (m["content"] || m["value"]).to_s
112
+ twitter[$1.tr('-:' , '_').to_sym] ||= value unless Onebox::Helpers::blank?(value)
114
113
  end
115
-
116
- twitter
117
114
  end
118
115
 
119
- def get_favicon
120
- return nil unless html_doc
116
+ twitter
117
+ end
121
118
 
122
- favicon = html_doc.css('link[rel="shortcut icon"], link[rel="icon shortcut"], link[rel="shortcut"], link[rel="icon"]').first
123
- favicon = favicon.nil? ? nil : (favicon['href'].nil? ? nil : favicon['href'].strip)
119
+ def get_favicon
120
+ return nil unless html_doc
124
121
 
125
- Onebox::Helpers::get_absolute_image_url(favicon, url)
126
- end
122
+ favicon = html_doc.css('link[rel="shortcut icon"], link[rel="icon shortcut"], link[rel="shortcut"], link[rel="icon"]').first
123
+ favicon = favicon.nil? ? nil : (favicon['href'].nil? ? nil : favicon['href'].strip)
124
+
125
+ Onebox::Helpers::get_absolute_image_url(favicon, url)
126
+ end
127
127
  end
128
128
  end
129
129
  end
@@ -18,13 +18,13 @@ module Onebox
18
18
  end
19
19
 
20
20
  private
21
- def match
22
- return @match if @match
21
+ def match
22
+ return @match if @match
23
23
 
24
- @match = @url.match(%{trello\.com/(?<type>[^/]+)/(?<key>[^/]+)/?\W*})
24
+ @match = @url.match(%{trello\.com/(?<type>[^/]+)/(?<key>[^/]+)/?\W*})
25
25
 
26
- @match
27
- end
26
+ @match
27
+ end
28
28
  end
29
29
  end
30
30
  end
@@ -254,74 +254,74 @@ module Onebox
254
254
 
255
255
  private
256
256
 
257
- def rewrite_https(html)
258
- return unless html
259
- uri = URI(@url)
260
- html.gsub!("http://", "https://") if WhitelistedGenericOnebox.host_matches(uri, WhitelistedGenericOnebox.rewrites)
261
- html
262
- end
257
+ def rewrite_https(html)
258
+ return unless html
259
+ uri = URI(@url)
260
+ html.gsub!("http://", "https://") if WhitelistedGenericOnebox.host_matches(uri, WhitelistedGenericOnebox.rewrites)
261
+ html
262
+ end
263
263
 
264
- def generic_html
265
- return article_html if is_article?
266
- return video_html if is_video?
267
- return image_html if is_image?
268
- return embedded_html if is_embedded?
269
- return article_html if has_text?
270
- end
264
+ def generic_html
265
+ return article_html if is_article?
266
+ return video_html if is_video?
267
+ return image_html if is_image?
268
+ return embedded_html if is_embedded?
269
+ return article_html if has_text?
270
+ end
271
271
 
272
- def is_article?
273
- (data[:type] =~ /article/ || data[:asset_type] =~ /article/) &&
274
- has_text?
275
- end
272
+ def is_article?
273
+ (data[:type] =~ /article/ || data[:asset_type] =~ /article/) &&
274
+ has_text?
275
+ end
276
276
 
277
- def has_text?
278
- !Onebox::Helpers.blank?(data[:title]) &&
279
- !Onebox::Helpers.blank?(data[:description])
280
- end
277
+ def has_text?
278
+ !Onebox::Helpers.blank?(data[:title]) &&
279
+ !Onebox::Helpers.blank?(data[:description])
280
+ end
281
281
 
282
- def is_image?
283
- data[:type] =~ /photo|image/ &&
284
- data[:type] !~ /photostream/ &&
285
- has_image?
286
- end
282
+ def is_image?
283
+ data[:type] =~ /photo|image/ &&
284
+ data[:type] !~ /photostream/ &&
285
+ has_image?
286
+ end
287
287
 
288
- def has_image?
289
- !Onebox::Helpers.blank?(data[:image])
290
- end
288
+ def has_image?
289
+ !Onebox::Helpers.blank?(data[:image])
290
+ end
291
291
 
292
- def is_video?
293
- data[:type] =~ /^video[\/\.]/ && !Onebox::Helpers.blank?(data[:video])
294
- end
292
+ def is_video?
293
+ data[:type] =~ /^video[\/\.]/ && !Onebox::Helpers.blank?(data[:video])
294
+ end
295
295
 
296
- def is_embedded?
297
- data[:html] &&
298
- data[:height] &&
299
- (
300
- data[:html]["iframe"] ||
301
- WhitelistedGenericOnebox.html_providers.include?(data[:provider_name])
302
- )
303
- end
296
+ def is_embedded?
297
+ data[:html] &&
298
+ data[:height] &&
299
+ (
300
+ data[:html]["iframe"] ||
301
+ WhitelistedGenericOnebox.html_providers.include?(data[:provider_name])
302
+ )
303
+ end
304
304
 
305
- def article_html
306
- layout.to_html
307
- end
305
+ def article_html
306
+ layout.to_html
307
+ end
308
308
 
309
- def image_html
310
- return if Onebox::Helpers.blank?(data[:image])
309
+ def image_html
310
+ return if Onebox::Helpers.blank?(data[:image])
311
311
 
312
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(data[:image])
312
+ escaped_src = ::Onebox::Helpers.normalize_url_for_output(data[:image])
313
313
 
314
- alt = data[:description] || data[:title]
315
- width = data[:image_width] || data[:thumbnail_width] || data[:width]
316
- height = data[:image_height] || data[:thumbnail_height] || data[:height]
314
+ alt = data[:description] || data[:title]
315
+ width = data[:image_width] || data[:thumbnail_width] || data[:width]
316
+ height = data[:image_height] || data[:thumbnail_height] || data[:height]
317
317
 
318
- "<img src='#{escaped_src}' alt='#{alt}' width='#{width}' height='#{height}' class='onebox'>"
319
- end
318
+ "<img src='#{escaped_src}' alt='#{alt}' width='#{width}' height='#{height}' class='onebox'>"
319
+ end
320
320
 
321
- def video_html
322
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(data[:video])
321
+ def video_html
322
+ escaped_src = ::Onebox::Helpers.normalize_url_for_output(data[:video])
323
323
 
324
- <<-HTML
324
+ <<-HTML
325
325
  <video title='#{data[:title]}'
326
326
  width='#{data[:video_width]}'
327
327
  height='#{data[:video_height]}'
@@ -330,20 +330,20 @@ module Onebox
330
330
  <source src='#{escaped_src}'>
331
331
  </video>
332
332
  HTML
333
- end
333
+ end
334
334
 
335
- def embedded_html
336
- fragment = Nokogiri::HTML::fragment(data[:html])
337
- fragment.css("img").each { |img| img["class"] = "thumbnail" }
338
- if iframe = fragment.at_css("iframe")
339
- iframe.remove_attribute("style")
340
- iframe["width"] = data[:width] || "100%"
341
- iframe["height"] = data[:height]
342
- iframe["scrolling"] = "no"
343
- iframe["frameborder"] = "0"
344
- end
345
- fragment.to_html
335
+ def embedded_html
336
+ fragment = Nokogiri::HTML::fragment(data[:html])
337
+ fragment.css("img").each { |img| img["class"] = "thumbnail" }
338
+ if iframe = fragment.at_css("iframe")
339
+ iframe.remove_attribute("style")
340
+ iframe["width"] = data[:width] || "100%"
341
+ iframe["height"] = data[:height]
342
+ iframe["scrolling"] = "no"
343
+ iframe["frameborder"] = "0"
346
344
  end
345
+ fragment.to_html
346
+ end
347
347
  end
348
348
  end
349
349
  end
@@ -22,20 +22,20 @@ module Onebox
22
22
 
23
23
  private
24
24
 
25
- def match
26
- @match ||= @url.match(/^https?:\/\/commons\.wikimedia\.org\/wiki\/(?<name>File:.+)/)
27
- end
28
-
29
- def data
30
- first_page = raw['query']['pages'].first[1]
31
-
32
- {
33
- link: first_page['imageinfo'].first['descriptionurl'],
34
- title: first_page['title'],
35
- image: first_page['imageinfo'].first['url'],
36
- thumbnail: first_page['imageinfo'].first['thumburl']
37
- }
38
- end
25
+ def match
26
+ @match ||= @url.match(/^https?:\/\/commons\.wikimedia\.org\/wiki\/(?<name>File:.+)/)
27
+ end
28
+
29
+ def data
30
+ first_page = raw['query']['pages'].first[1]
31
+
32
+ {
33
+ link: first_page['imageinfo'].first['descriptionurl'],
34
+ title: first_page['title'],
35
+ image: first_page['imageinfo'].first['url'],
36
+ thumbnail: first_page['imageinfo'].first['thumburl']
37
+ }
38
+ end
39
39
 
40
40
  end
41
41
  end
@@ -18,15 +18,15 @@ module Onebox
18
18
  end
19
19
 
20
20
  private
21
- def oembed_data
22
- @oembed_data ||= begin
23
- oembed_url = "https://fast.wistia.com/oembed?embedType=iframe&url=#{url}"
24
- response = Onebox::Helpers.fetch_response(oembed_url) rescue "{}"
25
- Onebox::Helpers.symbolize_keys(::MultiJson.load(response))
26
- rescue
27
- {}
28
- end
21
+ def oembed_data
22
+ @oembed_data ||= begin
23
+ oembed_url = "https://fast.wistia.com/oembed?embedType=iframe&url=#{url}"
24
+ response = Onebox::Helpers.fetch_response(oembed_url) rescue "{}"
25
+ Onebox::Helpers.symbolize_keys(::MultiJson.load(response))
26
+ rescue
27
+ {}
29
28
  end
29
+ end
30
30
  end
31
31
  end
32
32
  end
@@ -13,18 +13,18 @@ module Onebox
13
13
 
14
14
  private
15
15
 
16
- def match
17
- @match ||= @url.match(%{xkcd\.com/(?<comic_id>\\d+)})
18
- end
16
+ def match
17
+ @match ||= @url.match(%{xkcd\.com/(?<comic_id>\\d+)})
18
+ end
19
19
 
20
- def data
21
- {
22
- link: @url,
23
- title: raw['safe_title'],
24
- image: raw['img'],
25
- description: raw['alt']
26
- }
27
- end
20
+ def data
21
+ {
22
+ link: @url,
23
+ title: raw['safe_title'],
24
+ image: raw['img'],
25
+ description: raw['alt']
26
+ }
27
+ end
28
28
  end
29
29
  end
30
30
  end
@@ -58,104 +58,104 @@ module Onebox
58
58
 
59
59
  private
60
60
 
61
- def video_id
62
- @video_id ||= begin
63
- # http://youtu.be/afyK1HSFfgw
64
- if uri.host["youtu.be"]
65
- id = uri.path[/\/([\w\-]+)/, 1]
66
- return id if id
67
- end
68
-
69
- # https://www.youtube.com/embed/vsF0K3Ou1v0
70
- if uri.path["/embed/"]
71
- id = uri.path[/\/embed\/([\w\-]+)/, 1]
72
- return id if id
73
- end
74
-
75
- # https://www.youtube.com/watch?v=Z0UISCEe52Y
76
- params['v']
61
+ def video_id
62
+ @video_id ||= begin
63
+ # http://youtu.be/afyK1HSFfgw
64
+ if uri.host["youtu.be"]
65
+ id = uri.path[/\/([\w\-]+)/, 1]
66
+ return id if id
77
67
  end
78
- end
79
-
80
- def list_id
81
- @list_id ||= params['list']
82
- end
83
68
 
84
- def list_thumbnail_url
85
- @list_thumbnail_url ||= begin
86
- url = "https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/playlist?list=#{list_id}"
87
- response = Onebox::Helpers.fetch_response(url) rescue "{}"
88
- data = Onebox::Helpers.symbolize_keys(::MultiJson.load(response))
89
- data[:thumbnail_url]
90
- rescue
91
- nil
69
+ # https://www.youtube.com/embed/vsF0K3Ou1v0
70
+ if uri.path["/embed/"]
71
+ id = uri.path[/\/embed\/([\w\-]+)/, 1]
72
+ return id if id
92
73
  end
93
- end
94
74
 
95
- def video_oembed_data
96
- url = "https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=#{video_id}"
97
- response = Onebox::Helpers.fetch_response(url) rescue "{}"
98
- Onebox::Helpers.symbolize_keys(::MultiJson.load(response))
75
+ # https://www.youtube.com/watch?v=Z0UISCEe52Y
76
+ params['v']
99
77
  end
78
+ end
100
79
 
101
- def list_oembed_data
80
+ def list_id
81
+ @list_id ||= params['list']
82
+ end
83
+
84
+ def list_thumbnail_url
85
+ @list_thumbnail_url ||= begin
102
86
  url = "https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/playlist?list=#{list_id}"
103
87
  response = Onebox::Helpers.fetch_response(url) rescue "{}"
104
- Onebox::Helpers.symbolize_keys(::MultiJson.load(response))
88
+ data = Onebox::Helpers.symbolize_keys(::MultiJson.load(response))
89
+ data[:thumbnail_url]
90
+ rescue
91
+ nil
105
92
  end
93
+ end
94
+
95
+ def video_oembed_data
96
+ url = "https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=#{video_id}"
97
+ response = Onebox::Helpers.fetch_response(url) rescue "{}"
98
+ Onebox::Helpers.symbolize_keys(::MultiJson.load(response))
99
+ end
106
100
 
107
- def embed_params
108
- p = { 'feature' => 'oembed', 'wmode' => 'opaque' }
101
+ def list_oembed_data
102
+ url = "https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/playlist?list=#{list_id}"
103
+ response = Onebox::Helpers.fetch_response(url) rescue "{}"
104
+ Onebox::Helpers.symbolize_keys(::MultiJson.load(response))
105
+ end
109
106
 
110
- p['list'] = list_id if list_id
107
+ def embed_params
108
+ p = { 'feature' => 'oembed', 'wmode' => 'opaque' }
111
109
 
112
- # Parse timestrings, and assign the result as a start= parameter
113
- start = if params['start']
114
- params['start']
115
- elsif params['t']
116
- params['t']
117
- elsif uri.fragment && uri.fragment.start_with?('t=')
118
- # referencing uri is safe here because any throws were already caught by video_id returning nil
119
- # remove the t= from the start
120
- uri.fragment[2..-1]
121
- end
110
+ p['list'] = list_id if list_id
122
111
 
123
- p['start'] = parse_timestring(start) if start
124
- p['end'] = parse_timestring params['end'] if params['end']
112
+ # Parse timestrings, and assign the result as a start= parameter
113
+ start = if params['start']
114
+ params['start']
115
+ elsif params['t']
116
+ params['t']
117
+ elsif uri.fragment && uri.fragment.start_with?('t=')
118
+ # referencing uri is safe here because any throws were already caught by video_id returning nil
119
+ # remove the t= from the start
120
+ uri.fragment[2..-1]
121
+ end
125
122
 
126
- # Official workaround for looping videos
127
- # https://developers.google.com/youtube/player_parameters#loop
128
- # use params.include? so that you can just add "&loop"
129
- if params.include?('loop')
130
- p['loop'] = 1
131
- p['playlist'] = video_id
132
- end
123
+ p['start'] = parse_timestring(start) if start
124
+ p['end'] = parse_timestring params['end'] if params['end']
133
125
 
134
- URI.encode_www_form(p)
126
+ # Official workaround for looping videos
127
+ # https://developers.google.com/youtube/player_parameters#loop
128
+ # use params.include? so that you can just add "&loop"
129
+ if params.include?('loop')
130
+ p['loop'] = 1
131
+ p['playlist'] = video_id
135
132
  end
136
133
 
137
- def parse_timestring(string)
138
- if string =~ /(\d+h)?(\d+m)?(\d+s?)?/
139
- ($1.to_i * 3600) + ($2.to_i * 60) + $3.to_i
140
- end
134
+ URI.encode_www_form(p)
135
+ end
136
+
137
+ def parse_timestring(string)
138
+ if string =~ /(\d+h)?(\d+m)?(\d+s?)?/
139
+ ($1.to_i * 3600) + ($2.to_i * 60) + $3.to_i
141
140
  end
141
+ end
142
142
 
143
- def params
144
- return {} unless uri.query
145
- # This mapping is necessary because CGI.parse returns a hash of keys to arrays.
146
- # And *that* is necessary because querystrings support arrays, so they
147
- # force you to deal with it to avoid security issues that would pop up
148
- # if one day it suddenly gave you an array.
149
- #
150
- # However, we aren't interested. Just take the first one.
151
- @params ||= begin
152
- p = {}
153
- CGI.parse(uri.query).each { |k, v| p[k] = v.first }
154
- p
155
- end
156
- rescue
157
- {}
143
+ def params
144
+ return {} unless uri.query
145
+ # This mapping is necessary because CGI.parse returns a hash of keys to arrays.
146
+ # And *that* is necessary because querystrings support arrays, so they
147
+ # force you to deal with it to avoid security issues that would pop up
148
+ # if one day it suddenly gave you an array.
149
+ #
150
+ # However, we aren't interested. Just take the first one.
151
+ @params ||= begin
152
+ p = {}
153
+ CGI.parse(uri.query).each { |k, v| p[k] = v.first }
154
+ p
158
155
  end
156
+ rescue
157
+ {}
158
+ end
159
159
 
160
160
  end
161
161
  end