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) ⇒ Amex::Client

Generates an Amex::Client object from a username and password

Parameters:

  • username (String)

    Your American Express online services username

  • password (String)

    Your American Express online services password


18
19
20
21
# File 'lib/amex/client.rb', line 18

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

Instance Method Details

#accountsArray<Amex::CardAccount>

Fetches the cards on an American Express online services account

Returns:


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
82
83
84
85
86
87
88
89
90
# File 'lib/amex/client.rb', line 27

def accounts
  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) ⇒ String

Generates the XML to send in a request to fetch transactions for a card

Parameters:

  • card_index (Integer)

    The index of the card you're looking up in your account (see Amex::CardAccount#card_index)

  • billing_period (Integer) (defaults to: 0)

    The billing period to look at, with “0” being transactions since your last statement, “1” being your last statement, “2” the statement before that and so on.…

Returns:

  • (String)

    XML to be sent in the request


102
103
104
105
106
107
108
109
# File 'lib/amex/client.rb', line 102

def statement_request_xml(card_index, billing_period=0)
  xml = File.read(
    File.expand_path(File.dirname(__FILE__) + '/data/statement_request.xml')
  )

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