Class: ArticleJSON::Utils::OEmbedResolver::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/article_json/utils/o_embed_resolver/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(embed_element) ⇒ Base

Returns a new instance of Base.

Parameters:



6
7
8
# File 'lib/article_json/utils/o_embed_resolver/base.rb', line 6

def initialize(embed_element)
  @element = embed_element
end

Class Method Details

.build(embed_element) ⇒ ArticleJSON::Utils::OEmbedResolver::Base

Instantiate the correct sub class for a given element



73
74
75
76
# File 'lib/article_json/utils/o_embed_resolver/base.rb', line 73

def build(embed_element)
  resolver = resolver_by_embed_type(embed_element.embed_type)
  resolver.new(embed_element) unless resolver.nil?
end

.resolver_by_embed_type(type) ⇒ ArticleJSON::Utils::OEmbedResolver::Base

Lookup the correct sub class for a given element type

Parameters:

  • :type (Symbol)

Returns:



81
82
83
84
85
86
87
88
89
90
# File 'lib/article_json/utils/o_embed_resolver/base.rb', line 81

def resolver_by_embed_type(type)
  {
    facebook_video: FacebookVideo,
    slideshare: Slideshare,
    tweet: Tweet,
    vimeo_video: VimeoVideo,
    youtube_video: YoutubeVideo,
    soundcloud: Soundcloud,
  }[type.to_sym]
end

Instance Method Details

#nameObject

Raises:

  • (NotImplementedError)


32
33
34
35
# File 'lib/article_json/utils/o_embed_resolver/base.rb', line 32

def name
  raise NotImplementedError,
        '`#name` needs to be implemented by the subclass'
end

#oembed_dataHash|nil

Requests the OEmbed endpoint of the respective service and returns its data as a Hash. If the endpoint returns an error, ‘nil` will be returned.

Returns:

  • (Hash|nil)


14
15
16
17
# File 'lib/article_json/utils/o_embed_resolver/base.rb', line 14

def oembed_data
  resolver = self.class == Base ? self.class.build(@element) : self
  resolver.parsed_api_response
end

#source_urlObject

Raises:

  • (NotImplementedError)


37
38
39
40
# File 'lib/article_json/utils/o_embed_resolver/base.rb', line 37

def source_url
  raise NotImplementedError,
        '`#source_url` needs to be implemented by the subclass'
end

#unavailable_messageArray[ArticleJSON::Elements::Text]|nil

In case that there was an error with requesting the OEmbed data from the endpoint (e.g. because the URL is unavailable), this message can be rendered to let the user know about the issue

Returns:



23
24
25
26
27
28
29
30
# File 'lib/article_json/utils/o_embed_resolver/base.rb', line 23

def unavailable_message
  [
    ArticleJSON::Elements::Text.new(content: "The #{name} "),
    ArticleJSON::Elements::Text.new(content: source_url,
                                    href: source_url),
    ArticleJSON::Elements::Text.new(content: ' is not available.'),
  ]
end