Class: Clockwork::XML::Credit

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

Overview

XML building and parsing for checking credit.

Author:

Class Method Summary collapse

Class Method Details

.build(api) ⇒ string

Build the XML data to check the credit 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/credit.rb', line 11

def self.build api
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    xml.Credit {
      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
# File 'lib/clockwork/xml/credit.rb', line 31

def self.parse response
  if response.code.to_i == 200
    doc = Nokogiri.parse( response.body )
    if doc.css('ErrDesc').empty?
      doc.css('Credit').inner_html.to_i
    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 credit."
  end
end