Class: BasicResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/basic_response.rb

Overview

Base class for service responses.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_xml, raise_exception_on_error = true) ⇒ BasicResponse

Constructor.

Parameters

response_xml

Xml as returned by a status-method call.

raise_exception_on_error

Xml as returned by a status-method call.



15
16
17
18
19
20
# File 'lib/basic_response.rb', line 15

def initialize(response_xml, raise_exception_on_error = true)
  doc = response_xml.document
  @error_code      = doc.xpath("//errorCode").to_s || doc.xpath("//statusCode").to_s
  @error_message   = doc.xpath("//errorMessage").to_s || doc.xpath("//statusMessage").to_s
  raise_on_error(response_xml) if raise_exception_on_error
end

Instance Attribute Details

#error_codeObject

Returns the value of attribute error_code.



5
6
7
# File 'lib/basic_response.rb', line 5

def error_code
  @error_code
end

#error_messageObject

Returns the value of attribute error_message.



5
6
7
# File 'lib/basic_response.rb', line 5

def error_message
  @error_message
end

Instance Method Details

#inizializeObject

Constructor



8
9
# File 'lib/basic_response.rb', line 8

def inizialize
end

#raise_on_error(response_xml) ⇒ Object

Raises an exception if the response is an error. Since in some response types the error code is not named “errorCode” it is not possible to call this method only in the constructur of this base class. This is sufficient for services using the BaseResponse directly but subclasses of BaseResponse need to call this method at the end of their constructure initialize.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/basic_response.rb', line 32

def raise_on_error(response_xml)
  if @error_code && !@error_code.empty? &&  @error_code != "0000" then

    # It is important to create the exception with self.class.new and not only BasicResponse.new
    # This is because this method can be also invoked from a subclass. If a VoiceCallResponse object for example
    # raises an exception you will receive "VoiceCallResponse" from self.class. This is very important because
    # only the VoiceCallResponse knows that in this case the response code is called <tt>status</tt>.
    # This is because there is an inconsistency within the different service responses. 
    raise(ServiceException.new( self.class.new(response_xml, false) ), "The developer garden service you invoked responded with an error: " + @error_message.to_s)
  end
end

#to_sObject

Returns self.inspect. Good for debugging purposes.



23
24
25
# File 'lib/basic_response.rb', line 23

def to_s
  return self.inspect
end