Sha256: 504dabf83e200e49c4759ffdeec4ac9d2a4b14dd3a35371d89fbd47463db06d8

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module Europeana
  module Feeds
    ##
    # Models an image extracted from a remote RSS feed
    class FeedEntryImage
      # Methods of `[Feedjira::Parser::RSSEntry]` feed entry to inspect for image
      # URLs
      ENTRY_ELEMENTS = %i(summary content).freeze

      # HTML tag and attribute pairs to inspect for image URLs
      TAGS_ATTRS = [
        { tag: :img, attr: :src },
        { tag: :video, attr: :poster }
      ].freeze

      # @param feed_entry [Feedjira::Parser::RSSEntry]
      def initialize(feed_entry)
        @feed_entry = feed_entry
      end

      def url
        @url ||= find_url_in_feed_entry
      end

      protected

      def find_url_in_feed_entry
        # first check for attached images
        return @feed_entry.image if @feed_entry.image

        TAGS_ATTRS.each do |tag_attr|
          url = first_url_attr(tag_attr[:tag], tag_attr[:attr])
          return url unless url.nil?
        end
        nil
      end

      def element_html(element_name)
        Nokogiri::HTML(@feed_entry.send(element_name))
      end

      def first_tag_in_element(element_name, tag_name)
        element_html(element_name).css(tag_name).first
      end

      def first_attr_on_element(element_name, tag_name, attr_name)
        first_tag = first_tag_in_element(element_name, tag_name)
        first_tag.nil? ? nil : first_tag[attr_name]
      end

      def first_url_attr_on_element(element_name, tag_name, attr_name)
        attr_value = first_attr_on_element(element_name, tag_name, attr_name)
        attr_value.present? && attr_value =~ %r{\Ahttps?://} ? attr_value : nil
      end

      def first_url_attr(tag_name, attr_name)
        ENTRY_ELEMENTS.each do |element_name|
          url_attr = first_url_attr_on_element(element_name, tag_name, attr_name)
          return url_attr unless url_attr.nil?
        end
        nil
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
europeana-feeds-0.0.2 app/models/europeana/feeds/feed_entry_image.rb
europeana-feeds-0.0.1 app/models/europeana/feeds/feed_entry_image.rb