Class: KYCAID::Response
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- KYCAID::Response
- Defined in:
- lib/kycaid/response.rb
Overview
Response is an object to wrap parsed json response.
Direct Known Subclasses
Class Method Summary collapse
-
.respond(response) ⇒ Object
Wraps JSON response into OpenStruct.
Instance Method Summary collapse
-
#handle_error(err) ⇒ Object
-
If response is not successful, set errors to array of detected errors.
-
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
Constructor Details
#initialize(response) ⇒ Response
Returns a new instance of Response.
4 5 6 |
# File 'lib/kycaid/response.rb', line 4 def initialize(response) super(response) end |
Class Method Details
.respond(response) ⇒ Object
Wraps JSON response into OpenStruct. Original response in stored in raw_response
22 23 24 25 26 27 28 29 30 |
# File 'lib/kycaid/response.rb', line 22 def self.respond(response) resp = new(JSON.parse(response.body)) resp.raw_response = response resp.handle_error(Unauthorized.new(response.body)) if response.status == 403 resp.handle_error(Error.new(response.body)) unless response.success? resp end |
Instance Method Details
#handle_error(err) ⇒ Object
-
If response is not successful, set errors to array of detected errors.
-
If response is successful, errors in nil.
-
If raise_errors is set in configuration, will raise error if any.
11 12 13 14 15 16 17 18 |
# File 'lib/kycaid/response.rb', line 11 def handle_error(err) self.errors ||= [] self.errors << err raise err if KYCAID.configuration.raise_errors self end |