Class: PostageApp::Response
- Inherits:
-
Object
- Object
- PostageApp::Response
- Defined in:
- lib/postageapp/response.rb
Constant Summary collapse
- STATUS_TIMEOUT =
Constants ============================================================
'timeout'.freeze
- STATUS_FAIL =
'fail'.freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
The data payload of the response.
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#message ⇒ Object
readonly
The message of the response.
-
#status ⇒ Object
readonly
The status of the response in string format (like: ok, bad_request) Will be set to
fail
if Request times out. -
#uid ⇒ Object
readonly
The UID should match the Request’s UID.
Instance Method Summary collapse
-
#initialize(http_response) ⇒ Response
constructor
Takes in Net::HTTPResponse object as the attribute.
-
#method_missing(method) ⇒ Object
Little helper that checks for the Response status => @response.ok? >> true => @response.fail? >> false.
Constructor Details
#initialize(http_response) ⇒ Response
Takes in Net::HTTPResponse object as the attribute. If something goes wrong Response will be thought of as failed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/postageapp/response.rb', line 30 def initialize(http_response) case (http_response) when Exception @status = STATUS_TIMEOUT @message = '[%s] %s' % [ http_response.class, http_response.to_s ] else hash = JSON::parse(http_response.body) _response = hash['response'] @status = _response['status'] @uid = _response['uid'] @message = _response['message'] @data = hash['data'] end rescue => e @status = STATUS_FAIL @exception = '[%s] %s' % [ e.class, e ] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method) ⇒ Object
Little helper that checks for the Response status
=> @response.ok?
>> true
=> @response.fail?
>> false
56 57 58 |
# File 'lib/postageapp/response.rb', line 56 def method_missing(method) /.*?\?$/.match(method.to_s) ? "#{self.status}?" == method.to_s : super(method) end |
Instance Attribute Details
#data ⇒ Object (readonly)
The data payload of the response. This is usually the return value of the request we’re looking for
22 23 24 |
# File 'lib/postageapp/response.rb', line 22 def data @data end |
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
24 25 26 |
# File 'lib/postageapp/response.rb', line 24 def exception @exception end |
#message ⇒ Object (readonly)
The message of the response. Could be used as an error explanation.
18 19 20 |
# File 'lib/postageapp/response.rb', line 18 def @message end |
#status ⇒ Object (readonly)
The status of the response in string format (like: ok, bad_request) Will be set to fail
if Request times out
15 16 17 |
# File 'lib/postageapp/response.rb', line 15 def status @status end |
#uid ⇒ Object (readonly)
The UID should match the Request’s UID. If Request didn’t provide with one PostageApp service should generate it for the Response
11 12 13 |
# File 'lib/postageapp/response.rb', line 11 def uid @uid end |