Class: IMAPToRSS::Handler::Itunes

Inherits:
IMAPToRSS::Handler show all
Defined in:
lib/imap_to_rss/handler/itunes.rb

Overview

Processes iTunes receipts

Instance Attribute Summary

Attributes inherited from IMAPToRSS::Handler

#mail, #search

Instance Method Summary collapse

Methods inherited from IMAPToRSS::Handler

#add_item, #each_message, handlers, inherited, #log, #setup

Constructor Details

#initializeItunes

Returns a new instance of Itunes.



8
9
10
# File 'lib/imap_to_rss/handler/itunes.rb', line 8

def initialize
  @search = 'FROM', 'itunes'
end

Instance Method Details

#handle(uids) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/imap_to_rss/handler/itunes.rb', line 12

def handle(uids)
  each_message uids, 'text/plain' do |uid, mail|
    body = ''
    mail.body =~ /Order Number: ([^\s]*)/
    order_number = $1

    next false unless order_number

    mail.body =~ /Order Total: (\$[\d.]+)/
    total = $1

    mail.body =~ /Purchase History:.*?(https:.*?)\r/m
    url = $1

    mail.body =~ /^Item\sNumber.*?
                  (^-+\r?\n)
                  (.*?)
                  \1/mx

    body << "<table>\n<tr><th>Item<th>Price\n"

    $2.scan(/^([^\s]+)\s+(.*?)\s+(Free|\$[\d.]+)/) do |item, name, price|
      body << "<tr><td>#{name}<td>#{price}\n"
    end

    body << "</table>\n"

    body << "<p>Total: #{total}"

    subject = "iTunes Receipt ##{order_number}, #{total}"

    add_item subject, body, mail.from, mail.date, url,
             mail.message_id, 'iTunes'
  end
end