onebox 2.2.10 → 2.2.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +0 -4
  3. data/lib/onebox/engine.rb +7 -11
  4. data/lib/onebox/engine/allowlisted_generic_onebox.rb +11 -18
  5. data/lib/onebox/engine/amazon_onebox.rb +26 -17
  6. data/lib/onebox/engine/asciinema_onebox.rb +0 -1
  7. data/lib/onebox/engine/audioboom_onebox.rb +5 -2
  8. data/lib/onebox/engine/bandcamp_onebox.rb +8 -8
  9. data/lib/onebox/engine/cloudapp_onebox.rb +12 -12
  10. data/lib/onebox/engine/coub_onebox.rb +0 -1
  11. data/lib/onebox/engine/facebook_media_onebox.rb +8 -7
  12. data/lib/onebox/engine/five_hundred_px_onebox.rb +0 -1
  13. data/lib/onebox/engine/flickr_onebox.rb +14 -15
  14. data/lib/onebox/engine/gfycat_onebox.rb +26 -26
  15. data/lib/onebox/engine/giphy_onebox.rb +0 -1
  16. data/lib/onebox/engine/github_blob_onebox.rb +4 -0
  17. data/lib/onebox/engine/github_commit_onebox.rb +13 -16
  18. data/lib/onebox/engine/github_folder_onebox.rb +1 -1
  19. data/lib/onebox/engine/github_gist_onebox.rb +1 -2
  20. data/lib/onebox/engine/github_issue_onebox.rb +16 -18
  21. data/lib/onebox/engine/github_pullrequest_onebox.rb +9 -1
  22. data/lib/onebox/engine/gitlab_blob_onebox.rb +4 -0
  23. data/lib/onebox/engine/google_calendar_onebox.rb +1 -1
  24. data/lib/onebox/engine/google_docs_onebox.rb +23 -41
  25. data/lib/onebox/engine/google_drive_onebox.rb +7 -7
  26. data/lib/onebox/engine/google_maps_onebox.rb +10 -8
  27. data/lib/onebox/engine/google_photos_onebox.rb +18 -18
  28. data/lib/onebox/engine/google_play_app_onebox.rb +3 -7
  29. data/lib/onebox/engine/imgur_onebox.rb +2 -2
  30. data/lib/onebox/engine/instagram_onebox.rb +4 -5
  31. data/lib/onebox/engine/kaltura_onebox.rb +8 -6
  32. data/lib/onebox/engine/opengraph_image.rb +0 -1
  33. data/lib/onebox/engine/pastebin_onebox.rb +11 -15
  34. data/lib/onebox/engine/pdf_onebox.rb +7 -15
  35. data/lib/onebox/engine/pubmed_onebox.rb +17 -12
  36. data/lib/onebox/engine/replit_onebox.rb +0 -1
  37. data/lib/onebox/engine/sketchfab_onebox.rb +8 -8
  38. data/lib/onebox/engine/slides_onebox.rb +10 -10
  39. data/lib/onebox/engine/soundcloud_onebox.rb +0 -1
  40. data/lib/onebox/engine/stack_exchange_onebox.rb +3 -2
  41. data/lib/onebox/engine/standard_embed.rb +2 -5
  42. data/lib/onebox/engine/steam_store_onebox.rb +6 -5
  43. data/lib/onebox/engine/trello_onebox.rb +4 -7
  44. data/lib/onebox/engine/twitch_clips_onebox.rb +1 -3
  45. data/lib/onebox/engine/twitch_stream_onebox.rb +1 -2
  46. data/lib/onebox/engine/twitch_video_onebox.rb +0 -2
  47. data/lib/onebox/engine/typeform_onebox.rb +7 -6
  48. data/lib/onebox/engine/vimeo_onebox.rb +9 -7
  49. data/lib/onebox/engine/wikimedia_onebox.rb +1 -2
  50. data/lib/onebox/engine/wikipedia_onebox.rb +12 -8
  51. data/lib/onebox/engine/youku_onebox.rb +7 -13
  52. data/lib/onebox/engine/youtube_onebox.rb +46 -17
  53. data/lib/onebox/file_type_finder.rb +0 -1
  54. data/lib/onebox/helpers.rb +5 -4
  55. data/lib/onebox/layout.rb +2 -14
  56. data/lib/onebox/matcher.rb +10 -8
  57. data/lib/onebox/mixins/git_blob_onebox.rb +8 -8
  58. data/lib/onebox/mixins/github_body.rb +30 -0
  59. data/lib/onebox/mixins/twitch_onebox.rb +0 -1
  60. data/lib/onebox/open_graph.rb +4 -4
  61. data/lib/onebox/preview.rb +3 -4
  62. data/lib/onebox/sanitize_config.rb +1 -1
  63. data/lib/onebox/version.rb +1 -1
  64. data/templates/_layout.mustache +6 -2
  65. data/templates/allowlistedgeneric.mustache +8 -9
  66. data/templates/amazon.mustache +5 -2
  67. data/templates/github/github_body.mustache +5 -0
  68. data/templates/githubblob.mustache +44 -34
  69. data/templates/githubcommit.mustache +2 -10
  70. data/templates/githubfolder.mustache +2 -2
  71. data/templates/githubgist.mustache +9 -6
  72. data/templates/githubissue.mustache +9 -11
  73. data/templates/githubpullrequest.mustache +3 -2
  74. data/templates/gitlabblob.mustache +11 -4
  75. data/templates/googledocs.mustache +2 -2
  76. data/templates/googledrive.mustache +2 -2
  77. data/templates/googleplayapp.mustache +2 -1
  78. data/templates/instagram.mustache +1 -1
  79. data/templates/pastebin.mustache +6 -2
  80. data/templates/pdf.mustache +6 -3
  81. data/templates/stackexchange.mustache +1 -0
  82. data/templates/twitterstatus.mustache +20 -5
  83. data/templates/wikimedia.mustache +2 -2
  84. data/templates/wikipedia.mustache +2 -2
  85. data/templates/xkcd.mustache +2 -2
  86. metadata +4 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f942eef9390a961ec26aa8d4301bf83eedabe577d067639ab6e0036ca20e02b9
4
- data.tar.gz: b05c99bd27f025edc02475e38cc28b900bfa2bf64e1c5f9cd4be74cd5ab927a4
3
+ metadata.gz: 5e2169ef1cfb44f0208566f5ecff7bd7c5d2bbe2f12b58791f50aa24b776e56d
4
+ data.tar.gz: 9f9d38b578e46e6ce8c8c007577ff02522eab24f3dcc42f9eabd5b838a668e72
5
5
  SHA512:
6
- metadata.gz: '081ccc4c533884804d094d40b71175409077002595b10b9c3fd56d8f6c11bcacdc99502fdd78f082adb1e59d83090182550436579ec8bc2361714bb659f7a693'
7
- data.tar.gz: d3dfcb8f455e7d9189415cfdf9e00f7f1d938b8ce41d8d307ff2ff4bbb6fcf09449a4a881b0bfec34f8b275556691601f2e6d070351ea2b51d5626e5a561b2fd
6
+ metadata.gz: e293af57162b61ad0fa3472b7d9b35709f13504308e69eba45f40e9babff53e0258186add06ed4a89bc8305b8903dc49718c274659b5fca0712bc26d281ca7d4
7
+ data.tar.gz: 3bb70c552e010149bc32fd4c9dbe416cd6ae0098b4286e5894fbc12c07628b5079c0463ad78b54ab6dc773954e055c9195aea5ac180ab8bdf7dca3847db1bc96
data/.rspec CHANGED
@@ -1,5 +1 @@
1
1
  --color
2
- --format documentation
3
- --fail-fast
4
- --backtrace
5
-
data/lib/onebox/engine.rb CHANGED
@@ -28,23 +28,19 @@ module Onebox
28
28
  end
29
29
  end
30
30
 
31
- attr_reader :url, :uri
32
- attr_reader :timeout
31
+ attr_reader :url, :uri, :options, :timeout
33
32
  attr :errors
34
33
 
35
34
  DEFAULT = {}
36
- def options
37
- @options
38
- end
39
35
 
40
36
  def options=(opt)
41
- return @options if opt.nil? #make sure options provided
42
- opt = opt.to_h if opt.instance_of?(OpenStruct)
37
+ return @options if opt.nil? # make sure options provided
38
+ opt = opt.to_h if opt.instance_of?(OpenStruct)
43
39
  @options.merge!(opt)
44
40
  @options
45
41
  end
46
42
 
47
- def initialize(link, timeout = nil)
43
+ def initialize(url, timeout = nil)
48
44
  @errors = {}
49
45
  @options = DEFAULT
50
46
  class_name = self.class.name.split("::").last.to_s
@@ -52,8 +48,8 @@ module Onebox
52
48
  # Set the engine options extracted from global options.
53
49
  self.options = Onebox.options[class_name] || {}
54
50
 
55
- @url = link
56
- @uri = URI(link)
51
+ @url = url
52
+ @uri = URI(url)
57
53
  if always_https?
58
54
  @uri.scheme = 'https'
59
55
  @url = @uri.to_s
@@ -136,7 +132,7 @@ module Onebox
136
132
  end
137
133
 
138
134
  def always_https?
139
- @https
135
+ defined?(@https) ? @https : false
140
136
  end
141
137
  end
142
138
  end
@@ -27,7 +27,6 @@ module Onebox
27
27
  500px.com
28
28
  8tracks.com
29
29
  abc.net.au
30
- about.com
31
30
  answers.com
32
31
  arstechnica.com
33
32
  ask.com
@@ -36,11 +35,9 @@ module Onebox
36
35
  bbs.boingboing.net
37
36
  bestbuy.ca
38
37
  bestbuy.com
39
- blip.tv
40
38
  bloomberg.com
41
39
  businessinsider.com
42
40
  change.org
43
- clikthrough.com
44
41
  cnet.com
45
42
  cnn.com
46
43
  codepen.io
@@ -90,7 +87,6 @@ module Onebox
90
87
  meetup.com
91
88
  mixcloud.com
92
89
  mlb.com
93
- myshopify.com
94
90
  myspace.com
95
91
  nba.com
96
92
  npr.org
@@ -98,16 +94,13 @@ module Onebox
98
94
  photobucket.com
99
95
  pinterest.com
100
96
  reference.com
101
- revision3.com
102
97
  rottentomatoes.com
103
98
  samsung.com
104
- screenr.com
105
99
  scribd.com
106
100
  slideshare.net
107
101
  sourceforge.net
108
102
  speakerdeck.com
109
103
  spotify.com
110
- squidoo.com
111
104
  streamable.com
112
105
  techcrunch.com
113
106
  ted.com
@@ -124,7 +117,6 @@ module Onebox
124
117
  twitpic.com
125
118
  usatoday.com
126
119
  viddler.com
127
- videojug.com
128
120
  vine.co
129
121
  walmart.com
130
122
  washingtonpost.com
@@ -275,7 +267,6 @@ module Onebox
275
267
 
276
268
  def rewrite_https(html)
277
269
  return unless html
278
- uri = URI(@url)
279
270
  if AllowlistedGenericOnebox.host_matches(uri, AllowlistedGenericOnebox.rewrites)
280
271
  html = html.gsub("http://", "https://")
281
272
  end
@@ -374,15 +365,17 @@ module Onebox
374
365
  escaped_image_src = ::Onebox::Helpers.normalize_url_for_output(data[:image])
375
366
 
376
367
  <<-HTML
377
- <video title='#{data[:title]}'
378
- width='#{data[:video_width]}'
379
- height='#{data[:video_height]}'
380
- style='max-width:100%'
381
- poster='#{escaped_image_src}'
382
- controls=''>
383
- <source src='#{escaped_video_src}'>
384
- </video>
385
- HTML
368
+ <video
369
+ title='#{data[:title]}'
370
+ width='#{data[:video_width]}'
371
+ height='#{data[:video_height]}'
372
+ style='max-width:100%'
373
+ poster='#{escaped_image_src}'
374
+ controls=''
375
+ >
376
+ <source src='#{escaped_video_src}'>
377
+ </video>
378
+ HTML
386
379
  end
387
380
 
388
381
  def embedded_html
@@ -14,13 +14,13 @@ module Onebox
14
14
  matches_regexp(/^https?:\/\/(?:www\.)?(?:smile\.)?(amazon|amzn)\.(?<tld>com|ca|de|it|es|fr|co\.jp|co\.uk|cn|in|com\.br|com\.mx|nl|pl|sa|sg|se|com\.tr|ae)\//)
15
15
 
16
16
  def url
17
+ @raw ||= nil
18
+
17
19
  # If possible, fetch the cached HTML body immediately so we can
18
20
  # try to grab the canonical URL from that document,
19
21
  # rather than guess at the best URL structure to use
20
- if body_cacher&.respond_to?('cache_response_body?')
21
- if body_cacher.cache_response_body?(uri.to_s) && body_cacher.cached_response_body_exists?(uri.to_s)
22
- @raw ||= Onebox::Helpers.fetch_html_doc(@url, http_params, body_cacher)
23
- end
22
+ if !@raw && has_cached_body
23
+ @raw = Onebox::Helpers.fetch_html_doc(@url, http_params, body_cacher)
24
24
  end
25
25
 
26
26
  if @raw
@@ -29,14 +29,15 @@ module Onebox
29
29
  end
30
30
 
31
31
  if match && match[:id]
32
- return "https://www.amazon.#{tld}/dp/#{Onebox::Helpers.uri_encode(match[:id])}"
32
+ id = Addressable::URI.encode_component(match[:id], Addressable::URI::CharacterClasses::PATH)
33
+ return "https://www.amazon.#{tld}/dp/#{id}"
33
34
  end
34
35
 
35
36
  @url
36
37
  end
37
38
 
38
39
  def tld
39
- @tld || @@matcher.match(@url)["tld"]
40
+ @tld ||= @@matcher.match(@url)["tld"]
40
41
  end
41
42
 
42
43
  def http_params
@@ -47,6 +48,12 @@ module Onebox
47
48
 
48
49
  private
49
50
 
51
+ def has_cached_body
52
+ body_cacher&.respond_to?('cache_response_body?') &&
53
+ body_cacher.cache_response_body?(uri.to_s) &&
54
+ body_cacher.cached_response_body_exists?(uri.to_s)
55
+ end
56
+
50
57
  def match
51
58
  @match ||= @url.match(/(?:d|g)p\/(?:product\/|video\/detail\/)?(?<id>[A-Z0-9]+)(?:\/|\?|$)/mi)
52
59
  end
@@ -55,9 +62,9 @@ module Onebox
55
62
  if (main_image = raw.css("#main-image")) && main_image.any?
56
63
  attributes = main_image.first.attributes
57
64
 
58
- return attributes["data-a-hires"].to_s if attributes["data-a-hires"]
59
-
60
- if attributes["data-a-dynamic-image"]
65
+ if attributes["data-a-hires"]
66
+ return attributes["data-a-hires"].to_s
67
+ elsif attributes["data-a-dynamic-image"]
61
68
  return ::JSON.parse(attributes["data-a-dynamic-image"].value).keys.first
62
69
  end
63
70
  end
@@ -65,9 +72,11 @@ module Onebox
65
72
  if (landing_image = raw.css("#landingImage")) && landing_image.any?
66
73
  attributes = landing_image.first.attributes
67
74
 
68
- return attributes["data-old-hires"].to_s if attributes["data-old-hires"]
69
-
70
- landing_image.first["src"].to_s
75
+ if attributes["data-old-hires"]
76
+ return attributes["data-old-hires"].to_s
77
+ else
78
+ return landing_image.first["src"].to_s
79
+ end
71
80
  end
72
81
 
73
82
  if (ebook_image = raw.css("#ebooksImgBlkFront")) && ebook_image.any?
@@ -89,16 +98,16 @@ module Onebox
89
98
  end
90
99
 
91
100
  def multiple_authors(authors_xpath)
92
- author_list = raw.xpath(authors_xpath)
93
- authors = []
94
- author_list.each { |a| authors << a.inner_text.strip }
95
- authors.join(", ")
101
+ raw
102
+ .xpath(authors_xpath)
103
+ .map { |a| a.inner_text.strip }
104
+ .join(", ")
96
105
  end
97
106
 
98
107
  def data
99
108
  og = ::Onebox::OpenGraph.new(raw)
100
109
 
101
- if raw.at_css('#dp.book_mobile') #printed books
110
+ if raw.at_css('#dp.book_mobile') # printed books
102
111
  title = raw.at("h1#title")&.inner_text
103
112
  authors = raw.at_css('#byline_secondary_view_div') ? multiple_authors("//div[@id='byline_secondary_view_div']//span[@class='a-text-bold']") : raw.at("#byline")&.inner_text
104
113
  rating = raw.at("#averageCustomerReviews_feature_div .a-icon")&.inner_text || raw.at("#cmrsArcLink .a-icon")&.inner_text
@@ -7,7 +7,6 @@ module Onebox
7
7
  include StandardEmbed
8
8
 
9
9
  always_https
10
-
11
10
  matches_regexp(/^https?:\/\/asciinema\.org\/a\/[\p{Alnum}_\-]+$/)
12
11
 
13
12
  def to_html
@@ -13,14 +13,17 @@ module Onebox
13
13
  oembed = get_oembed
14
14
 
15
15
  <<-HTML
16
- <img src="#{oembed.thumbnail_url}" style="max-width: #{oembed.width}px; max-height: #{oembed.height}px;" #{oembed.title_attr}>
16
+ <img
17
+ src="#{oembed.thumbnail_url}"
18
+ style="max-width: #{oembed.width}px; max-height: #{oembed.height}px;"
19
+ #{oembed.title_attr}
20
+ >
17
21
  HTML
18
22
  end
19
23
 
20
24
  def to_html
21
25
  get_oembed.html
22
26
  end
23
-
24
27
  end
25
28
  end
26
29
  end
@@ -20,16 +20,16 @@ module Onebox
20
20
  escaped_src = og.video_secure_url || og.video
21
21
 
22
22
  <<-HTML
23
- <iframe src="#{escaped_src}"
24
- width="#{og.video_width}"
25
- height="#{og.video_height}"
26
- scrolling="no"
27
- frameborder="0"
28
- allowfullscreen>
29
- </iframe>
23
+ <iframe
24
+ src="#{escaped_src}"
25
+ width="#{og.video_width}"
26
+ height="#{og.video_height}"
27
+ scrolling="no"
28
+ frameborder="0"
29
+ allowfullscreen
30
+ ></iframe>
30
31
  HTML
31
32
  end
32
-
33
33
  end
34
34
  end
35
35
  end
@@ -25,28 +25,28 @@ module Onebox
25
25
 
26
26
  def link_html(og)
27
27
  <<-HTML
28
- <a href='#{og.url}' target='_blank' rel='noopener'>
29
- #{og.title}
30
- </a>
31
- HTML
28
+ <a href='#{og.url}' target='_blank' rel='noopener'>
29
+ #{og.title}
30
+ </a>
31
+ HTML
32
32
  end
33
33
 
34
34
  def video_html(og)
35
35
  direct_src = ::Onebox::Helpers.normalize_url_for_output("#{og.get(:url)}/#{og.title}")
36
36
 
37
37
  <<-HTML
38
- <video width='480' height='360' #{og.title_attr} controls loop>
39
- <source src='#{direct_src}' type='video/mp4'>
40
- </video>
41
- HTML
38
+ <video width='480' height='360' #{og.title_attr} controls loop>
39
+ <source src='#{direct_src}' type='video/mp4'>
40
+ </video>
41
+ HTML
42
42
  end
43
43
 
44
44
  def image_html(og)
45
45
  <<-HTML
46
- <a href='#{og.url}' target='_blank' class='onebox' rel='noopener'>
47
- <img src='#{og.image}' #{og.title_attr} alt='CloudApp' width='480'>
48
- </a>
49
- HTML
46
+ <a href='#{og.url}' target='_blank' class='onebox' rel='noopener'>
47
+ <img src='#{og.image}' #{og.title_attr} alt='CloudApp' width='480'>
48
+ </a>
49
+ HTML
50
50
  end
51
51
  end
52
52
  end
@@ -17,7 +17,6 @@ module Onebox
17
17
  def to_html
18
18
  get_oembed.html
19
19
  end
20
-
21
20
  end
22
21
  end
23
22
  end
@@ -14,13 +14,14 @@ module Onebox
14
14
  metadata = get_twitter
15
15
  if metadata.present? && metadata[:card] == "player" && metadata[:player].present?
16
16
  <<-HTML
17
- <iframe src="#{metadata[:player]}"
18
- width="#{metadata[:player_width]}"
19
- height="#{metadata[:player_height]}"
20
- scrolling="no"
21
- frameborder="0"
22
- allowfullscreen>
23
- </iframe>
17
+ <iframe
18
+ src="#{metadata[:player]}"
19
+ width="#{metadata[:player_width]}"
20
+ height="#{metadata[:player_height]}"
21
+ scrolling="no"
22
+ frameborder="0"
23
+ allowfullscreen
24
+ ></iframe>
24
25
  HTML
25
26
  else
26
27
  html = Onebox::Engine::AllowlistedGenericOnebox.new(@url, @timeout).to_html
@@ -13,7 +13,6 @@ module Onebox
13
13
  og = get_opengraph
14
14
  "<img src='#{og.image}' width='#{og.image_width}' height='#{og.image_height}' class='onebox' #{og.title_attr}>"
15
15
  end
16
-
17
16
  end
18
17
  end
19
18
  end
@@ -25,29 +25,28 @@ module Onebox
25
25
  album_title = "[Album] #{og.title}"
26
26
 
27
27
  <<-HTML
28
- <div class='onebox flickr-album'>
29
- <a href='#{escaped_url}' target='_blank' rel='noopener'>
30
- <span class='outer-box' style='max-width:#{og.image_width}px'>
31
- <span class='inner-box'>
32
- <span class='album-title'>#{album_title}</span>
33
- </span>
28
+ <div class='onebox flickr-album'>
29
+ <a href='#{escaped_url}' target='_blank' rel='noopener'>
30
+ <span class='outer-box' style='max-width:#{og.image_width}px'>
31
+ <span class='inner-box'>
32
+ <span class='album-title'>#{album_title}</span>
34
33
  </span>
35
- <img src='#{og.get_secure_image}' #{og.title_attr} height='#{og.image_height}' width='#{og.image_width}'>
36
- </a>
37
- </div>
38
- HTML
34
+ </span>
35
+ <img src='#{og.secure_image_url}' #{og.title_attr} height='#{og.image_height}' width='#{og.image_width}'>
36
+ </a>
37
+ </div>
38
+ HTML
39
39
  end
40
40
 
41
41
  def image_html(og)
42
42
  escaped_url = ::Onebox::Helpers.normalize_url_for_output(url)
43
43
 
44
44
  <<-HTML
45
- <a href='#{escaped_url}' target='_blank' rel='noopener' class="onebox">
46
- <img src='#{og.get_secure_image}' #{og.title_attr} alt='Imgur' height='#{og.image_height}' width='#{og.image_width}'>
47
- </a>
48
- HTML
45
+ <a href='#{escaped_url}' target='_blank' rel='noopener' class="onebox">
46
+ <img src='#{og.secure_image_url}' #{og.title_attr} alt='Imgur' height='#{og.image_height}' width='#{og.image_width}'>
47
+ </a>
48
+ HTML
49
49
  end
50
-
51
50
  end
52
51
  end
53
52
  end
@@ -9,8 +9,8 @@ module Onebox
9
9
  matches_regexp(/^https?:\/\/gfycat\.com\//)
10
10
  always_https
11
11
 
12
+ # This engine should have priority over AllowlistedGenericOnebox.
12
13
  def self.priority
13
- # This engine should have priority over AllowlistedGenericOnebox.
14
14
  1
15
15
  end
16
16
 
@@ -21,6 +21,7 @@ module Onebox
21
21
  <img src="https://gfycat.com/static/favicons/favicon-96x96.png" class="site-icon" width="64" height="64">
22
22
  <a href="#{data[:url]}" target="_blank" rel="nofollow ugc noopener">Gfycat.com</a>
23
23
  </header>
24
+
24
25
  <article class="onebox-body">
25
26
  <h4>
26
27
  #{data[:title]} by
@@ -36,11 +37,12 @@ module Onebox
36
37
  <img title="Sorry, your browser doesn't support HTML5 video." src="#{data[:posterUrl]}">
37
38
  </video>
38
39
  </div>
40
+
39
41
  <p>
40
42
  <span class="label1">#{data[:keywords]}</span>
41
43
  </p>
42
-
43
44
  </article>
45
+
44
46
  <div style="clear: both"></div>
45
47
  </aside>
46
48
  HTML
@@ -61,52 +63,50 @@ module Onebox
61
63
  @match ||= @url.match(/^https?:\/\/gfycat\.com\/(gifs\/detail\/)?(?<name>.+)/)
62
64
  end
63
65
 
64
- def nokogiri_page
65
- @nokogiri_page ||= begin
66
- response = Onebox::Helpers.fetch_response(url, redirect_limit: 10) rescue nil
67
- Nokogiri::HTML(response)
68
- end
69
- end
66
+ def og_data
67
+ return @og_data if defined?(@og_data)
70
68
 
71
- def get_og_data
72
- og_data = {}
69
+ response = Onebox::Helpers.fetch_response(url, redirect_limit: 10) rescue nil
70
+ page = Nokogiri::HTML(response)
71
+ script = page.at_css('script[type="application/ld+json"]')
73
72
 
74
- if json_string = nokogiri_page.at_css('script[type="application/ld+json"]')&.text
75
- og_data = Onebox::Helpers.symbolize_keys(::MultiJson.load(json_string))
73
+ if json_string = script&.text
74
+ @og_data = Onebox::Helpers.symbolize_keys(::MultiJson.load(json_string))
75
+ else
76
+ @og_data = {}
76
77
  end
77
-
78
- og_data
79
78
  end
80
79
 
81
80
  def data
82
- og_data = get_og_data
81
+ return @data if defined?(@data)
83
82
 
84
- response = {
83
+ @data = {
85
84
  name: match[:name],
86
85
  title: og_data[:headline] || 'No Title',
87
86
  author: og_data[:author],
88
- url: @url
87
+ url: @url,
89
88
  }
90
89
 
91
- keywords = og_data[:keywords]&.split(',')
92
- if keywords
93
- response[:keywords] = keywords.map { |t| "<a href='https://gfycat.com/gifs/search/#{t}'>##{t}</a>" }.join(' ')
90
+ if keywords = og_data[:keywords]&.split(',')
91
+ @data[:keywords] = keywords
92
+ .map { |keyword| "<a href='https://gfycat.com/gifs/search/#{keyword}'>##{keyword}</a>" }
93
+ .join(' ')
94
94
  end
95
95
 
96
96
  if og_data[:video]
97
97
  content_url = ::Onebox::Helpers.normalize_url_for_output(og_data[:video][:contentUrl])
98
98
  video_url = Pathname.new(content_url)
99
- response[:webmUrl] = video_url.sub_ext(".webm").to_s
100
- response[:mp4Url] = video_url.sub_ext(".mp4").to_s
99
+ @data[:webmUrl] = video_url.sub_ext(".webm").to_s
100
+ @data[:mp4Url] = video_url.sub_ext(".mp4").to_s
101
101
 
102
102
  thumbnail_url = ::Onebox::Helpers.normalize_url_for_output(og_data[:video][:thumbnailUrl])
103
- response[:posterUrl] = thumbnail_url
103
+ @data[:posterUrl] = thumbnail_url
104
104
 
105
- response[:width] = og_data[:video][:width]
106
- response[:height] = og_data[:video][:height]
105
+ @data[:width] = og_data[:video][:width]
106
+ @data[:height] = og_data[:video][:height]
107
107
  end
108
108
 
109
- response
109
+ @data
110
110
  end
111
111
  end
112
112
  end