Class: Html2rss::Item

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml, config) ⇒ Item



12
13
14
15
# File 'lib/html2rss/item.rb', line 12

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
31
32
33
# 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 = 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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#xmlObject (readonly)

Returns the value of attribute xml.



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

def xml
  @xml
end

Class Method Details

.from_url(url, config) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/html2rss/item.rb', line 50

def self.from_url(url, config)
  connection = Faraday.new(url: url, headers: config.headers) { |faraday|
    faraday.use FaradayMiddleware::FollowRedirects
    faraday.adapter Faraday.default_adapter
  }

  page = Nokogiri::HTML(connection.get.body)
  page.css(config.selector('items')).map do |xml_item|
    new xml_item, config
  end
end

Instance Method Details

#available_attributesObject



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

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

#categoriesObject



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

def categories
  config.categories.map(&method(:method_missing)).uniq.keep_if { |category| category.to_s != '' }
end

#respond_to_missing?(method_name, _include_private = false) ⇒ 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



40
41
42
43
44
# File 'lib/html2rss/item.rb', line 40

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

  true
end