Class: Cin7API::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/cin7_api/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Resource



9
10
11
# File 'lib/cin7_api/resource.rb', line 9

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/cin7_api/resource.rb', line 7

def client
  @client
end

Instance Method Details

#get_request(url, params: {}, headers: {}) ⇒ Object



13
14
15
# File 'lib/cin7_api/resource.rb', line 13

def get_request(url, params: {}, headers: {})
  handle_response client.connection.get(url, parsed_params(params), headers)
end

#handle_response(response) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cin7_api/resource.rb', line 25

def handle_response(response)
  error_message = response.body

  case response.status
  when 400
    raise Error, "A bad request or a validation exception has occurred. #{error_message}"
  when 401
    raise AuthenticationError, "Invalid authorization credentials. #{error_message}"
  when 403
    raise PermissionError, "Connection doesn't have permission to access the resource. #{error_message}"
  when 404
    raise Error, "The resource you have specified cannot be found. #{error_message}"
  when 429
    raise RateLimitError,
      "The API rate limit for your application has been exceeded. #{error_message}"
  when 500
    raise ServerError,
      "An unhandled error with the Cin7 API. Contact the Cin7 API team if problems persist. #{error_message}"
  when 503
    raise Error,
      "API is currently unavailable – typically due to a scheduled outage – try again soon. #{error_message}"
  end

  response
end

#post_request(url, body:, headers: {}) ⇒ Object



17
18
19
# File 'lib/cin7_api/resource.rb', line 17

def post_request(url, body:, headers: {})
  handle_response client.connection.post(url, body, headers)
end

#put_request(url, body:, headers: {}) ⇒ Object



21
22
23
# File 'lib/cin7_api/resource.rb', line 21

def put_request(url, body:, headers: {})
  handle_response client.connection.put(url, body, headers)
end