Class: Clockwork::XML::Balance
- Inherits:
-
Object
- Object
- Clockwork::XML::Balance
- Defined in:
- lib/clockwork/xml/balance.rb
Overview
XML building and parsing for checking balance.
Class Method Summary collapse
-
.build(api) ⇒ string
Build the XML data to check the balance from the XML API.
-
.parse(response) ⇒ string
Parse the XML response.
Class Method Details
.build(api) ⇒ string
Build the XML data to check the balance from the XML API.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/clockwork/xml/balance.rb', line 11 def self.build api builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml| xml.Balance { if api.api_key xml.Key api.api_key else xml.Username api.username xml.Password api.password end } end builder.to_xml end |
.parse(response) ⇒ string
Parse the XML response.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/clockwork/xml/balance.rb', line 31 def self.parse response if response.code.to_i == 200 doc = Nokogiri.parse( response.body ) if doc.css('ErrDesc').empty? hsh = {} hsh[:account_type] = doc.css('Balance_Resp').css('AccountType').inner_html hsh[:balance] = doc.css('Balance_Resp').css('Balance').inner_html.to_f hsh[:currency] = { :code => doc.css('Balance_Resp').css('Currency').css('Code').inner_html, :symbol => doc.css('Balance_Resp').css('Currency').css('Symbol').inner_html } hsh elsif doc.css('ErrNo').inner_html.to_i == 2 raise Clockwork::Error::Authentication, doc.css('ErrDesc').inner_html else raise Clockwork::Error::Generic, doc.css('ErrDesc').inner_html end else raise Clockwork::Error::HTTP, "Could not connect to the Clockwork API to check balance." end end |