Class: Html2rss::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/item.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml, config) ⇒ Item

Returns a new instance of Item.



10
11
12
13
# File 'lib/html2rss/item.rb', line 10

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# 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

Class Method Details

.from_url(url, config) ⇒ Array

Returns:

  • (Array)


63
64
65
66
67
68
69
# 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

Instance Method Details

#available_attributesObject



32
33
34
35
# 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

#categoriesArray

Returns:

  • (Array)


47
48
49
# File 'lib/html2rss/item.rb', line 47

def categories
  config.category_selectors.map(&method(:method_missing))
end

#enclosure?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/html2rss/item.rb', line 51

def enclosure?
  config.attribute?(:enclosure)
end

#enclosure_urlObject



55
56
57
58
59
# 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

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/html2rss/item.rb', line 17

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

#valid?Boolean

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

Returns:

  • (Boolean)


39
40
41
42
43
# 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