Class: Trustly::Data::Response
Instance Attribute Summary collapse
#payload
Instance Method Summary
collapse
#get, #get_from, #json, #pop, #set, #set_in, #vacumm
Constructor Details
#initialize(http_response) ⇒ Response
called from Net::HTTP.get_response(“trustly.com”,“/api_path”) -> returns Net::HTTPResponse
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/trustly/data/response.rb', line 4
def initialize(http_response)
super()
self.response_status = http_response.code
self.response_reason = http_response.class.name
self.response_body = http_response.body
begin
self.payload = JSON.parse(self.response_body)
rescue JSON::ParserError => e
if self.response_status != 200
raise Trustly::Exception::ConnectionError, "#{self.response_status}: #{self.response_reason} [#{self.response_body}]"
else
raise Trustly::Exception::DataError, e.message
end
end
begin
self.response_result = self.get('result')
rescue IndexError::KeyError => e
self.response_result = nil
end
if self.response_result.nil?
begin
self.response_result = self.payload["error"]["error"]
rescue IndexError::KeyError => e
end
end
raise Trustly::Exception::DataError, "No result or error in response #{self.payload}" if self.response_result.nil?
end
|
Instance Attribute Details
#response_body ⇒ Object
Returns the value of attribute response_body.
2
3
4
|
# File 'lib/trustly/data/response.rb', line 2
def response_body
@response_body
end
|
#response_reason ⇒ Object
Returns the value of attribute response_reason.
2
3
4
|
# File 'lib/trustly/data/response.rb', line 2
def response_reason
@response_reason
end
|
#response_result ⇒ Object
Returns the value of attribute response_result.
2
3
4
|
# File 'lib/trustly/data/response.rb', line 2
def response_result
@response_result
end
|
#response_status ⇒ Object
Returns the value of attribute response_status.
2
3
4
|
# File 'lib/trustly/data/response.rb', line 2
def response_status
@response_status
end
|
Instance Method Details
#error? ⇒ Boolean
34
35
36
37
38
|
# File 'lib/trustly/data/response.rb', line 34
def error?
return !self.get('error').nil?
rescue IndexError::KeyError => e
return false
end
|
#error_code ⇒ Object
40
41
42
43
|
# File 'lib/trustly/data/response.rb', line 40
def error_code
return nil unless self.error?
return self.response_result["data"].try(:[],'code')
end
|
#error_msg ⇒ Object
45
46
47
48
|
# File 'lib/trustly/data/response.rb', line 45
def error_msg
return nil unless self.error?
return self.response_result["data"].try(:[],'message')
end
|
#get_method ⇒ Object
60
61
62
|
# File 'lib/trustly/data/response.rb', line 60
def get_method
return self.response_result.try(:[],'method')
end
|
#get_result ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/trustly/data/response.rb', line 68
def get_result
unless name.nil?
if self.response_result.is_a?(Hash)
return self.response_result.try(:[],name)
else
raise StandardError::TypeError, "Result is not a Hash"
end
else
return self.response_result.dup
end
end
|
#get_signature ⇒ Object
64
65
66
|
# File 'lib/trustly/data/response.rb', line 64
def get_signature
return self.response_result.try(:[],"signature")
end
|
#get_uuid ⇒ Object
56
57
58
|
# File 'lib/trustly/data/response.rb', line 56
def get_uuid
return self.response_result.try(:[],'uuid')
end
|
#success? ⇒ Boolean
50
51
52
53
54
|
# File 'lib/trustly/data/response.rb', line 50
def success?
return !self.get('result').nil?
rescue IndexError::KeyError => e
return false
end
|