Class: Amex::Client
Instance Method Summary collapse
-
#accounts ⇒ Array<Amex::CardAccount>
Fetches the cards on an American Express online services account.
-
#initialize(username, password) ⇒ Amex::Client
constructor
Generates an Amex::Client object from a username and password.
-
#statement_request_xml(card_index, billing_period = 0) ⇒ String
Generates the XML to send in a request to fetch transactions for a card.
Constructor Details
#initialize(username, password) ⇒ Amex::Client
Generates an Amex::Client object from a username and password
18 19 20 21 |
# File 'lib/amex/client.rb', line 18 def initialize(username, password) @username = username @password = password end |
Instance Method Details
#accounts ⇒ Array<Amex::CardAccount>
Fetches the cards on an American Express online services account
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 = { :body => { "PayLoadText" => request_xml }} response = self.class.post( '/myca/intl/moblclient/emea/ws.do?Face=en_GB', ) 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| account_details = {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| account_details[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| account_details[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 account = Amex::CardAccount.new(account_details) # Finally, let's rip out all the loyalty balances... item.css('LoyaltyProgramData LoyaltyElement').each do |element| account.loyalty_programmes << Amex::LoyaltyProgramme.new( element.attr('label'), element.attr('formattedValue').gsub(",", "").to_i ) end # Now we can fetch the transactions... = { :body => { "PayLoadText" => statement_request_xml(account.card_index) }} response = self.class.post( '/myca/intl/moblclient/emea/ws.do?Face=en_GB', ) xml = Nokogiri::XML(response.body) xml = xml.css("XMLResponse") xml.css('Transaction').each do |transaction| account.transactions << Amex::Transaction.new(transaction) end accounts << account 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
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.(File.dirname(__FILE__) + '/data/statement_request.xml') ) security_token = @security_token ERB.new(xml).result(binding) end |