Class: Clockwork::XML::Balance

Inherits:
Object
  • Object
show all
Defined in:
lib/clockwork/xml/balance.rb

Overview

XML building and parsing for checking balance.

Author:

Class Method Summary collapse

Class Method Details

.build(api) ⇒ string

Build the XML data to check the balance from the XML API.

Parameters:

Returns:

  • (string)

    XML data



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.

Parameters:

  • response (Net::HTTPResponse)

    Instance of Net::HTTPResponse

Returns:

  • (string)

    Number of remaining credits

Raises:

  • Clockwork:HTTPError - if a connection to the Clockwork API cannot be made

  • Clockwork::Error::Generic - if the API returns an error code other than 2

  • Clockwork::Error::Authentication - if API login details are incorrect



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