Class: PostageApp::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/postageapp/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/postageapp/response.rb', line 19

def initialize(http_response)
  hash = JSON::parse(http_response.body)

  _response = hash['response']

  @status = _response['status']
  @uid = _response['uid']
  @message = _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


39
40
41
# File 'lib/postageapp/response.rb', line 39

def method_missing(method)
  /.*?\?$/.match(method.to_s) ? "#{self.status}?" == method.to_s : super(method)
end

Instance Attribute Details

#dataObject (readonly)

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



15
16
17
# File 'lib/postageapp/response.rb', line 15

def data
  @data
end

#messageObject (readonly)

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



11
12
13
# File 'lib/postageapp/response.rb', line 11

def message
  @message
end

#statusObject (readonly)

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



8
9
10
# File 'lib/postageapp/response.rb', line 8

def status
  @status
end

#uidObject (readonly)

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



4
5
6
# File 'lib/postageapp/response.rb', line 4

def uid
  @uid
end