Class: DaisybillApi::Data::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/daisybill_api/data/client.rb

Defined Under Namespace

Classes: BasicError, InternalServerError, InvalidParams, MethodNotAllowed, UnauthorizedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, path, params = {}) ⇒ Client

Returns a new instance of Client.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/daisybill_api/data/client.rb', line 25

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,
    headers: {
      "Authorization" => "Bearer #{DaisybillApi.configuration.api_token}",
      "User-Agent" => "DaisyBill_API/#{DaisybillApi::VERSION}",
    }
  }
  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

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



23
24
25
# File 'lib/daisybill_api/data/client.rb', line 23

def headers
  @headers
end

#requestObject (readonly)

Returns the value of attribute request.



23
24
25
# File 'lib/daisybill_api/data/client.rb', line 23

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



23
24
25
# File 'lib/daisybill_api/data/client.rb', line 23

def response
  @response
end

Class Method Details

.build(method, path, params = {}) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/daisybill_api/data/client.rb', line 14

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

Instance Method Details

#bad_request?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/daisybill_api/data/client.rb', line 56

def bad_request?
  status == "400"
end

#error?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/daisybill_api/data/client.rb', line 76

def error?
  status == "500"
end

#forbidden?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/daisybill_api/data/client.rb', line 68

def forbidden?
  status == "403"
end

#method_not_allowed?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/daisybill_api/data/client.rb', line 72

def method_not_allowed?
  status == "405"
end

#not_found?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/daisybill_api/data/client.rb', line 64

def not_found?
  status == "404"
end

#statusObject



48
49
50
# File 'lib/daisybill_api/data/client.rb', line 48

def status
  @status.code
end

#success?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/daisybill_api/data/client.rb', line 52

def success?
  status == "200" || status == "201"
end

#unauthorized?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/daisybill_api/data/client.rb', line 60

def unauthorized?
  status == "401"
end