class DaisybillApi::Data::Client

Attributes

headers[R]
request[R]
response[R]

Public Class Methods

build(method, path, params = {}) click to toggle source
# File lib/daisybill_api/data/client.rb, line 13
def self.build(method, path, params = {})
  client = new method, path, params
  raise InternalServerError.new(client.response["error"]) if client.error?
  raise UnauthorizedError.new(client.response["error"]) if client.unauthorized?
  raise InvalidParams.new(client.response["error"]) if client.forbidden?
  raise MethodNotAllowed.new(client.response["error"]) if client.method_not_allowed?
  client
end
new(method, path, params = {}) click to toggle source
# File lib/daisybill_api/data/client.rb, line 24
def initialize(method, path, params = {})
  DaisybillApi.logger.info "#{method.to_s.upcase} #{path}"
  DaisybillApi.logger.debug params.inspect
  url = DaisybillApi::Data::Url.build(path).to_s
  data = {
    method: method,
    url: url,
    payload: params
  }
  RestClient::Request.execute(data) { |response, request, status|
    @headers = response.headers
    @response = JSON.parse response
    @request = request
    @status = status
    DaisybillApi.logger.info "Response status #{self.status}"
    DaisybillApi.logger.debug @response.inspect
  }
end

Public Instance Methods

bad_request?() click to toggle source
# File lib/daisybill_api/data/client.rb, line 51
def bad_request?
  status == "400"
end
error?() click to toggle source
# File lib/daisybill_api/data/client.rb, line 71
def error?
  status == "500"
end
forbidden?() click to toggle source
# File lib/daisybill_api/data/client.rb, line 63
def forbidden?
  status == "403"
end
method_not_allowed?() click to toggle source
# File lib/daisybill_api/data/client.rb, line 67
def method_not_allowed?
  status == "405"
end
not_found?() click to toggle source
# File lib/daisybill_api/data/client.rb, line 59
def not_found?
  status == "404"
end
status() click to toggle source
# File lib/daisybill_api/data/client.rb, line 43
def status
  @status.code
end
success?() click to toggle source
# File lib/daisybill_api/data/client.rb, line 47
def success?
  status == "200" || status == "201"
end
unauthorized?() click to toggle source
# File lib/daisybill_api/data/client.rb, line 55
def unauthorized?
  status == "401"
end