Class: VSafe::Response

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

Constant Summary collapse

SUCCESS_RESPONSE_CODE =
"0".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vsafe/response.rb', line 26

def initialize(response)
  @response = response

  if response.success?
    unless success?
      @error = ResponseError.new(response.parsed_response)
    end
  else
    raise RequestError.new(response)
  end
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



8
9
10
# File 'lib/vsafe/response.rb', line 8

def error
  @error
end

#responseObject (readonly)

Returns the value of attribute response.



8
9
10
# File 'lib/vsafe/response.rb', line 8

def response
  @response
end

Class Method Details

.define_attribute_mapping(attr_method, response_parameter, wrapper = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/vsafe/response.rb', line 10

def self.define_attribute_mapping(attr_method, response_parameter, wrapper = nil)
  define_method(attr_method) do
    memo_variable = "@_#{attr_method}"
    memo_value = instance_variable_get(memo_variable)
    return memo_value if instance_variable_defined?(memo_variable)

    memo_value = if wrapper && response.parsed_response[response_parameter]
      wrapper.new(response.parsed_response[response_parameter])
    else
      response.parsed_response[response_parameter]
    end

    instance_variable_set(memo_variable, memo_value)
  end
end

Instance Method Details

#success?Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/vsafe/response.rb', line 38

def success?
  response.success? &&
    response.parsed_response["ResponseCode"].to_s == SUCCESS_RESPONSE_CODE
end

#to_hashObject



43
44
45
# File 'lib/vsafe/response.rb', line 43

def to_hash
  response.parsed_response || {}
end