Class: Amex::Client
Instance Method Summary collapse
- #accounts ⇒ Object
-
#initialize(username, password) ⇒ Client
constructor
A new instance of Client.
- #request_xml ⇒ Object
- #statement_request_xml(card_index) ⇒ Object
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
#accounts ⇒ Object
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 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/amex/client.rb', line 37 def accounts # This only supports one account for now, because I'm lazy and I # hate traversing XML... = { :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 = {} # 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 |
#request_xml ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/amex/client.rb', line 16 def request_xml xml = File.read( File.(File.dirname(__FILE__) + '/data/request.xml') ) username = @username password = @password = Time.now.to_i ERB.new(xml).result(binding) end |
#statement_request_xml(card_index) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/amex/client.rb', line 28 def statement_request_xml(card_index) xml = File.read( File.(File.dirname(__FILE__) + '/data/statement_request.xml') ) security_token = @security_token ERB.new(xml).result(binding) end |