class Html2rss::Item

Takes the selected Nokogiri::HTML and responds to accessors names defined in the feed config.

Attributes

config[R]
xml[R]

Public Class Methods

from_url(url, config) click to toggle source

@return [Array]

# File lib/html2rss/item.rb, line 63
def self.from_url(url, config)
  body = get_body_from_url(url, config)

  Nokogiri.HTML(body).css(config.selector(:items))
          .map { |xml_item| new xml_item, config }
          .keep_if(&:valid?)
end

Private Class Methods

get_body_from_url(url, config) click to toggle source
# File lib/html2rss/item.rb, line 73
def self.get_body_from_url(url, config)
  request = Faraday.new(url: url, headers: config.headers) do |faraday|
    faraday.use FaradayMiddleware::FollowRedirects
    faraday.adapter Faraday.default_adapter
  end

  body = request.get.body

  config.json? ? Html2rss::Utils.object_to_xml(JSON.parse(body)) : body
end
new(xml, config) click to toggle source
# File lib/html2rss/item.rb, line 10
def initialize(xml, config)
  @xml = xml
  @config = config
end

Public Instance Methods

available_attributes() click to toggle source
# File lib/html2rss/item.rb, line 32
def available_attributes
  @available_attributes ||= (%i[title link description author comments updated] &
    @config.attribute_names) - %i[categories enclosure]
end
categories() click to toggle source

@return [Array]

# File lib/html2rss/item.rb, line 47
def categories
  config.category_selectors.map(&method(:method_missing))
end
enclosure?() click to toggle source
# File lib/html2rss/item.rb, line 51
def enclosure?
  config.attribute?(:enclosure)
end
enclosure_url() click to toggle source
# File lib/html2rss/item.rb, line 55
def enclosure_url
  enclosure = Html2rss::Utils.sanitize_url(method_missing(:enclosure))

  Html2rss::Utils.build_absolute_url_from_relative(enclosure, config.url).to_s if enclosure
end
method_missing(method_name, *_args) click to toggle source
Calls superclass method
# File lib/html2rss/item.rb, line 21
def method_missing(method_name, *_args)
  return super unless respond_to_missing?(method_name)

  attribute_options = config.attribute_options(method_name)

  extractor = ItemExtractors.get_extractor(attribute_options[:extractor])
  value = extractor.new(xml, attribute_options).get

  post_process(value, attribute_options.fetch(:post_process, false))
end
respond_to_missing?(method_name, _include_private = false) click to toggle source
Calls superclass method
# File lib/html2rss/item.rb, line 17
def respond_to_missing?(method_name, _include_private = false)
  config.attribute?(method_name) || super
end
valid?() click to toggle source

At least a title or a description is required to be a valid RSS 2.0 item.

# File lib/html2rss/item.rb, line 39
def valid?
  title = self.title if config.attribute?(:title)
  description = self.description if config.attribute?(:description)
  [title, description].join != ''
end

Private Instance Methods

post_process(value, post_process_options) click to toggle source
# File lib/html2rss/item.rb, line 87
def post_process(value, post_process_options)
  return value unless post_process_options

  [post_process_options].flatten.each do |options|
    value = AttributePostProcessors.get_processor(options[:name])
                                   .new(value, options: options, item: self, config: @config)
                                   .get
  end

  value
end