Class: Amex::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/amex/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Client

Returns a new instance of Client.



11
12
13
14
# File 'lib/amex/client.rb', line 11

def initialize(username, password)
  @username = username
  @password = password
end

Instance Method Details

#accountsObject



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
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
78
79
80
81
# File 'lib/amex/client.rb', line 16

def accounts
  # This only supports one account for now, because I'm lazy and I
  # hate traversing XML...
  options = { :body => { "PayLoadText" => request_xml }}
  response = self.class.post(
    '/myca/intl/moblclient/emea/ws.do?Face=en_GB', options
  )

  xml = Nokogiri::XML(response.body)
  xml = xml.css("XMLResponse")

  if xml.css('ServiceResponse Status').text != "success"
    raise "There was a problem logging in to American Express."
  else
    # Store the security token - we need this for further requests
    @security_token = xml.css('ClientSecurityToken').text

    accounts = [] # We'll store all the accounts in here!

    xml.css('CardAccounts CardAccount').each do |item|
       = {client: self} # All the attributes from the XML go in here
      # For each of the CardAccount objects, let's first go through
      # the CardData to pull out lots of nice information
      item.css('CardData param').each do |attribute|
        [attribute.attr('name')] = attribute.text
      end

      # Now let's go through the AccountSummaryData to find all the
      # various bits of balance information
      item.css('AccountSummaryData SummaryElement').each do |attribute|
        [attribute.attr('name')] = attribute.attr('value') ? attribute.attr('value').to_f : attribute.attr('formattedValue')
      end

      # We have all the attributes ready to go, so let's make an
      # Amex::CardAccount object
       = Amex::CardAccount.new()

      # Finally, let's rip out all the loyalty balances...
      item.css('LoyaltyProgramData LoyaltyElement').each do |element|
        .loyalty_programmes << Amex::LoyaltyProgramme.new(
          element.attr('label'), element.attr('formattedValue').gsub(",", "").to_i
        )
      end

      # Now we can fetch the transactions...
      options = { :body => {
        "PayLoadText" => statement_request_xml(.card_index)
      }}
      response = self.class.post(
        '/myca/intl/moblclient/emea/ws.do?Face=en_GB', options
      )
      xml = Nokogiri::XML(response.body)
      xml = xml.css("XMLResponse")

      xml.css('Transaction').each do |transaction|
        .transactions << Amex::Transaction.new(transaction)
      end

      accounts << 

    end

    accounts
  end

end

#statement_request_xml(card_index, billing_period = 0) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/amex/client.rb', line 83

def statement_request_xml(card_index, billing_period=0)
  # Generates XML for grabbing the last statement's transactions for a
  # card, using the card_index attribute from an account's XML

  xml = File.read(
    File.expand_path(File.dirname(__FILE__) + '/data/statement_request.xml')
  )

  security_token = @security_token
  ERB.new(xml).result(binding)
end