class PostageApp::Response

Constants

STATUS_FAIL
STATUS_TIMEOUT

Constants ============================================================

Attributes

data[R]

The data payload of the response. This is usually the return value of the request we're looking for

exception[R]
message[R]

The message of the response. Could be used as an error explanation.

status[R]

The status of the response in string format (like: ok, bad_request) Will be set to fail if Request times out

uid[R]

The UID should match the Request's UID. If Request didn't provide with one PostageApp service should generate it for the Response

Public Class Methods

new(http_response) click to toggle source

Takes in Net::HTTPResponse object as the attribute. If something goes wrong Response will be thought of as failed

# 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

Public Instance Methods

method_missing(method) click to toggle source

Little helper that checks for the Response status

=> @response.ok?
>> true
=> @response.fail?
>> false
Calls superclass method
# File lib/postageapp/response.rb, line 56
def method_missing(method)
  /.*?\?$/.match(method.to_s) ? "#{self.status}?" == method.to_s : super(method)
end