Class: IMAPToRSS::Handler::Amazon
- Inherits:
-
IMAPToRSS::Handler
- Object
- IMAPToRSS::Handler
- IMAPToRSS::Handler::Amazon
- Defined in:
- lib/imap_to_rss/handler/amazon.rb
Overview
Handles messages from Amazon
Instance Attribute Summary
Attributes inherited from IMAPToRSS::Handler
Instance Method Summary collapse
-
#add_item(subject, description, url) ⇒ Object
Adds an RSS item with
subject
,description
andurl
. -
#add_order(order) ⇒ Object
Adds an RSS item from order
order
. -
#aws_bill ⇒ Object
Adds an RSS item for an AWS bill.
-
#gift_card ⇒ Object
Adds an RSS item for a gift card.
-
#handle(uids) ⇒ Object
Turns
uids
into RSSItems. -
#initialize ⇒ Amazon
constructor
Selects messages with amazon in the From header.
-
#order_cancellation ⇒ Object
Adds an RSS item for an order cancellation.
-
#order_revision(order_number) ⇒ Object
Adds an RSS item for an order revision on
order_number
. -
#order_shipped ⇒ Object
Adds an RSS item for a shipped order.
-
#order_shipped_bang ⇒ Object
Adds an RSS item for a shipped order (alternate subject ending with !).
-
#order_table(items) ⇒ Object
Creates an HTML table for
items
. -
#order_url(order_number) ⇒ Object
Returns the link for order
order_number
.
Methods inherited from IMAPToRSS::Handler
#each_message, handlers, inherited, #log, #setup
Constructor Details
#initialize ⇒ Amazon
Selects messages with amazon in the From header
11 12 13 |
# File 'lib/imap_to_rss/handler/amazon.rb', line 11 def initialize @search = 'FROM', 'amazon' end |
Instance Method Details
#add_item(subject, description, url) ⇒ Object
Adds an RSS item with subject
, description
and url
56 57 58 59 |
# File 'lib/imap_to_rss/handler/amazon.rb', line 56 def add_item(subject, description, url) super subject, description, @mail.from, @mail.date, url, @mail., 'Amazon' end |
#add_order(order) ⇒ Object
Adds an RSS item from order order
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/imap_to_rss/handler/amazon.rb', line 64 def add_order(order) items = order.scan(/^(\d+) "(.*?)"/) order =~ /Order number:\s+([\d-]+)/ return unless $1 order_number = $1 url = order_url order_number order =~ /^Total for this Order:\s+(.*)/ total = $1.strip subject = "Amazon order #{order_number}" description = "<p>Total: #{total}\n" description << order_table(items) add_item subject, description, url end |
#aws_bill ⇒ Object
Adds an RSS item for an AWS bill
87 88 89 90 91 92 93 94 |
# File 'lib/imap_to_rss/handler/amazon.rb', line 87 def aws_bill @mail.body =~ /^Total: (.*)/ total = $1.strip @mail.body =~ /^(http.*)/ add_item "Amazon Web Services Bill: #{total}", '', $1.strip end |
#gift_card ⇒ Object
Adds an RSS item for a gift card
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/imap_to_rss/handler/amazon.rb', line 99 def gift_card url = "https://www.amazon.com/gp/css/account/payment/view-gc-balance.html" @mail.body =~ /^Amount: (.*)/ amount = $1.strip @mail.body =~ /^From: (.*)/ from = $1.strip @mail.body =~ /^Gift Message: (.*)/ = $1.strip @mail.body =~ /^Claim code (.*)/ claim_code = $1.strip subject = "Amazon Gift Card from #{from} - #{amount}!" description = "<p><strong>#{from} sent you a #{amount} gift card!</strong>\n\n" description << "<p>#{}\n\n" description << "<h2>#{claim_code}</h2>\n\n" description << "<p><a href=\"#{url}\">Claim your gift card</a>" add_item subject, description, url end |
#handle(uids) ⇒ Object
Turns uids
into RSSItems
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 47 48 49 50 51 |
# File 'lib/imap_to_rss/handler/amazon.rb', line 18 def handle(uids) uids, 'text/plain' do |uid, mail| @mail = mail case @mail.subject when /^Your Order with Amazon.com/ then if @mail.body =~ /Your purchase has been divided into/ then @mail.body.split(/^Order #\d+/).each do |order| add_order order end else add_order @mail.body end when /has shipped!$/ then order_shipped_bang when /has shipped/ then order_shipped when /^Amazon.com - Order Revision \((.*?)\)/ then order_revision $1 when /^Amazon.com - Your Cancellation/ then order_cancellation when /sent you an Amazon.com Gift Card!$/ then gift_card when /^Amazon Web Services Billing Statement Available/ then aws_bill when /^Your savings from Amazon.com/, # ignore /^Your Amazon.com Purchase from/ then # dup of regular order email next else log "Unknown Subject: %p" % @mail.subject next false end end end |
#order_cancellation ⇒ Object
Adds an RSS item for an order cancellation
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/imap_to_rss/handler/amazon.rb', line 126 def order_cancellation @mail.body =~ /order #(.*?) / order_number = $1 items = @mail.body.scan(/(\d?) of (.*)/) url = order_url order_number subject = "Amazon order cancellation #{order_number}" description = "<p>You cancelled your order:\n\n" description << order_table(items) add_item subject, description, url end |
#order_revision(order_number) ⇒ Object
Adds an RSS item for an order revision on order_number
231 232 233 234 235 236 237 238 239 240 |
# File 'lib/imap_to_rss/handler/amazon.rb', line 231 def order_revision(order_number) url = order_url order_number subject = "Order Revision (#{order_number})" @mail.body =~ /^Dear .*?,\r\n\r\n(.*?)\r\n\r\n/m description = "<p>#{$1}" add_item subject, description, url end |
#order_shipped ⇒ Object
Adds an RSS item for a shipped order
144 145 146 147 148 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 |
# File 'lib/imap_to_rss/handler/amazon.rb', line 144 def order_shipped @mail.body =~ /^(The following items .*?:\r\n.*?)(Shipping Carrier|Item Subtotal)/m items = $1.map do |item| next unless item =~ /\d+\s(.*?)\$[\d.]+\s+(\d+)/ [$2, $1.strip] end.compact carrier = $1.strip if @mail.body =~ /(?:Shipping Carrier:|Shipped via) (.*)/ date = $1.strip if @mail.body =~ /Ship Date: (.*)/ speed = $1.strip if @mail.body =~ /Shipping Speed: (.*)/ tracking_number = $1.strip if @mail.body =~ /(?:Carrier Tracking ID|Tracking number): (.*)/ @mail.body =~ /(?:Your shipping address|This shipment was sent to):\r\n\r\n(.*?)\r\n\r\n/m address = $1.split("\n").map { |line| line.strip }.join "<br />\n" if $1 if tracking_number then url = case carrier when 'USPS' then "http://trkcnfrm1.smi.usps.com/PTSInternetWeb/InterLabelInquiry.do?strOrigTrackNum=#{tracking_number}" when 'FedEx' then "http://fedex.com/Tracking?tracknumbers=#{tracking_number}" when 'UPS' then "http://wwwapps.ups.com/WebTracking/processInputRequest?InquiryNumber1=#{tracking_number}" else log "Unknown carrier: %p" % carrier "http://www.google.com/search?q=#{tracking_number}" end end subject = @mail.subject description = order_table items if url then description << "<p>Via <a href=\"#{url}\">#{carrier}</a>\n\n" elsif carrier then description << "<p>Via #{carrier} (no tracking number found)\n\n" end description << "<p>To:<br>\n#{address}" if address add_item subject, description, url end |
#order_shipped_bang ⇒ Object
Adds an RSS item for a shipped order (alternate subject ending with !)
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/imap_to_rss/handler/amazon.rb', line 192 def order_shipped_bang @mail.body =~ /this shipment:\r\n\r\n(.*?)\r\n\r\nShip/m items = $1.scan(/(\d+) of (.*)/) carrier = $1.strip if @mail.body =~ /Shipped via (.*?)\s/ tracking_number = $1.strip if @mail.body =~ /Tracking number: (.*?)\s/ @mail.body =~ /This shipment was sent to:\r\n\r\n(.*?)\r\n\r\n/m address = $1.split("\n").map { |line| line.strip }.join "<br />\n" if $1 if tracking_number then url = case carrier when 'USPS' then "http://trkcnfrm1.smi.usps.com/PTSInternetWeb/InterLabelInquiry.do?strOrigTrackNum=#{tracking_number}" when 'FedEx' then "http://fedex.com/Tracking?tracknumbers=#{tracking_number}" else log "Unknown carrier: %p" % carrier nil end end subject = @mail.subject description = order_table items if url then description << "<p>Via <a href=\"#{url}\">#{carrier}</a>\n\n" elsif carrier then description << "<p>Via #{carrier} (no tracking number found)\n\n" end description << "<p>To:<br>\n#{address}" if address add_item subject, description, url end |
#order_table(items) ⇒ Object
Creates an HTML table for items
245 246 247 248 249 250 251 252 253 254 |
# File 'lib/imap_to_rss/handler/amazon.rb', line 245 def order_table(items) table = "<table>\n<tr><th>Quantity<th>Description\n" items.each do |qty, desc| table << "<tr><td>#{qty}<td>#{desc.strip}\n" end table << "</table>\n\n" table end |
#order_url(order_number) ⇒ Object
Returns the link for order order_number
259 260 261 |
# File 'lib/imap_to_rss/handler/amazon.rb', line 259 def order_url(order_number) "https://www.amazon.com/gp/css/summary/edit.html?ie=UTF8&orderID=#{order_number}" end |