Sha256: 1ac4180cf513e600bd62bd11df5d97aa0bd7a15b8aecc16fd2846d6707109f22

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

class PostageApp::Response
  # The UID should match the Request's UID. If Request didn't provide with one
  # PostageApp service should generate it for the Response
  attr_reader :uid
  
  # The status of the response in string format (like: ok, bad_request)
  # Will be set to +fail+ if Request times out
  attr_reader :status
  
  # The message of the response. Could be used as an error explanation.
  attr_reader :message
  
  # The data payload of the response. This is usually the return value of the
  # request we're looking for
  attr_reader :data
  
  # Takes in Net::HTTPResponse object as the attribute.
  # If something goes wrong Response will be thought of as failed
  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
  
  # Little helper that checks for the Response status
  #   => @response.ok?
  #   >> true
  #   => @response.fail?
  #   >> false
  def method_missing(method)
    /.*?\?$/.match(method.to_s) ? "#{self.status}?" == method.to_s : super(method)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
postageapp-1.2.0 lib/postageapp/response.rb