class ArticleJSON::Import::GoogleDoc::HTML::EmbeddedParser
Public Class Methods
Build a embedded element based on the node's content @param [Nokogiri::HTML::Node] node @param [Nokogiri::HTML::Node] caption_node @param [ArticleJSON::Import::GoogleDoc::HTML::CSSAnalyzer] css_analyzer @return [ArticleJSON::Elements::Embed]
# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 69 def build(node:, caption_node:, css_analyzer:) find_parser(node.inner_text) &.new( node: node, caption_node: caption_node, css_analyzer: css_analyzer ) &.element end
Check if a given string is a Youtube embedding @param [String] text @return [Boolean]
# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 53 def matches?(text) !!(url_regexp =~ text) end
@param [Nokogiri::HTML::Node] node @param [Nokogiri::HTML::Node] caption_node @param [ArticleJSON::Import::GoogleDoc::HTML::CSSAnalyzer] css_analyzer
# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 11 def initialize(node:, caption_node:, css_analyzer:) @node = node @caption_node = caption_node @css_analyzer = css_analyzer end
Check if a node contains a supported embedded element @param [Nokogiri::HTML::Node] node @return [Boolean]
# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 82 def supported?(node) !find_parser(node.inner_text).nil? end
Regular expression to check if node content is embeddable element Is also used to extract the ID from the URL. @return [Regexp]
# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 60 def url_regexp raise NotImplementedError end
Private Class Methods
Find the first matching class for a given (URL) string @param [String] text @return [Class]
# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 104 def find_parser(text) text = text.strip.downcase return nil if text.empty? parsers.find { |klass| klass.matches?(text) } end
List of embedded element classes @return [ArticleJSON::Import::GoogleDoc::HTML::EmbeddedParser]
# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 90 def parsers [ EmbeddedFacebookVideoParser, EmbeddedVimeoVideoParser, EmbeddedYoutubeVideoParser, EmbeddedTweetParser, EmbeddedSlideshareParser, EmbeddedSoundcloudParser, ] end
Public Instance Methods
The embedded element @return [ArticleJSON::Elements::Embed]
# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 40 def element ArticleJSON::Elements::Embed.new( embed_type: embed_type, embed_id: embed_id, tags: tags, caption: caption ) end
Extract the video ID from an URL @return [String]
# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 19 def embed_id match = @node.inner_text.strip.match(self.class.url_regexp) match[:id] if match end
The type of this embedded element To be implemented by sub classes! @return [Symbol]
# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 27 def embed_type raise NotImplementedError end