Class: Trustly::Data::Response

Inherits:
Trustly::Data show all
Defined in:
lib/trustly/data/response.rb

Direct Known Subclasses

JSONRPCResponse

Instance Attribute Summary collapse

Attributes inherited from Trustly::Data

#payload

Instance Method Summary collapse

Methods inherited from Trustly::Data

#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) #called from Net::HTTP.get_response("trustly.com","/api_path") -> returns Net::HTTPResponse 
  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_bodyObject

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_reasonObject

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_resultObject

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_statusObject

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

Returns:

  • (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_codeObject



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_msgObject



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_methodObject



60
61
62
# File 'lib/trustly/data/response.rb', line 60

def get_method
  return self.response_result.try(:[],'method')
end

#get_resultObject



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_signatureObject



64
65
66
# File 'lib/trustly/data/response.rb', line 64

def get_signature
  return self.response_result.try(:[],"signature")
end

#get_uuidObject



56
57
58
# File 'lib/trustly/data/response.rb', line 56

def get_uuid
  return self.response_result.try(:[],'uuid')
end

#success?Boolean

Returns:

  • (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