Class: PostageApp::Response
- Inherits:
-
Object
- Object
- PostageApp::Response
- Defined in:
- lib/postageapp/response.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
The data payload of the response.
-
#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
20 21 22 23 24 25 26 27 28 |
# File 'lib/postageapp/response.rb', line 20 def initialize(http_response) hash = JSON::parse(http_response.body) @status = hash['response']['status'] @uid = hash['response']['uid'] @message = hash['response']['message'] @data = hash['data'] rescue @status = 'fail' 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
35 36 37 |
# File 'lib/postageapp/response.rb', line 35 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
16 17 18 |
# File 'lib/postageapp/response.rb', line 16 def data @data end |
#message ⇒ Object (readonly)
The message of the response. Could be used as an error explanation.
12 13 14 |
# File 'lib/postageapp/response.rb', line 12 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
9 10 11 |
# File 'lib/postageapp/response.rb', line 9 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
5 6 7 |
# File 'lib/postageapp/response.rb', line 5 def uid @uid end |