Class: PavlovRss::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/pavlov_rss/reader.rb

Instance Method Summary collapse

Instance Method Details

#check(&blk) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/pavlov_rss/reader.rb', line 15

def check &blk
  now = fetch
  @prev ||= now
  result = now - @prev
  result.select!(&blk) if blk
  @prev = now
  return result
end

#fetchObject



11
12
13
# File 'lib/pavlov_rss/reader.rb', line 11

def fetch
  hash_to_item(rss_to_hash(Nokogiri.XML(@opener.call)))
end

#hash_to_item(hash) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pavlov_rss/reader.rb', line 28

def hash_to_item hash
  result = case
           when hash.has_key?('rss')
             hash['rss']['channel']['item']
           when hash.has_key?('feed')
             hash['feed']['entry']
           end
  result ||= []
  return result if result.is_a? Array
  return [result]
end

#opener(&opener) ⇒ Object



7
8
9
# File 'lib/pavlov_rss/reader.rb', line 7

def opener &opener
  @opener = opener
end

#rss_to_hash(rss) ⇒ Object



24
25
26
# File 'lib/pavlov_rss/reader.rb', line 24

def rss_to_hash rss
  Hash.from_xml(rss.to_xml)
end