Class: RSS_Check::RSS_File

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

Direct Known Subclasses

LIRS_File

Instance Method Summary collapse

Constructor Details

#initialize(path, init_now) ⇒ RSS_File

Returns a new instance of RSS_File.



28
29
30
31
# File 'lib/rss_check.rb', line 28

def initialize path, init_now
  @uri = URI.parse(path)
  @entry_time = @file_time = (init_now ? Time.now : Time.at(0))
end

Instance Method Details

#checkObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rss_check.rb', line 33

def check
  begin
    if (mt=mtime) > @file_time
      @file_time = mt
      check_entries
    else
      []
    end
  rescue => e
    [{
      :about => e.message,
      :title => "RSS Check Error (#{@uri})",
      :ccode => 'UTF-8'
    }]
  end
end

#check_entriesObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rss_check.rb', line 58

def check_entries
  rss = RSS::Parser.parse(read_content, false)
  et = @entry_time
  items = rss.items.sort_by{|e|
    date_of(e)
  }.map{|e|
    e_date = date_of(e)
    if e_date > @entry_time
      if e_date > et
        et = e_date
      end
      {
        :about => e.about,
        :title => e.title,
        :ccode => 'UTF-8'
      }
    end
  }.compact
  @entry_time = et
  items
end

#date_of(e) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/rss_check.rb', line 50

def date_of e
  if e.respond_to? :dc_date
    e.dc_date || Time.at(0)
  else
    e.pubDate || Time.at(0)
  end
end

#mtimeObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/rss_check.rb', line 97

def mtime
  case @uri.scheme
  when 'http'
    open(@uri){|f|
      f.last_modified || Time.now
    }
  else
    File.mtime(@rss_file)
  end
end

#read_contentObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rss_check.rb', line 80

def read_content
  case @uri.scheme
  when 'http'
    open(@uri){|f|
      if f.content_encoding.any?{|e| /gzip/ =~ e}
        Zlib::GzipReader.new(StringIO.new(f.read)).read || ''
      else
        f.read
      end
    }
  else
    open(@uri.to_s){|f|
      f.read
    }
  end
end