Sha256: ccd8114eb46e928146dd1bb259c13a2d6e0dfa5b22b4a1b08c9b174c0f4cda4a

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

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

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(method_name, value)
    end

    def post_process(method_name, value)
      case method_name
      when :link
        URI(value)
      when :updated
        Time.parse(value).to_s
      else
        value
      end
    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 { |xml_item|
        new xml_item, config
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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