onebox 1.8.79 → 1.8.80

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 896effdb129adbeb1e51707f925b21e607cae0b6dd5c18da33df140e5b1bdc6d
4
- data.tar.gz: 10979bdfa001024538c7a141fe1ff945c6212ee2279c4b053bb33bcfedcdd8fb
3
+ metadata.gz: 6f4025cb791c71ae3c8e60721222523344220036885b580ab8b95724a3919f9f
4
+ data.tar.gz: c86237739a577b807887bcab7888b79c69abe4e20a79ad3bd64a44254445a940
5
5
  SHA512:
6
- metadata.gz: 3fad55242a2919216c2f23a9fd0ca68cb11f5f6a4d9c6cfdd78e90e702f42a91ef80c102ffe47a287b90b79584715650deb727d432273905b191b956f47cd5cb
7
- data.tar.gz: 137a2739c8e81a2f05626e66e225c90c37068f3c449cc4caeedf8864c62b408a56678d932a3bf713b836b89a23f0e29eae7a637d5066fb41c8a5553e40e75ef3
6
+ metadata.gz: ab7468411bd2f318d70c1d1621f92f90f3c378df3f28ff57abfa744e3778cf1817c9ed486ad52ed00c428fdd782c43f4df68de20c8ff1b8cef0e343a552940ea
7
+ data.tar.gz: 180a8b88df76668dc6d0f73b35aa28ab21587208c42892d02f024e37dee66f403bb31cacd9cd2160b4b7c8596864652d837bdde8e793fd099204b71046e4a21f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- onebox (1.8.78)
4
+ onebox (1.8.79)
5
5
  htmlentities (~> 4.3)
6
6
  moneta (~> 1.0)
7
7
  multi_json (~> 1.11)
@@ -1,4 +1,5 @@
1
1
  require 'json'
2
+ require "onebox/open_graph"
2
3
 
3
4
  module Onebox
4
5
  module Engine
@@ -76,7 +77,7 @@ module Onebox
76
77
  end
77
78
 
78
79
  def data
79
- og = ::Onebox::Helpers.extract_opengraph(raw)
80
+ og = ::Onebox::OpenGraph.new(raw)
80
81
 
81
82
  if raw.at_css('#dp.book_mobile') #printed books
82
83
  title = raw.at("h1#title")&.inner_text
@@ -100,7 +101,7 @@ module Onebox
100
101
  link: link,
101
102
  title: title,
102
103
  by_info: authors,
103
- image: og[:image] || image,
104
+ image: og.image || image,
104
105
  description: raw.at("#productDescription")&.inner_text,
105
106
  rating: "#{rating}#{', ' if rating && (!isbn&.empty? || !price&.empty?)}",
106
107
  price: price,
@@ -131,7 +132,7 @@ module Onebox
131
132
  link: link,
132
133
  title: title,
133
134
  by_info: authors,
134
- image: og[:image] || image,
135
+ image: og.image || image,
135
136
  description: raw.at("#productDescription")&.inner_text,
136
137
  rating: "#{rating}#{', ' if rating && (!asin&.empty? || !price&.empty?)}",
137
138
  price: price,
@@ -142,11 +143,11 @@ module Onebox
142
143
  }
143
144
 
144
145
  else
145
- title = og[:title] || CGI.unescapeHTML(raw.css("title").inner_text)
146
+ title = og.title || CGI.unescapeHTML(raw.css("title").inner_text)
146
147
  result = {
147
148
  link: link,
148
149
  title: title,
149
- image: og[:image] || image,
150
+ image: og.image || image,
150
151
  price: price
151
152
  }
152
153
 
@@ -154,7 +155,7 @@ module Onebox
154
155
  result[:by_info] = Onebox::Helpers.clean(result[:by_info].inner_html) if result[:by_info]
155
156
 
156
157
  summary = raw.at("#productDescription")
157
- result[:description] = og[:description] || (summary && summary.inner_text)
158
+ result[:description] = og.description || (summary && summary.inner_text)
158
159
  end
159
160
 
160
161
  result[:price] = nil if result[:price].start_with?("$0") || result[:price] == 0
@@ -9,16 +9,14 @@ module Onebox
9
9
 
10
10
  def placeholder_html
11
11
  oembed = get_oembed
12
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(oembed[:thumbnail_url])
13
12
 
14
13
  <<-HTML
15
- <img src="#{escaped_src}" style="max-width: #{oembed[:width]}px; max-height: #{oembed[:height]}px;" #{Helpers.title_attr(oembed)}>
14
+ <img src="#{oembed.thumbnail_url}" style="max-width: #{oembed.width}px; max-height: #{oembed.height}px;" #{oembed.title_attr}>
16
15
  HTML
17
16
  end
18
17
 
19
18
  def to_html
20
- oembed = get_oembed
21
- oembed[:html]
19
+ get_oembed.html
22
20
  end
23
21
 
24
22
  end
@@ -9,19 +9,17 @@ module Onebox
9
9
 
10
10
  def placeholder_html
11
11
  og = get_opengraph
12
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(og[:image])
13
- "<img src='#{escaped_src}' height='#{og[:video_height]}' #{Helpers.title_attr(og)}>"
12
+ "<img src='#{og.image}' height='#{og.video_height}' #{og.title_attr}>"
14
13
  end
15
14
 
16
15
  def to_html
17
16
  og = get_opengraph
18
- src = og[:video_secure_url] || og[:video]
19
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(src)
17
+ escaped_src = og.video_secure_url || og.video
20
18
 
21
19
  <<-HTML
22
20
  <iframe src="#{escaped_src}"
23
- width="#{og[:video_width]}"
24
- height="#{og[:video_height]}"
21
+ width="#{og.video_width}"
22
+ height="#{og.video_height}"
25
23
  scrolling="no"
26
24
  frameborder="0"
27
25
  allowfullscreen>
@@ -10,9 +10,9 @@ module Onebox
10
10
  def to_html
11
11
  og = get_opengraph
12
12
 
13
- if !Onebox::Helpers::blank?(og[:image])
13
+ if !og.image.nil?
14
14
  return image_html(og)
15
- elsif og[:title].to_s[/\.(mp4|ogv|webm)$/]
15
+ elsif og.title.to_s[/\.(mp4|ogv|webm)$/]
16
16
  return video_html(og)
17
17
  else
18
18
  return link_html(og)
@@ -22,33 +22,27 @@ module Onebox
22
22
  private
23
23
 
24
24
  def link_html(og)
25
- escaped_url = ::Onebox::Helpers.normalize_url_for_output(og[:url])
26
-
27
25
  <<-HTML
28
- <a href='#{escaped_url}' target='_blank'>
29
- #{og[:title]}
26
+ <a href='#{og.url}' target='_blank'>
27
+ #{og.title}
30
28
  </a>
31
29
  HTML
32
30
  end
33
31
 
34
32
  def video_html(og)
35
- src = og[:url]
36
- title = og[:title]
37
- direct_src = ::Onebox::Helpers.normalize_url_for_output("#{src}/#{title}")
33
+ direct_src = ::Onebox::Helpers.normalize_url_for_output("#{og.get(:url)}/#{og.title}")
38
34
 
39
35
  <<-HTML
40
- <video width='480' height='360' #{Helpers.title_attr(og)} controls loop>
36
+ <video width='480' height='360' #{og.title_attr} controls loop>
41
37
  <source src='#{direct_src}' type='video/mp4'>
42
38
  </video>
43
39
  HTML
44
40
  end
45
41
 
46
42
  def image_html(og)
47
- escaped_url = ::Onebox::Helpers.normalize_url_for_output(og[:url])
48
-
49
43
  <<-HTML
50
- <a href='#{escaped_url}' target='_blank' class='onebox'>
51
- <img src='#{og[:image]}' #{Helpers.title_attr(og)} alt='CloudApp' width='480'>
44
+ <a href='#{og.url}' target='_blank' class='onebox'>
45
+ <img src='#{og.image}' #{og.title_attr} alt='CloudApp' width='480'>
52
46
  </a>
53
47
  HTML
54
48
  end
@@ -9,12 +9,11 @@ module Onebox
9
9
 
10
10
  def placeholder_html
11
11
  oembed = get_oembed
12
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(oembed[:thumbnail_url])
13
- "<img src='#{escaped_src}' height='#{oembed[:thumbnail_height]}' width='#{oembed[:thumbnail_width]}' #{Helpers.title_attr(oembed)}>"
12
+ "<img src='#{oembed.thumbnail_url}' height='#{oembed.thumbnail_height}' width='#{oembed.thumbnail_width}' #{oembed.title_attr}>"
14
13
  end
15
14
 
16
15
  def to_html
17
- get_oembed[:html]
16
+ get_oembed.html
18
17
  end
19
18
 
20
19
  end
@@ -9,8 +9,7 @@ module Onebox
9
9
 
10
10
  def to_html
11
11
  og = get_opengraph
12
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(og[:image])
13
- "<img src='#{escaped_src}' width='#{og[:image_width]}' height='#{og[:image_height]}' class='onebox' #{Helpers.title_attr(og)}>"
12
+ "<img src='#{og.image}' width='#{og.image_width}' height='#{og.image_height}' class='onebox' #{og.title_attr}>"
14
13
  end
15
14
 
16
15
  end
@@ -9,11 +9,10 @@ module Onebox
9
9
 
10
10
  def to_html
11
11
  oembed = get_oembed
12
- escaped_url = ::Onebox::Helpers.normalize_url_for_output(oembed[:url])
13
12
 
14
13
  <<-HTML
15
- <a href="#{escaped_url}" target="_blank" class="onebox">
16
- <img src="#{escaped_url}" width="#{oembed[:width]}" height="#{oembed[:height]}" #{Helpers.title_attr(oembed)}>
14
+ <a href="#{oembed.url}" target="_blank" class="onebox">
15
+ <img src="#{oembed.url}" width="#{oembed.width}" height="#{oembed.height}" #{oembed.title_attr}>
17
16
  </a>
18
17
  HTML
19
18
  end
@@ -9,39 +9,35 @@ module Onebox
9
9
 
10
10
  def to_html
11
11
  og = get_opengraph
12
- return video_html(og) if !Onebox::Helpers::blank?(og[:video_secure_url])
13
- return album_html(og) if !Onebox::Helpers::blank?(og[:type]) && og[:type] == "google_photos:photo_album"
14
- return image_html(og) if !Onebox::Helpers::blank?(og[:image])
12
+ return video_html(og) if !og.video_secure_url.nil?
13
+ return album_html(og) if !og.type.nil? && og.type == "google_photos:photo_album"
14
+ return image_html(og) if !og.image.nil?
15
15
  nil
16
16
  end
17
17
 
18
18
  private
19
19
 
20
20
  def video_html(og)
21
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(og[:video_secure_url])
22
- escaped_image_src = ::Onebox::Helpers.normalize_url_for_output(get_secure_link(og[:image]))
23
-
24
21
  <<-HTML
25
- <video width='#{og[:video_width]}' height='#{og[:video_height]}' #{Helpers.title_attr(og)} poster="#{escaped_image_src}" controls loop>
26
- <source src='#{escaped_src}' type='video/mp4'>
22
+ <video width='#{og.video_width}' height='#{og.video_height}' #{og.title_attr} poster="#{og.get_secure_image}" controls loop>
23
+ <source src='#{og.video_secure_url}' type='video/mp4'>
27
24
  </video>
28
25
  HTML
29
26
  end
30
27
 
31
28
  def album_html(og)
32
29
  escaped_url = ::Onebox::Helpers.normalize_url_for_output(url)
33
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(get_secure_link(og[:image]))
34
- album_title = Onebox::Helpers::blank?(og[:description]) ? og[:title].strip : "[#{og[:description].strip}] #{og[:title].strip}"
30
+ album_title = og.description.nil? ? og.title : "[#{og.description}] #{og.title}"
35
31
 
36
32
  <<-HTML
37
33
  <div class='onebox google-photos-album'>
38
34
  <a href='#{escaped_url}' target='_blank'>
39
- <span class='outer-box' style='width:#{og[:image_width]}px'>
35
+ <span class='outer-box' style='width:#{og.image_width}px'>
40
36
  <span class='inner-box'>
41
37
  <span class='album-title'>#{Onebox::Helpers.truncate(album_title, 80)}</span>
42
38
  </span>
43
39
  </span>
44
- <img src='#{escaped_src}' #{Helpers.title_attr(og)} height='#{og[:image_height]}' width='#{og[:image_width]}'>
40
+ <img src='#{og.get_secure_image}' #{og.title_attr} height='#{og.image_height}' width='#{og.image_width}'>
45
41
  </a>
46
42
  </div>
47
43
  HTML
@@ -49,20 +45,13 @@ module Onebox
49
45
 
50
46
  def image_html(og)
51
47
  escaped_url = ::Onebox::Helpers.normalize_url_for_output(url)
52
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(get_secure_link(og[:image]))
53
48
 
54
49
  <<-HTML
55
50
  <a href='#{escaped_url}' target='_blank' class="onebox">
56
- <img src='#{escaped_src}' #{Helpers.title_attr(og)} alt='Google Photos' height='#{og[:image_height]}' width='#{og[:image_width]}'>
51
+ <img src='#{og.get_secure_image}' #{og.title_attr} alt='Google Photos' height='#{og.image_height}' width='#{og.image_width}'>
57
52
  </a>
58
53
  HTML
59
54
  end
60
-
61
- def get_secure_link(link)
62
- secure_link = URI(link)
63
- secure_link.scheme = 'https'
64
- secure_link.to_s
65
- end
66
55
  end
67
56
  end
68
57
  end
@@ -9,39 +9,36 @@ module Onebox
9
9
 
10
10
  def to_html
11
11
  og = get_opengraph
12
- return video_html(og) if !Onebox::Helpers::blank?(og[:video_secure_url])
12
+ return video_html(og) if !og.video_secure_url.nil?
13
13
  return album_html(og) if is_album?
14
- return image_html(og) if !Onebox::Helpers::blank?(og[:image])
14
+ return image_html(og) if !og.image.nil?
15
15
  nil
16
16
  end
17
17
 
18
18
  private
19
19
 
20
20
  def video_html(og)
21
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(og[:video_secure_url])
22
-
23
21
  <<-HTML
24
- <video width='#{og[:video_width]}' height='#{og[:video_height]}' #{Helpers.title_attr(og)} controls loop>
25
- <source src='#{escaped_src}' type='video/mp4'>
26
- <source src='#{escaped_src.gsub('mp4', 'webm')}' type='video/webm'>
22
+ <video width='#{og.video_width}' height='#{og.video_height}' #{og.title_attr} controls loop>
23
+ <source src='#{og.video_secure_url}' type='video/mp4'>
24
+ <source src='#{og.video_secure_url.gsub('mp4', 'webm')}' type='video/webm'>
27
25
  </video>
28
26
  HTML
29
27
  end
30
28
 
31
29
  def album_html(og)
32
30
  escaped_url = ::Onebox::Helpers.normalize_url_for_output(url)
33
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(get_secure_link(og[:image]))
34
- album_title = "[Album] #{Onebox::Helpers.truncate(og[:title], 80)}"
31
+ album_title = "[Album] #{og.title}"
35
32
 
36
33
  <<-HTML
37
34
  <div class='onebox imgur-album'>
38
35
  <a href='#{escaped_url}' target='_blank'>
39
- <span class='outer-box' style='width:#{og[:image_width]}px'>
36
+ <span class='outer-box' style='width:#{og.image_width}px'>
40
37
  <span class='inner-box'>
41
38
  <span class='album-title'>#{album_title}</span>
42
39
  </span>
43
40
  </span>
44
- <img src='#{escaped_src}' #{Helpers.title_attr(og)} height='#{og[:image_height]}' width='#{og[:image_width]}'>
41
+ <img src='#{og.get_secure_image}' #{og.title_attr} height='#{og.image_height}' width='#{og.image_width}'>
45
42
  </a>
46
43
  </div>
47
44
  HTML
@@ -56,21 +53,13 @@ module Onebox
56
53
 
57
54
  def image_html(og)
58
55
  escaped_url = ::Onebox::Helpers.normalize_url_for_output(url)
59
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(get_secure_link(og[:image]))
60
56
 
61
57
  <<-HTML
62
58
  <a href='#{escaped_url}' target='_blank' class="onebox">
63
- <img src='#{escaped_src}' #{Helpers.title_attr(og)} alt='Imgur' height='#{og[:image_height]}' width='#{og[:image_width]}'>
59
+ <img src='#{og.get_secure_image}' #{og.title_attr} alt='Imgur' height='#{og.image_height}' width='#{og.image_width}'>
64
60
  </a>
65
61
  HTML
66
62
  end
67
-
68
- def get_secure_link(link)
69
- secure_link = URI(link)
70
- secure_link.scheme = 'https'
71
- secure_link.to_s
72
- end
73
-
74
63
  end
75
64
  end
76
65
  end
@@ -12,24 +12,18 @@ module Onebox
12
12
 
13
13
  def data
14
14
  og = get_opengraph
15
- title = og[:title].split(":")[0].strip.gsub(" on Instagram", "")
16
- html_entities = HTMLEntities.new
15
+ title = og.title.split(":")[0].strip.gsub(" on Instagram", "")
17
16
 
18
17
  json_data = html_doc.xpath('//script[contains(text(),"window._sharedData")]').text.to_s
19
18
  title = "[Album] #{title}" if json_data =~ /"edge_sidecar_to_children"/
20
19
 
21
- result = { link: og[:url],
22
- title: html_entities.decode(Onebox::Helpers.truncate(title, 80)),
23
- description: html_entities.decode(Onebox::Helpers.truncate(og[:description], 250))
20
+ result = { link: og.url,
21
+ title: Onebox::Helpers.truncate(title, 80),
22
+ description: og.description(250)
24
23
  }
25
24
 
26
- if !Onebox::Helpers.blank?(og[:image])
27
- result[:image] = ::Onebox::Helpers.normalize_url_for_output(og[:image])
28
- end
29
-
30
- if !Onebox::Helpers.blank?(og[:video_secure_url])
31
- result[:video_link] = og[:url]
32
- end
25
+ result[:image] = og.image if !og.image.nil?
26
+ result[:video_link] = og.url if !og.video_secure_url.nil?
33
27
 
34
28
  result
35
29
  end
@@ -9,20 +9,18 @@ module Onebox
9
9
 
10
10
  def preview_html
11
11
  og = get_opengraph
12
- thumbnail_url = ::Onebox::Helpers.normalize_url_for_output(og[:image_secure_url])
13
12
 
14
13
  <<~HTML
15
- <img src="#{thumbnail_url}" width="#{og[:video_width]}" height="#{og[:video_height]}" >
14
+ <img src="#{og.image_secure_url}" width="#{og.video_width}" height="#{og.video_height}" >
16
15
  HTML
17
16
  end
18
17
 
19
18
  def to_html
20
19
  og = get_opengraph
21
- embedded_video_url = ::Onebox::Helpers.normalize_url_for_output(og[:video_secure_url])
22
20
 
23
21
  <<~HTML
24
- <iframe src="#{embedded_video_url}"
25
- width="#{og[:video_width]}" height="#{og[:video_height]}"
22
+ <iframe src="#{og.video_secure_url}"
23
+ width="#{og.video_width}" height="#{og.video_height}"
26
24
  frameborder='0'
27
25
  allowfullscreen >
28
26
  </iframe>
@@ -9,12 +9,11 @@ module Onebox
9
9
 
10
10
  def placeholder_html
11
11
  oembed = get_oembed
12
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(oembed[:image])
13
- "<img src='#{escaped_src}' height='#{oembed[:height]}' #{Helpers.title_attr(oembed)}>"
12
+ "<img src='#{oembed.image}' height='#{oembed.height}' #{oembed.title_attr}>"
14
13
  end
15
14
 
16
15
  def to_html
17
- get_oembed[:html]
16
+ get_oembed.html
18
17
  end
19
18
  end
20
19
  end
@@ -4,8 +4,7 @@ module Onebox
4
4
 
5
5
  def to_html
6
6
  og = get_opengraph
7
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(og[:image])
8
- "<img src='#{escaped_src}' width='#{og[:image_width]}' height='#{og[:image_height]}' class='onebox' #{Helpers.title_attr(og)}>"
7
+ "<img src='#{og.image}' width='#{og.image_width}' height='#{og.image_height}' class='onebox' #{og.title_attr}>"
9
8
  end
10
9
 
11
10
  end
@@ -9,16 +9,14 @@ module Onebox
9
9
 
10
10
  def placeholder_html
11
11
  oembed = get_oembed
12
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(oembed[:thumbnail_url])
13
12
 
14
13
  <<-HTML
15
- <img src="#{escaped_src}" style="max-width: #{oembed[:width]}px; max-height: #{oembed[:height]}px;" #{Helpers.title_attr(oembed)}>
14
+ <img src="#{oembed.thumbnail_url}" style="max-width: #{oembed.width}px; max-height: #{oembed.height}px;" #{oembed.title_attr}>
16
15
  HTML
17
16
  end
18
17
 
19
18
  def to_html
20
- oembed = get_oembed
21
- oembed[:html]
19
+ get_oembed.html
22
20
  end
23
21
 
24
22
  end
@@ -8,15 +8,13 @@ module Onebox
8
8
  always_https
9
9
 
10
10
  def to_html
11
- opengraph = get_opengraph
12
-
13
- src = opengraph[:video_url].gsub("autostart=1", "")
14
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(src)
11
+ og = get_opengraph
12
+ src = og.video_url.gsub("autostart=1", "")
15
13
 
16
14
  <<-HTML
17
- <iframe src="#{escaped_src}"
18
- width="#{opengraph[:video_width]}"
19
- height="#{opengraph[:video_height]}"
15
+ <iframe src="#{src}"
16
+ width="#{og.video_width}"
17
+ height="#{og.video_height}"
20
18
  scrolling="no"
21
19
  frameborder="0"
22
20
  allowfullscreen>
@@ -25,9 +23,7 @@ module Onebox
25
23
  end
26
24
 
27
25
  def placeholder_html
28
- opengraph = get_opengraph
29
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(opengraph[:image])
30
- "<img src='#{escaped_src}'>"
26
+ "<img src='#{get_opengraph.image}'>"
31
27
  end
32
28
 
33
29
  end
@@ -8,26 +8,22 @@ module Onebox
8
8
  always_https
9
9
 
10
10
  def to_html
11
- oembed_data[:html].gsub('visual=true', 'visual=false')
11
+ oembed = get_oembed
12
+ oembed.html.gsub('visual=true', 'visual=false')
12
13
  end
13
14
 
14
15
  def placeholder_html
15
- return if Onebox::Helpers.blank?(oembed_data[:thumbnail_url])
16
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(oembed_data[:thumbnail_url])
17
- "<img src='#{escaped_src}' #{Helpers.title_attr(oembed_data)}>"
16
+ oembed = get_oembed
17
+ return if Onebox::Helpers.blank?(oembed.thumbnail_url)
18
+ "<img src='#{oembed.thumbnail_url}' #{oembed.title_attr}>"
18
19
  end
19
20
 
20
- private
21
+ protected
21
22
 
22
- def oembed_data
23
- @oembed_data ||= begin
24
- oembed_url = "https://soundcloud.com/oembed.json?url=#{url}"
25
- oembed_url << "&maxheight=166" unless url["/sets/"]
26
- response = Onebox::Helpers.fetch_response(oembed_url) rescue "{}"
27
- Onebox::Helpers.symbolize_keys(::MultiJson.load(response))
28
- rescue
29
- {}
30
- end
23
+ def get_oembed_url
24
+ oembed_url = "https://soundcloud.com/oembed.json?url=#{url}"
25
+ oembed_url << "&maxheight=166" unless url["/sets/"]
26
+ oembed_url
31
27
  end
32
28
 
33
29
  end
@@ -1,4 +1,5 @@
1
1
  require "cgi"
2
+ require "onebox/open_graph"
2
3
 
3
4
  module Onebox
4
5
  module Engine
@@ -41,9 +42,18 @@ module Onebox
41
42
 
42
43
  @raw = {}
43
44
 
44
- og.each { |k, v| @raw[k] ||= v unless Onebox::Helpers::blank?(v) }
45
+ og.data.each do |k, v|
46
+ next if k == "title_attr"
47
+ v = og.send(k)
48
+ @raw[k] ||= v unless v.nil?
49
+ end
50
+
45
51
  twitter.each { |k, v| @raw[k] ||= v unless Onebox::Helpers::blank?(v) }
46
- oembed.each { |k, v| @raw[k] ||= v unless Onebox::Helpers::blank?(v) }
52
+
53
+ oembed.data.each do |k, v|
54
+ v = oembed.send(k)
55
+ @raw[k] ||= v unless v.nil?
56
+ end
47
57
 
48
58
  favicon = get_favicon
49
59
  @raw["favicon".to_sym] = favicon unless Onebox::Helpers::blank?(favicon)
@@ -63,42 +73,11 @@ module Onebox
63
73
  end
64
74
 
65
75
  def get_oembed
66
- oembed_url = nil
67
-
68
- StandardEmbed.oembed_providers.each do |regexp, endpoint|
69
- if url =~ regexp
70
- oembed_url = "#{endpoint}?url=#{url}"
71
- break
72
- end
73
- end
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
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
85
- end
86
-
87
- return {} if Onebox::Helpers.blank?(oembed_url)
88
-
89
- json_response = Onebox::Helpers.fetch_response(oembed_url) rescue "{}"
90
- oe = Onebox::Helpers.symbolize_keys(::MultiJson.load(json_response))
91
-
92
- # never use oembed from WordPress 4.4 (it's broken)
93
- oe.delete(:html) if oe[:html] && oe[:html]["wp-embedded-content"]
94
-
95
- oe
96
- rescue Errno::ECONNREFUSED, Net::HTTPError, Net::HTTPFatalError, MultiJson::LoadError
97
- {}
76
+ @oembed ||= Onebox::Oembed.new(get_json_response)
98
77
  end
99
78
 
100
79
  def get_opengraph
101
- ::Onebox::Helpers.extract_opengraph(html_doc)
80
+ @opengraph ||= ::Onebox::OpenGraph.new(html_doc)
102
81
  end
103
82
 
104
83
  def get_twitter
@@ -124,6 +103,43 @@ module Onebox
124
103
 
125
104
  Onebox::Helpers::get_absolute_image_url(favicon, url)
126
105
  end
106
+
107
+ def get_json_response
108
+ oembed_url = get_oembed_url
109
+
110
+ return "{}" if Onebox::Helpers.blank?(oembed_url)
111
+
112
+ Onebox::Helpers.fetch_response(oembed_url) rescue "{}"
113
+ rescue Errno::ECONNREFUSED, Net::HTTPError, Net::HTTPFatalError, MultiJson::LoadError
114
+ "{}"
115
+ end
116
+
117
+ protected
118
+
119
+ def get_oembed_url
120
+ oembed_url = nil
121
+
122
+ StandardEmbed.oembed_providers.each do |regexp, endpoint|
123
+ if url =~ regexp
124
+ oembed_url = "#{endpoint}?url=#{url}"
125
+ break
126
+ end
127
+ end
128
+
129
+ if html_doc
130
+ if Onebox::Helpers.blank?(oembed_url)
131
+ application_json = html_doc.at("//link[@type='application/json+oembed']/@href")
132
+ oembed_url = application_json.value if application_json
133
+ end
134
+
135
+ if Onebox::Helpers.blank?(oembed_url)
136
+ text_json = html_doc.at("//link[@type='text/json+oembed']/@href")
137
+ oembed_url ||= text_json.value if text_json
138
+ end
139
+ end
140
+
141
+ oembed_url
142
+ end
127
143
  end
128
144
  end
129
145
  end
@@ -9,13 +9,12 @@ module Onebox
9
9
 
10
10
  def placeholder_html
11
11
  og = get_opengraph
12
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(og[:image])
13
12
  <<-HTML
14
13
  <div style='width:100%; height:190px; background-color:#262626; color:#9e9e9e; margin:15px 0;'>
15
14
  <div style='padding:10px'>
16
- <h3 style='color:#fff; margin:10px 0 10px 5px;'>#{og[:title]}</h3>
17
- <img src='#{escaped_src}' style='float:left; max-width:184px; margin:5px 15px 0 5px'/>
18
- <p>#{og[:description]}</p>
15
+ <h3 style='color:#fff; margin:10px 0 10px 5px;'>#{og.title}</h3>
16
+ <img src='#{og.image}' style='float:left; max-width:184px; margin:5px 15px 0 5px'/>
17
+ <p>#{og.description}</p>
19
18
  </div>
20
19
  </div>
21
20
  HTML
@@ -9,12 +9,11 @@ module Onebox
9
9
 
10
10
  def placeholder_html
11
11
  oembed = get_oembed
12
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(oembed[:thumbnail_url])
13
- "<img src='#{escaped_src}' width='#{oembed[:thumbnail_width]}' height='#{oembed[:thumbnail_height]}' #{Helpers.title_attr(oembed)}>"
12
+ "<img src='#{oembed.thumbnail_url}' width='#{oembed.thumbnail_width}' height='#{oembed.thumbnail_height}' #{oembed.title_attr}>"
14
13
  end
15
14
 
16
15
  def to_html
17
- get_oembed[:html]
16
+ get_oembed.html
18
17
  end
19
18
  end
20
19
  end
@@ -8,24 +8,19 @@ module Onebox
8
8
  always_https
9
9
 
10
10
  def to_html
11
- oembed_data[:html]
11
+ get_oembed.html
12
12
  end
13
13
 
14
14
  def placeholder_html
15
- return if Onebox::Helpers.blank?(oembed_data[:thumbnail_url])
16
- escaped_src = ::Onebox::Helpers.normalize_url_for_output(oembed_data[:thumbnail_url])
17
- "<img src='#{escaped_src}' #{Helpers.title_attr(oembed_data)}>"
15
+ oembed = get_oembed
16
+ return if Onebox::Helpers.blank?(oembed.thumbnail_url)
17
+ "<img src='#{oembed.thumbnail_url}' #{oembed.title_attr}>"
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
+
22
+ def get_oembed_url
23
+ "https://fast.wistia.com/oembed?embedType=iframe&url=#{url}"
29
24
  end
30
25
  end
31
26
  end
@@ -1,3 +1,5 @@
1
+ require 'onebox/oembed'
2
+
1
3
  module Onebox
2
4
  module Engine
3
5
  class YoutubeOnebox
@@ -12,9 +14,9 @@ module Onebox
12
14
 
13
15
  def placeholder_html
14
16
  if video_id
15
- "<img src='https://i.ytimg.com/vi/#{video_id}/hqdefault.jpg' width='#{WIDTH}' height='#{HEIGHT}' #{Helpers.title_attr(video_oembed_data)}>"
17
+ "<img src='https://i.ytimg.com/vi/#{video_id}/hqdefault.jpg' width='#{WIDTH}' height='#{HEIGHT}' #{video_oembed_data.title_attr}>"
16
18
  elsif list_id
17
- "<img src='#{list_thumbnail_url}' width='#{WIDTH}' height='#{HEIGHT}' #{Helpers.title_attr(list_oembed_data)}>"
19
+ "<img src='#{list_thumbnail_url}' width='#{WIDTH}' height='#{HEIGHT}' #{list_oembed_data.title_attr}>"
18
20
  else
19
21
  to_html
20
22
  end
@@ -49,11 +51,7 @@ module Onebox
49
51
  end
50
52
 
51
53
  def video_title
52
- @video_title ||= begin
53
- Onebox::Helpers.truncate(video_oembed_data[:title], 80)
54
- rescue
55
- nil
56
- end
54
+ @video_title ||= video_oembed_data.title
57
55
  end
58
56
 
59
57
  private
@@ -85,8 +83,8 @@ module Onebox
85
83
  @list_thumbnail_url ||= begin
86
84
  url = "https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/playlist?list=#{list_id}"
87
85
  response = Onebox::Helpers.fetch_response(url) rescue "{}"
88
- data = Onebox::Helpers.symbolize_keys(::MultiJson.load(response))
89
- data[:thumbnail_url]
86
+ data = Onebox::Oembed.new(response)
87
+ data.thumbnail_url
90
88
  rescue
91
89
  nil
92
90
  end
@@ -95,13 +93,13 @@ module Onebox
95
93
  def video_oembed_data
96
94
  url = "https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=#{video_id}"
97
95
  response = Onebox::Helpers.fetch_response(url) rescue "{}"
98
- Onebox::Helpers.symbolize_keys(::MultiJson.load(response))
96
+ Onebox::Oembed.new(response)
99
97
  end
100
98
 
101
99
  def list_oembed_data
102
100
  url = "https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/playlist?list=#{list_id}"
103
101
  response = Onebox::Helpers.fetch_response(url) rescue "{}"
104
- Onebox::Helpers.symbolize_keys(::MultiJson.load(response))
102
+ Onebox::Oembed.new(response)
105
103
  end
106
104
 
107
105
  def embed_params
@@ -18,27 +18,6 @@ module Onebox
18
18
  html.gsub(/<[^>]+>/, ' ').gsub(/\n/, '')
19
19
  end
20
20
 
21
- def self.extract_opengraph(doc)
22
- return {} unless doc
23
-
24
- og = {}
25
-
26
- doc.css('meta').each do |m|
27
- if (m["property"] && m["property"][/^(?:og|article|product):(.+)$/i]) || (m["name"] && m["name"][/^(?:og|article|product):(.+)$/i])
28
- value = (m["content"] || m["value"]).to_s
29
- og[$1.tr('-:', '_').to_sym] ||= value unless Onebox::Helpers::blank?(value)
30
- end
31
- end
32
-
33
- # Attempt to retrieve the title from the meta tag
34
- title_element = doc.at_css('title')
35
- if title_element && title_element.text
36
- og[:title] ||= title_element.text unless Onebox::Helpers.blank?(title_element.text)
37
- end
38
-
39
- og
40
- end
41
-
42
21
  def self.fetch_html_doc(url, headers = nil)
43
22
  response = (fetch_response(url, nil, nil, headers) rescue nil)
44
23
  doc = Nokogiri::HTML(response)
@@ -169,15 +148,9 @@ module Onebox
169
148
  end
170
149
 
171
150
  def self.truncate(string, length = 50)
172
- string = sanitize(string)
173
151
  string.size > length ? string[0...(string.rindex(" ", length) || length)] + "..." : string
174
152
  end
175
153
 
176
- def self.title_attr(meta)
177
- title = get(meta, :title)
178
- !title.nil? ? "title='#{title}'" : ""
179
- end
180
-
181
154
  def self.get(meta, attr)
182
155
  (meta && !blank?(meta[attr])) ? sanitize(meta[attr]) : nil
183
156
  end
@@ -0,0 +1,12 @@
1
+ module Onebox
2
+ class Oembed < OpenGraph
3
+
4
+ def initialize(response)
5
+ @data = Onebox::Helpers.symbolize_keys(::MultiJson.load(response))
6
+
7
+ # never use oembed from WordPress 4.4 (it's broken)
8
+ data.delete(:html) if data[:html] && data[:html]["wp-embedded-content"]
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,88 @@
1
+ module Onebox
2
+ class OpenGraph
3
+
4
+ attr_reader :data
5
+
6
+ def initialize(doc)
7
+ @data = extract(doc)
8
+ end
9
+
10
+ def title
11
+ get(:title, 80)
12
+ end
13
+
14
+ def title_attr
15
+ !title.nil? ? "title='#{title}'" : ""
16
+ end
17
+
18
+ def get_secure_image
19
+ secure_link = URI(get(:image))
20
+ secure_link.scheme = 'https'
21
+ secure_link.to_s
22
+ end
23
+
24
+ def method_missing(attr, *args, &block)
25
+ value = get(attr, *args)
26
+
27
+ return nil if Onebox::Helpers::blank?(value)
28
+
29
+ method_name = attr.to_s
30
+ if method_name.end_with?(*integer_suffixes)
31
+ value.to_i
32
+ elsif method_name.end_with?(*url_suffixes)
33
+ ::Onebox::Helpers.normalize_url_for_output(value)
34
+ else
35
+ value
36
+ end
37
+ end
38
+
39
+ def get(attr, length = nil)
40
+ return nil if Onebox::Helpers::blank?(data)
41
+
42
+ value = data[attr]
43
+
44
+ return nil if Onebox::Helpers::blank?(value)
45
+
46
+ value = Sanitize.fragment(html_entities.decode(value)).strip
47
+ value = Onebox::Helpers.truncate(value, length) unless length.nil?
48
+
49
+ value
50
+ end
51
+
52
+ private
53
+
54
+ def integer_suffixes
55
+ ['width', 'height']
56
+ end
57
+
58
+ def url_suffixes
59
+ ['url', 'image', 'video']
60
+ end
61
+
62
+ def html_entities
63
+ @html_entities ||= HTMLEntities.new
64
+ end
65
+
66
+ def extract(doc)
67
+ return {} if Onebox::Helpers::blank?(doc)
68
+
69
+ data = {}
70
+
71
+ doc.css('meta').each do |m|
72
+ if (m["property"] && m["property"][/^(?:og|article|product):(.+)$/i]) || (m["name"] && m["name"][/^(?:og|article|product):(.+)$/i])
73
+ value = (m["content"] || m["value"]).to_s
74
+ data[$1.tr('-:', '_').to_sym] ||= value unless Onebox::Helpers::blank?(value)
75
+ end
76
+ end
77
+
78
+ # Attempt to retrieve the title from the meta tag
79
+ title_element = doc.at_css('title')
80
+ if title_element && title_element.text
81
+ data[:title] ||= title_element.text unless Onebox::Helpers.blank?(title_element.text)
82
+ end
83
+
84
+ data
85
+ end
86
+
87
+ end
88
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Onebox
4
- VERSION = "1.8.79"
4
+ VERSION = "1.8.80"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onebox
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.79
4
+ version: 1.8.80
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joanna Zeta
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-02-26 00:00:00.000000000 Z
13
+ date: 2019-03-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: multi_json
@@ -366,6 +366,8 @@ files:
366
366
  - lib/onebox/matcher.rb
367
367
  - lib/onebox/mixins/git_blob_onebox.rb
368
368
  - lib/onebox/mixins/twitch_onebox.rb
369
+ - lib/onebox/oembed.rb
370
+ - lib/onebox/open_graph.rb
369
371
  - lib/onebox/preview.rb
370
372
  - lib/onebox/sanitize_config.rb
371
373
  - lib/onebox/status_check.rb
@@ -416,7 +418,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
416
418
  - !ruby/object:Gem::Version
417
419
  version: '0'
418
420
  requirements: []
419
- rubygems_version: 3.0.1
421
+ rubygems_version: 3.0.2
420
422
  signing_key:
421
423
  specification_version: 4
422
424
  summary: A gem for generating embeddable HTML previews from URLs.