Module: Rss::Motor

Defined in:
lib/rss-motor.rb,
lib/rss-motor/version.rb

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.item_filter(item, filter) ⇒ Object



62
63
64
65
# File 'lib/rss-motor.rb', line 62

def self.item_filter(item, filter)
  return false if item.empty? || filter.nil? || filter.strip.empty?
  return !item.values.select{|v| v.match(/#{filter}/i)}.empty?
end

.rss_grep(rss_urls, filters) ⇒ Object

Returned Array of Hashes, where the keys are given filters

Example

Rss::Motor.rss_grep 'http://example.com', ['item1', 'item2', 'item3']
[
   {'item1' => [{'title' => '....'}, {'title' => '....'}, ...]},
   {'item2' => [{'title' => '....'}, {'title' => '....'}, ...]},
   {'item3' => [{'title' => '....'}, {'title' => '....'}, ...]}
]


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rss-motor.rb', line 32

def self.rss_grep(rss_urls, filters)
  rss_items = filters.flatten.map{|k| {k => []} }
  [rss_urls].flatten.each do |rss_url|
    rss = rss_items rss_url
    rss.each do |item|
      [filters].flatten.each do |filter|
        rss_items.detect{|a| a.key?(filter)}[filter].push(item) if item_filter(item, filter)
      end
    end
  end
  rss_items
end


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rss-motor.rb', line 45

def self.rss_grep_link(rss_urls, filters)
  rss_items = filters.flatten.map{|k| {k => []} }
  [rss_urls].flatten.each do |rss_url|
    rss = rss_items rss_url
    rss.each do |item|
      link_body = Rss::WWW.http_requester item['link']
      [filters].flatten.each do |filter|
        rss_items.detect{|a| a.key?(filter)}[filter].push(item) if
          item_filter(item, filter) or
          link_body.match(/#{filter}/)
      end
    end
  end
  rss_items
end

.rss_items(rss_url, more_nodes = [], more_node_keys = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/rss-motor.rb', line 12

def self.rss_items(rss_url, more_nodes=[], more_node_keys={})
  rss_data = Rss::WWW.rss_channel rss_url
  return [{}] if rss_data.empty?

  xml_splitd = XMLMotor.splitter rss_data.join
  xml_tags = XMLMotor.indexify xml_splitd
  rss_items = XMLMotor.xmldata xml_splitd, xml_tags, 'item'
  rss_hash = Rss::Proc.rss_hashr rss_items, more_nodes, more_node_keys
end