Class: IMAPToRSS

Inherits:
IMAPProcessor
  • Object
show all
Defined in:
lib/imap_to_rss.rb,
lib/imap_to_rss/test_case.rb

Overview

:stopdoc:

Defined Under Namespace

Classes: Handler, RSSItem, TestCase

Constant Summary collapse

VERSION =

The version of IMAPToRSS you are using

'1.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ IMAPToRSS

Creates a new IMAPToRSS and connects to the selected server.



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/imap_to_rss.rb', line 73

def initialize(options = {})
  super

  @handlers = []
  @rss_items = []
  @output = options[:Output]

  connection = connect options[:Host], options[:Port], options[:SSL],
                       options[:Username], options[:Password], options[:Auth]

  @imap = connection.imap
end

Instance Attribute Details

#rss_itemsObject (readonly)

All added RSS items



34
35
36
# File 'lib/imap_to_rss.rb', line 34

def rss_items
  @rss_items
end

Class Method Details

.process_args(args) ⇒ Object

Processes command-line options



39
40
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
# File 'lib/imap_to_rss.rb', line 39

def self.process_args(args)
  required_args = {
    :Output => 'imap_to_rss.rss'
  }

  add_move

  super __FILE__, args, required_args do |opts, options|
    handlers = IMAPToRSS::Handler.handlers.map do |handler|
      handler.name.split('::').last
    end

    opts.separator ''
    opts.separator "Handlers: #{handlers.join ', '}"
    opts.separator ''

    opts.on("--handler=HANDLER", handlers,
            "Handler to run",
            "Default: all handlers") do |handler|
      options[:Handler] = handler
    end

    opts.on("--output=FILE",
            "File to write the RSS feed to",
            "Default: #{options[:Output]}",
            "Options file name: File") do |file|
      options[:Output] = file
    end
  end
end

Instance Method Details

#build_rssObject

Builds an RSS feed from rss_items



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/imap_to_rss.rb', line 89

def build_rss
  log 'Building RSS feed'
  rss = Nokogiri::XML::Builder.new

  rss_items = @rss_items.sort_by { |rss_item| rss_item.pub_date }
  host = options[:Host]

  copyover = []

  if File.exist? @output then
    doc = nil

    open @output, 'rb' do |io| doc = Nokogiri::XML io end

    copyover_count = 50 - rss_items.length

    if copyover_count > 0 then
      items = doc.xpath('//rss/channel/item')

      index = [copyover_count, items.length].min

      copyover = items.to_a[-index..-1]
    end
  end

  rss.rss :version => '2.0' do
    rss.channel do
      rss.title 'IMAP to RSS'
      rss.description "An RSS feed built from your IMAP server"
      rss.generator "imap_to_rss version #{IMAPToRSS::VERSION}"
      rss.docs 'http://cyber.law.harvard.edu/rss/rss.html'

      copyover.each do |item|
        rss.send :insert, item
      end

      rss_items.each do |item|
        rss.item do
          rss.title item.title
          rss.description item.description
          rss.author item.author
          rss.pubDate item.pub_date.rfc822
          rss.link item.link if item.link
          rss.guid item.guid, :isPermaLink => false if item.guid
          rss.category item.category if item.category
        end
      end
    end
  end

  open @output, 'w' do |io|
    io.write rss.to_xml
  end

  log 'Saved RSS feed'
end

#connect(*args) ⇒ Object



6
7
8
9
10
# File 'lib/imap_to_rss/test_case.rb', line 6

def connect(*args)
  o = Object.new
  def o.imap() end
  o
end

#runObject

Processes mailboxes with each handler then writes out the RSS file.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/imap_to_rss.rb', line 149

def run
  handlers = IMAPToRSS::Handler.handlers

  handlers.delete_if do |handler|
    handler.name.split('::').last != options[:Handler]
  end if options[:Handler]

  handlers = handlers.map do |handler|
    handler = handler.new
    handler.setup self
    handler
  end

  dest_mailbox = options[:MoveTo]

  @boxes.each do |mailbox|
    @imap.select mailbox
    log "Selected mailbox #{mailbox}"

    handlers.each do |handler|
      log "Running handler #{handler.search.join ' '}"
      messages = @imap.search [
        'NOT', 'DELETED',
        'NOT', 'KEYWORD', 'IMAP_TO_RSS',
        *handler.search
      ]

      next if messages.empty?

      handled = handler.handle messages

      # only tag handled messages, examined messages may be handled in the
      # future
      next if handled.empty?

      @imap.store handled, '+FLAGS', %w[IMAP_TO_RSS]

      move_messages handled, dest_mailbox if dest_mailbox
    end

    build_rss
  end
end