Class: FeedReader

Inherits:
Object
  • Object
show all
Defined in:
lib/imap-feeder/feedreader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(feed_url) ⇒ FeedReader

Returns a new instance of FeedReader.



19
20
21
22
23
24
25
26
27
28
# File 'lib/imap-feeder/feedreader.rb', line 19

def initialize(feed_url)
  @feed_url = feed_url
  @feed = SimpleRSS.parse(open(feed_url))

  @encoding = @feed.source[/encoding=["'](.*?)["']/, 1]
  if not @encoding
    $log.warn "No encoding found for #{feed_url}, defaulting to UTF-8."
    @encoding = "UTF-8"
  end
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



17
18
19
# File 'lib/imap-feeder/feedreader.rb', line 17

def messages
  @messages
end

Instance Method Details

#conv(str) ⇒ Object



30
31
32
33
34
35
# File 'lib/imap-feeder/feedreader.rb', line 30

def conv(str)
  Iconv.iconv("UTF-8", @encoding, str).first
rescue Iconv::IllegalSequence => e
  $log.error "IConv reports an IllegalSequence: #{e.message} from #{str}"
  return str
end

#get_new(archive) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/imap-feeder/feedreader.rb', line 41

def get_new(archive)
  return [] if not @feed

  archive ||= []
  if archive.include?("")
    $log.warn "Title is empty, that should never happen! Aborting #{@feed_url}."
    return []
  end

  messages = []
  @feed.entries.each do |item|

    body = conv(item.content_encoded || item.content ||
                item.summary || item.description)
    message = Message.new(
      :title => conv(item.title),
      :time => time_from(item),
      :body => body,
      :from => conv(item.author),
      :url => conv(item.link)
    )

    item_identifier = message.generate_identifier

    if archive.include? item_identifier
      short_name = message.title[0..30]
      short_name << "" if message.title.length > 30
      $log.debug "Already have '#{short_name}'."
    else
      messages << message
    end
  end

  messages
end

#number_of_entriesObject



37
38
39
# File 'lib/imap-feeder/feedreader.rb', line 37

def number_of_entries
  @feed.entries.size
end

#time_from(item) ⇒ Object



77
78
79
80
# File 'lib/imap-feeder/feedreader.rb', line 77

def time_from item
  return nil if not item
  item.published || item.pubDate || item.date_published || item.updated || nil
end