Class: DaisybillApi::Data::Client

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

Defined Under Namespace

Classes: BasicError, InternalServerError, InvalidParams, 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.



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

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: { 'Content-Type' => 'application/json' }
  }
  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.



21
22
23
# File 'lib/daisybill_api/data/client.rb', line 21

def headers
  @headers
end

#requestObject (readonly)

Returns the value of attribute request.



21
22
23
# File 'lib/daisybill_api/data/client.rb', line 21

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



21
22
23
# File 'lib/daisybill_api/data/client.rb', line 21

def response
  @response
end

Class Method Details

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



13
14
15
16
17
18
19
# 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?
  client
end

Instance Method Details

#bad_request?Boolean

Returns:

  • (Boolean)


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

def bad_request?
  status == '400'
end

#error?Boolean

Returns:

  • (Boolean)


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

def error?
  status == '500'
end

#forbidden?Boolean

Returns:

  • (Boolean)


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

def forbidden?
  status == '403'
end

#not_found?Boolean

Returns:

  • (Boolean)


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

def not_found?
  status == '404'
end

#statusObject



43
44
45
# File 'lib/daisybill_api/data/client.rb', line 43

def status
  @status.code
end

#success?Boolean

Returns:

  • (Boolean)


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

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

#unauthorized?Boolean

Returns:

  • (Boolean)


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

def unauthorized?
  status == '401'
end