onebox 2.1.5 → 2.2.0

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: 372344282cacdb38dfceab5dfb6c156d7c9fa5ec04cca9cc21eb8502987ce077
4
- data.tar.gz: 02a26147e10f54261596572328cf85a758178cda845a20c9999cc1b5f96ffe18
3
+ metadata.gz: 354e358f2909b92f1efa00092d3c82cc451760d9f8a0ee6628bb8b950f45ca2a
4
+ data.tar.gz: 86bcb44cc263203075a8b0e972b7899a6942fb4b0e4fad4917089ecbbed2fd32
5
5
  SHA512:
6
- metadata.gz: 187c7a40761cea4d784175373600c00c2045d29ba911c8838614bf51b76454ecd7080ee93ca787d6745e69fc9d823b6de0e592746a2ccc30e9a9d55847e86cd4
7
- data.tar.gz: f73f392c9451e85e5678dd0eff146092bfc0f3ae6a261eb4dfae02fb6a614976571837f3ec52e512334a4e815c3e3c8f49694179a7a41ec387d7195153cb7810
6
+ metadata.gz: 254b44103190d101795ccce8a736dacc7e3e5d3159b0512b7539f39e4968dd750800455c9867abbafd8387ef1ae870974c581965d7b53e699187af532dda1961
7
+ data.tar.gz: eff1b7f8c6add5760f5f7e6be3c1790a170e92a08ad6eb289b5a240702cfd40d681c2d3d437722dad20efe18a7a23ea154e67b60623f7f1e4a5aa651b00126e4
@@ -30,6 +30,7 @@ module Onebox
30
30
 
31
31
  attr_reader :url, :uri
32
32
  attr_reader :timeout
33
+ attr :errors
33
34
 
34
35
  DEFAULT = {}
35
36
  def options
@@ -44,6 +45,7 @@ module Onebox
44
45
  end
45
46
 
46
47
  def initialize(link, timeout = nil)
48
+ @errors = {}
47
49
  @options = DEFAULT
48
50
  class_name = self.class.name.split("::").last.to_s
49
51
 
@@ -150,6 +152,7 @@ require_relative "engine/amazon_onebox"
150
152
  require_relative "engine/github_issue_onebox"
151
153
  require_relative "engine/github_blob_onebox"
152
154
  require_relative "engine/github_commit_onebox"
155
+ require_relative "engine/github_folder_onebox"
153
156
  require_relative "engine/github_gist_onebox"
154
157
  require_relative "engine/github_pullrequest_onebox"
155
158
  require_relative "engine/google_calendar_onebox"
@@ -256,6 +256,15 @@ module Onebox
256
256
  d[:data_1] = Onebox::Helpers.truncate("#{d[:price_currency].strip} #{d[:price_amount].strip}")
257
257
  end
258
258
 
259
+ skip_missing_tags = [:video]
260
+ d.each do |k, v|
261
+ next if skip_missing_tags.include?(k)
262
+ if v == nil || v == ''
263
+ errors[k] ||= []
264
+ errors[k] << 'is blank'
265
+ end
266
+ end
267
+
259
268
  d
260
269
  end
261
270
  end
@@ -292,7 +301,8 @@ module Onebox
292
301
  end
293
302
 
294
303
  def has_text?
295
- !Onebox::Helpers.blank?(data[:title])
304
+ !Onebox::Helpers.blank?(data[:title]) &&
305
+ !Onebox::Helpers.blank?(data[:description])
296
306
  end
297
307
 
298
308
  def is_image?
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Onebox
4
+ module Engine
5
+ class GithubFolderOnebox
6
+ include Engine
7
+ include StandardEmbed
8
+ include LayoutSupport
9
+
10
+ matches_regexp Regexp.new(/^https?:\/\/(?:www\.)?(?:(?:\w)+\.)?(github)\.com[\:\d]*(\/[^\/]+){2}/)
11
+ always_https
12
+
13
+ def self.priority
14
+ # This engine should have lower priority than the other Github engines
15
+ 150
16
+ end
17
+
18
+ private
19
+
20
+ def data
21
+ og = get_opengraph
22
+
23
+ max_length = 250
24
+
25
+ display_path = extract_path(og.url, max_length)
26
+ display_description = clean_description(og.description, og.title, max_length)
27
+
28
+ title = og.title
29
+
30
+ fragment = Addressable::URI.parse(url).fragment
31
+ if fragment
32
+ fragment = Addressable::URI.unencode(fragment)
33
+
34
+ if html_doc.css('.Box.md')
35
+ # For links to markdown docs
36
+ node = html_doc.css('a.anchor').find { |n| n['href'] == "##{fragment}" }
37
+ subtitle = node&.parent&.text
38
+ elsif html_doc.css('.Box.rdoc')
39
+ # For links to rdoc docs
40
+ node = html_doc.css('h3').find { |n| n['id'] == "user-content-#{fragment.downcase}" }
41
+ subtitle = node&.css('text()')&.first&.text
42
+ end
43
+
44
+ title = "#{title} - #{subtitle}" if subtitle
45
+ end
46
+
47
+ {
48
+ link: url,
49
+ image: og.image,
50
+ title: Onebox::Helpers.truncate(title, 250),
51
+ path: display_path,
52
+ description: display_description,
53
+ favicon: get_favicon
54
+ }
55
+ end
56
+
57
+ def extract_path(root, max_length)
58
+ path = url.split('#')[0].split('?')[0]
59
+ path = path["#{root}/tree/".length..-1]
60
+
61
+ return unless path
62
+
63
+ path.length > max_length ? path[-max_length..-1] : path
64
+ end
65
+
66
+ def clean_description(description, title, max_length)
67
+ return unless description
68
+
69
+ desc_end = " - #{title}"
70
+ if description[-desc_end.length..-1] == desc_end
71
+ description = description[0...-desc_end.length]
72
+ end
73
+
74
+ Onebox::Helpers.truncate(description, max_length)
75
+ end
76
+ end
77
+ end
78
+ end
@@ -15,35 +15,32 @@ module Onebox
15
15
  end
16
16
 
17
17
  def data
18
- og = get_opengraph
18
+ oembed = get_oembed
19
+ raise "No oEmbed data found. Ensure 'facebook_app_access_token' is valid" if oembed.data.empty?
19
20
 
20
- # There are at least two different versions of the description. e.g.
21
- # - "3,227 Likes, 88 Comments - An Account (@user.name) on Instagram: “Look at my picture!”"
22
- # - "@user.name posted on their Instagram profile: “Look at my picture!”"
23
-
24
- m = og.description.match(/\(@([\w\.]+)\) on Instagram/)
25
- author_name = m[1] if m
26
-
27
- author_name ||= begin
28
- m = og.description.match(/^\@([\w\.]+)\ posted/)
29
- m[1] if m
30
- end
31
-
32
- raise "Author username not found for post #{clean_url}" unless author_name
33
-
34
- permalink = clean_url.gsub("/#{author_name}/", "/")
21
+ permalink = clean_url.gsub("/#{oembed.author_name}/", "/")
35
22
 
36
23
  { link: permalink,
37
- title: "@#{author_name}",
38
- image: og.image,
39
- description: Onebox::Helpers.truncate(og.title, 250)
24
+ title: "@#{oembed.author_name}",
25
+ image: oembed.thumbnail_url,
26
+ description: Onebox::Helpers.truncate(oembed.title, 250),
40
27
  }
28
+
41
29
  end
42
30
 
43
31
  protected
44
32
 
33
+ def access_token
34
+ (options[:facebook_app_access_token] || Onebox.options.facebook_app_access_token).to_s
35
+ end
36
+
45
37
  def get_oembed_url
46
- oembed_url = "https://api.instagram.com/oembed/?url=#{clean_url}"
38
+ if access_token != ''
39
+ oembed_url = "https://graph.facebook.com/v9.0/instagram_oembed?url=#{clean_url}&access_token=#{access_token}"
40
+ else
41
+ # The following is officially deprecated by Instagram, but works in some limited circumstances.
42
+ oembed_url = "https://api.instagram.com/oembed/?url=#{clean_url}"
43
+ end
47
44
  end
48
45
  end
49
46
  end
@@ -91,7 +91,7 @@ module Onebox
91
91
  html_doc.css('meta').each do |m|
92
92
  if (m["property"] && m["property"][/^twitter:(.+)$/i]) || (m["name"] && m["name"][/^twitter:(.+)$/i])
93
93
  value = (m["content"] || m["value"]).to_s
94
- twitter[$1.tr('-:' , '_').to_sym] ||= value unless Onebox::Helpers::blank?(value)
94
+ twitter[$1.tr('-:' , '_').to_sym] ||= value unless (Onebox::Helpers::blank?(value) || value == "0 minutes")
95
95
  end
96
96
  end
97
97
 
@@ -27,7 +27,7 @@ module Onebox
27
27
  def self.fetch_html_doc(url, headers = nil)
28
28
  response = (fetch_response(url, nil, nil, headers) rescue nil)
29
29
  doc = Nokogiri::HTML(response)
30
- uri = URI(url)
30
+ uri = Addressable::URI.parse(url)
31
31
 
32
32
  ignore_canonical_tag = doc.at('meta[property="og:ignore_canonical"]')
33
33
  should_ignore_canonical = IGNORE_CANONICAL_DOMAINS.map { |hostname| uri.hostname.match?(hostname) }.any?
@@ -35,8 +35,9 @@ module Onebox
35
35
  unless (ignore_canonical_tag && ignore_canonical_tag['content'].to_s == 'true') || should_ignore_canonical
36
36
  # prefer canonical link
37
37
  canonical_link = doc.at('//link[@rel="canonical"]/@href')
38
- if canonical_link && "#{URI(canonical_link).host}#{URI(canonical_link).path}" != "#{uri.host}#{uri.path}"
39
- response = (fetch_response(canonical_link, nil, nil, headers) rescue nil)
38
+ canonical_uri = Addressable::URI.parse(canonical_link)
39
+ if canonical_link && "#{canonical_uri.host}#{canonical_uri.path}" != "#{uri.host}#{uri.path}"
40
+ response = (fetch_response(canonical_uri.to_s, nil, nil, headers) rescue nil)
40
41
  doc = Nokogiri::HTML(response) if response
41
42
  end
42
43
  end
@@ -156,6 +157,7 @@ module Onebox
156
157
  end
157
158
 
158
159
  def self.truncate(string, length = 50)
160
+ return string if string.nil?
159
161
  string.size > length ? string[0...(string.rindex(" ", length) || length)] + "..." : string
160
162
  end
161
163
 
@@ -31,6 +31,16 @@ module Onebox
31
31
  ""
32
32
  end
33
33
 
34
+ def errors
35
+ return {} unless engine
36
+ engine.errors
37
+ end
38
+
39
+ def data
40
+ return {} unless engine
41
+ engine.data
42
+ end
43
+
34
44
  def options
35
45
  OpenStruct.new(@options)
36
46
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Onebox
4
- VERSION = "2.1.5"
4
+ VERSION = "2.2.0"
5
5
  end
@@ -0,0 +1,11 @@
1
+ {{#image}}<img src="{{image}}" class="thumbnail"/>{{/image}}
2
+
3
+ <h3><a href='{{link}}' target="_blank" rel="noopener">{{title}}</a></h3>
4
+
5
+ {{#path}}
6
+ <p><a href='{{link}}' target="_blank" rel="noopener">{{path}}</a></p>
7
+ {{/path}}
8
+
9
+ {{#description}}
10
+ <p><span class="label1">{{description}}</span></p>
11
+ {{/description}}
@@ -8,4 +8,6 @@
8
8
  </div>
9
9
  {{/image}}
10
10
 
11
- <div class="instagram-description">{{description}}</div>
11
+ {{#description}}
12
+ <div class="instagram-description">{{description}}</div>
13
+ {{/description}}
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: 2.1.5
4
+ version: 2.2.0
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: 2020-11-06 00:00:00.000000000 Z
13
+ date: 2020-12-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: addressable
@@ -316,6 +316,7 @@ files:
316
316
  - lib/onebox/engine/giphy_onebox.rb
317
317
  - lib/onebox/engine/github_blob_onebox.rb
318
318
  - lib/onebox/engine/github_commit_onebox.rb
319
+ - lib/onebox/engine/github_folder_onebox.rb
319
320
  - lib/onebox/engine/github_gist_onebox.rb
320
321
  - lib/onebox/engine/github_issue_onebox.rb
321
322
  - lib/onebox/engine/github_pullrequest_onebox.rb
@@ -383,6 +384,7 @@ files:
383
384
  - templates/amazon.mustache
384
385
  - templates/githubblob.mustache
385
386
  - templates/githubcommit.mustache
387
+ - templates/githubfolder.mustache
386
388
  - templates/githubgist.mustache
387
389
  - templates/githubissue.mustache
388
390
  - templates/githubpullrequest.mustache