Class: Html2rss::Item

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

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
# File 'lib/html2rss/item.rb', line 21

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

  extractor = ItemExtractors.get_extractor(attribute_config['extractor'])
  value = extractor.new(xml, attribute_config).get

  post_process(value, attribute_config.fetch('post_process', false))
end

Class Method Details

.from_url(url, config) ⇒ Array

Returns:

  • (Array)


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

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

Instance Method Details

#available_attributesObject



31
32
33
34
# File 'lib/html2rss/item.rb', line 31

def available_attributes
  @available_attributes ||= (%w[title link description author comments updated] &
    @config.attribute_names) - ['categories']
end

#categoriesArray

Returns:

  • (Array)


42
43
44
45
46
47
# File 'lib/html2rss/item.rb', line 42

def categories
  categories = config.categories
  categories.map!(&method(:method_missing))
  categories.uniq!
  categories.keep_if { |category| category.to_s != '' }
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_names.include?(method_name) || super
end

#valid?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/html2rss/item.rb', line 36

def valid?
  [title.to_s, description.to_s].join('') != ''
end