Class: IMAPToRSS::Handler::HSBC

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

Overview

Handles messages from HSBC savings and HSBC credit cards

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

#initializeHSBC

Selects messages with hsbc in the From header



11
12
13
# File 'lib/imap_to_rss/handler/hsbc.rb', line 11

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

Instance Method Details

#handle(uids) ⇒ Object

Turns uids into RSS items for bank-to-bank transfers and general announcements like CC payments due or interest rate changes.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/imap_to_rss/handler/hsbc.rb', line 19

def handle(uids)
  each_message uids, 'text/plain' do |uid, mail|
    @mail = mail
    @url = nil
    @description = nil

    case mail.from.first
    when '[email protected]' then
      next false unless handle_a2atransfer
    when '[email protected]' then
      next false unless handle_alert
    when '[email protected]' then
      next false unless handle_hsbc
    else
      log "Unknown From: #{mail.from.join ', '}"
      next false
    end

    add_item mail.subject, @description, mail.from, mail.date, @url,
             mail.message_id, 'HSBC'
  end
end

#handle_a2atransferObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/imap_to_rss/handler/hsbc.rb', line 42

def handle_a2atransfer
  @mail.body =~ /^Dear.*?[,:]
                 ([ \t]*\r?\n){2,}
                 (.*?)
                 ([ \t]*\r?\n){2,}
                 Sincerely,/mx

  return false unless $2

  body = $2

  body.gsub!(/[*\r]/, '')
  body.gsub!(/[ \t]*\n/, "\n")
  body = body.split(/\n\n+/).map { |para| "<p>#{para}</p>" }

  @description = body.join "\n\n"
end

#handle_alertObject



60
61
62
63
64
65
66
67
68
# File 'lib/imap_to_rss/handler/hsbc.rb', line 60

def handle_alert
  @mail.body =~ /^Dear.*?,
                 ([ \t]*\r?\n){2,}
                 (.*?)
                 ([ \t]*\r?\n){2,}/mx
  return false unless $2

  @description = $2
end

#handle_hsbcObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/imap_to_rss/handler/hsbc.rb', line 70

def handle_hsbc
  case @mail.body
  when /purchase exceeding/ then
    @mail.body =~ /Description: (.*)/
    purchase = $1.strip
    @mail.body =~ /^Amount: \$\s+([\d.]+)/
    amount = $1

    @description = <<-DESC
<p>Your purchase of #{purchase} was for $#{amount} which exceeds your
notification limit
    DESC
  when /^Dear.*?,
        ([ \t]*\r?\n){2,}
        (.*?)
        ([ \t]*\r?\n){2,}/mx
    @description = $2
  end

  return false unless @description
end