Class: FeedReader

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

Constant Summary collapse

DEFAULT_ENCODING =
"UTF-8"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(feed_url) ⇒ FeedReader

Returns a new instance of FeedReader.



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

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

  @encoding = @feed.source[/encoding=["'](.*?)["']/, 1]
  if not @encoding
    $log.info "No encoding found for #{feed_url}, defaulting to #{DEFAULT_ENCODING}."
    @encoding = DEFAULT_ENCODING
  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



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

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

#get_new(archive) ⇒ Object



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
76
77
# File 'lib/imap-feeder/feedreader.rb', line 43

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



39
40
41
# File 'lib/imap-feeder/feedreader.rb', line 39

def number_of_entries
  @feed.entries.size
end

#time_from(item) ⇒ Object



79
80
81
82
# File 'lib/imap-feeder/feedreader.rb', line 79

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