Sha256: 3be56e213f9c3523f3088d7322708d63fa9b9126a1ddd58739ee52a035332196

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

require 'faraday'
require 'open-uri'
require 'nokogiri'
require_relative 'item_extractor'
require_relative 'attribute_post_processors'

module Html2rss
  class Item
    attr_reader :xml, :config

    def initialize(xml, config)
      @xml = xml
      @config = config
    end

    def respond_to_missing?(method_name, _include_private = false)
      config.attribute_names.include?(method_name) || super
    end

    def method_missing(method_name, *_args)
      attribute_config = config.options(method_name.to_s)
      return super unless attribute_config

      extractor = attribute_config['extractor'] || 'text'
      proc = ItemExtractor.const_get extractor.upcase.to_sym
      value = proc.call(xml, attribute_config)

      post_process_options = attribute_config.fetch('post_process', false)
      value = post_process(value, post_process_options) if post_process_options

      value
    end

    def available_attributes
      # TODO: support optional attributes, e.g. category, enclosure, source
      @available_attributes ||= (%w[title link description author comments updated] & @config.attribute_names)
    end

    def valid?
      return false if [title.to_s, description.to_s].join('') == ''

      true
    end

    def self.from_url(url, config)
      connection = Faraday.new(url: url, headers: config.headers)
      page = Nokogiri::HTML(connection.get.body)
      page.css(config.selector('items')).map do |xml_item|
        new xml_item, config
      end
    end

    private

    def post_process(value, options)
      Html2rss::AttributePostProcessors.get_processor(options)
                                       .new(value, options, self)
                                       .get
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
html2rss-0.1.0 lib/html2rss/item.rb