class ArticleJSON::Utils::OEmbedResolver::Base
Public Class Methods
new(embed_element)
click to toggle source
@param [ArticleJSON::Elements::Embed] embed_element
# File lib/article_json/utils/o_embed_resolver/base.rb, line 6 def initialize(embed_element) @element = embed_element end
Protected Class Methods
build(embed_element)
click to toggle source
Instantiate the correct sub class for a given element @param [ArticleJSON::Elements::Embed] embed_element @return [ArticleJSON::Utils::OEmbedResolver::Base]
# 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)
click to toggle source
Lookup the correct sub class for a given element type @param [Symbol] :type @return [ArticleJSON::Utils::OEmbedResolver::Base]
# 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
Public Instance Methods
name()
click to toggle source
# 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_data()
click to toggle source
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. @return [Hash|nil]
# 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_url()
click to toggle source
# 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
Protected Instance Methods
http_headers()
click to toggle source
@return [Hash]
# File lib/article_json/utils/o_embed_resolver/base.rb, line 61 def http_headers headers = { 'Content-Type' => 'application/json' } unless ArticleJSON.configuration.oembed_user_agent.nil? headers['User-Agent'] = ArticleJSON.configuration.oembed_user_agent end headers end
parsed_api_response()
click to toggle source
@return [Hash|nil]
# File lib/article_json/utils/o_embed_resolver/base.rb, line 45 def parsed_api_response return @api_response if defined? @api_response @api_response = begin uri = URI.parse(oembed_url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == 'https') response = http.request(Net::HTTP::Get.new(uri, http_headers)) if response.kind_of? Net::HTTPSuccess JSON.parse(response.body, symbolize_names: true) end rescue Net::ProtocolError, JSON::ParserError nil end end