article_json 0.2.0 → 0.2.1

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
  SHA1:
3
- metadata.gz: 416c6c5505374ac9303aa393dcfbfb361276fa45
4
- data.tar.gz: 5e22d9856b75f0d16203678d2b00e9faa05831b5
3
+ metadata.gz: 0b9a465102850e508e0122f49d1a07dc2bb2f557
4
+ data.tar.gz: 1a8245e0c0c8e7d1d12cfc0fb3935ca339faf687
5
5
  SHA512:
6
- metadata.gz: 015714d59724701d0fc6edda9a7257315d903055c6d5c3a7c233e4f0780e2f6cb227e2c02c0f30df5999d6d8bc75da643c59bda757858386c55f604f8166b601
7
- data.tar.gz: dd5b53abece174c23c2639ae80ef53f28bd79004249c46dc4aec26e1c0de4fa9bb91512be40625394b3e7c740bd3229e2596fba0655d2837d0cca5e0636db651
6
+ metadata.gz: '0825cb1ba5323f0fc31472af514c1606720e4c3ce22011dd61da46309c6eb9bd3997c9faf32e6cca995c7cc9069caf17c3dd22bc105b10add4040a995d8d1675'
7
+ data.tar.gz: 738b909324f0a12a1a9a27efcbdb58b6e7f496790b2a5f99e153c4424253c2c79be7af14b4824ddf48b3865b1e32934755272edeee5569fba1d31df45b000e18
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.1 - 2017/11/08
4
+ **Fix**: Handle non-successful OEmbed responses by rendering message
5
+
3
6
  ## 0.2.0 - 2017/11/03
4
7
  In this second release we **added support** to:
5
8
  - Export AMP along with required libraries for AMP rendering
@@ -28,11 +28,16 @@ module ArticleJSON
28
28
  end
29
29
 
30
30
  # Obtain the oembed data for this embed element
31
- # @return [Hash]
31
+ # @return [Hash|nil]
32
32
  def oembed_data
33
33
  oembed_resolver&.oembed_data
34
34
  end
35
35
 
36
+ # @return [Array[ArticleJSON::Elements::Text]|nil]
37
+ def oembed_unavailable_message
38
+ oembed_resolver&.unavailable_message
39
+ end
40
+
36
41
  private
37
42
 
38
43
  # @return [ArticleJSON::Utils::OEmbedResolver::Base]
@@ -24,8 +24,17 @@ module ArticleJSON
24
24
  end
25
25
 
26
26
  def embedded_object
27
+ return unavailable_node unless @element.oembed_data
27
28
  Nokogiri::HTML.fragment(@element.oembed_data[:html])
28
29
  end
30
+
31
+ def unavailable_node
32
+ create_element(:span, class: 'unavailable-embed') do |span|
33
+ @element.oembed_unavailable_message.each do |element|
34
+ span.add_child(base_class.build(element).export)
35
+ end
36
+ end
37
+ end
29
38
  end
30
39
  end
31
40
  end
@@ -7,22 +7,53 @@ module ArticleJSON
7
7
  @element = embed_element
8
8
  end
9
9
 
10
- # @return [Hash]
10
+ # Requests the OEmbed endpoint of the respective service and returns its
11
+ # data as a Hash. If the endpoint returns an error, `nil` will be
12
+ # returned.
13
+ # @return [Hash|nil]
11
14
  def oembed_data
12
15
  resolver = self.class == Base ? self.class.build(@element) : self
13
16
  resolver.parsed_api_response
14
17
  end
15
18
 
19
+ # In case that there was an error with requesting the OEmbed data from
20
+ # the endpoint (e.g. because the URL is unavailable), this message can
21
+ # be rendered to let the user know about the issue
22
+ # @return [Array[ArticleJSON::Elements::Text]|nil]
23
+ def unavailable_message
24
+ [
25
+ ArticleJSON::Elements::Text.new(content: "The #{name} "),
26
+ ArticleJSON::Elements::Text.new(content: source_url,
27
+ href: source_url),
28
+ ArticleJSON::Elements::Text.new(content: ' is not available.'),
29
+ ]
30
+ end
31
+
32
+ def name
33
+ raise NotImplementedError,
34
+ '`#name` needs to be implemented by the subclass'
35
+ end
36
+
37
+ def source_url
38
+ raise NotImplementedError,
39
+ '`#source_url` needs to be implemented by the subclass'
40
+ end
41
+
16
42
  protected
17
43
 
18
- # @return [Hash]
44
+ # @return [Hash|nil]
19
45
  def parsed_api_response
20
- @api_response ||= begin
46
+ return @api_response if defined? @api_response
47
+ @api_response = begin
21
48
  uri = URI.parse(oembed_url)
22
49
  http = Net::HTTP.new(uri.host, uri.port)
23
50
  http.use_ssl = (uri.scheme == 'https')
24
51
  response = http.request(Net::HTTP::Get.new(uri, http_headers))
25
- JSON.parse(response.body, symbolize_names: true)
52
+ if response.kind_of? Net::HTTPSuccess
53
+ JSON.parse(response.body, symbolize_names: true)
54
+ end
55
+ rescue Net::ProtocolError, JSON::ParserError
56
+ nil
26
57
  end
27
58
  end
28
59
 
@@ -2,17 +2,21 @@ module ArticleJSON
2
2
  module Utils
3
3
  module OEmbedResolver
4
4
  class FacebookVideo < Base
5
+ # Human readable name of the resolver
6
+ # @return [String]
7
+ def name
8
+ 'Facebook video'
9
+ end
10
+
5
11
  # The URL for the oembed API call
6
12
  # @return [String]
7
13
  def oembed_url
8
- "https://www.facebook.com/plugins/video/oembed.json?url=#{video_url}"
14
+ "https://www.facebook.com/plugins/video/oembed.json?url=#{source_url}"
9
15
  end
10
16
 
11
- private
12
-
13
17
  # The video URL of the element
14
18
  # @return [String]
15
- def video_url
19
+ def source_url
16
20
  "https://www.facebook.com/facebook/videos/#{@element.embed_id}"
17
21
  end
18
22
  end
@@ -2,17 +2,22 @@ module ArticleJSON
2
2
  module Utils
3
3
  module OEmbedResolver
4
4
  class Slideshare < Base
5
+ # Human readable name of the resolver
6
+ # @return [String]
7
+ def name
8
+ 'Slideshare deck'
9
+ end
10
+
5
11
  # The URL for the oembed API call
6
12
  # @return [String]
7
13
  def oembed_url
8
- "https://www.slideshare.net/api/oembed/2?format=json&url=#{slide_url}"
14
+ 'https://www.slideshare.net/api/oembed/2?format=json&url='\
15
+ "#{source_url}"
9
16
  end
10
17
 
11
- private
12
-
13
18
  # The URL of the slideshow
14
19
  # @return [String]
15
- def slide_url
20
+ def source_url
16
21
  handle, slug = @element.embed_id.split('/', 2)
17
22
  "https://www.slideshare.net/#{handle}/#{slug}"
18
23
  end
@@ -2,18 +2,22 @@ module ArticleJSON
2
2
  module Utils
3
3
  module OEmbedResolver
4
4
  class Tweet < Base
5
+ # Human readable name of the resolver
6
+ # @return [String]
7
+ def name
8
+ 'Tweet'
9
+ end
10
+
5
11
  # The URL for the oembed API call
6
12
  # @return [String]
7
13
  def oembed_url
8
14
  'https://api.twitter.com/1/statuses/oembed.json?align=center' \
9
- "&url=#{tweet_url}"
15
+ "&url=#{source_url}"
10
16
  end
11
17
 
12
- private
13
-
14
18
  # The URL of the tweet
15
19
  # @return [String]
16
- def tweet_url
20
+ def source_url
17
21
  handle, tweet_id = @element.embed_id.split('/', 2)
18
22
  "https://twitter.com/#{handle}/status/#{tweet_id}"
19
23
  end
@@ -2,17 +2,21 @@ module ArticleJSON
2
2
  module Utils
3
3
  module OEmbedResolver
4
4
  class VimeoVideo < Base
5
+ # Human readable name of the resolver
6
+ # @return [String]
7
+ def name
8
+ 'Vimeo video'
9
+ end
10
+
5
11
  # The URL for the oembed API call
6
12
  # @return [String]
7
13
  def oembed_url
8
- "https://vimeo.com/api/oembed.json?url=#{video_url}"
14
+ "https://vimeo.com/api/oembed.json?url=#{source_url}"
9
15
  end
10
16
 
11
- private
12
-
13
17
  # The video URL of the element
14
18
  # @return [String]
15
- def video_url
19
+ def source_url
16
20
  "https://vimeo.com/#{@element.embed_id}"
17
21
  end
18
22
  end
@@ -2,17 +2,21 @@ module ArticleJSON
2
2
  module Utils
3
3
  module OEmbedResolver
4
4
  class YoutubeVideo < Base
5
+ # Human readable name of the resolver
6
+ # @return [String]
7
+ def name
8
+ 'Youtube video'
9
+ end
10
+
5
11
  # The URL for the oembed API call
6
12
  # @return [String]
7
13
  def oembed_url
8
- "http://www.youtube.com/oembed?format=json&url=#{video_url}"
14
+ "http://www.youtube.com/oembed?format=json&url=#{source_url}"
9
15
  end
10
16
 
11
- private
12
-
13
17
  # The video URL of the element
14
18
  # @return [String]
15
- def video_url
19
+ def source_url
16
20
  "https://www.youtube.com/watch?v=#{@element.embed_id}"
17
21
  end
18
22
  end
@@ -1,3 +1,3 @@
1
1
  module ArticleJSON
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: article_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Sager
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-11-03 00:00:00.000000000 Z
13
+ date: 2017-11-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: nokogiri