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

Returns a new instance of Resource.



11
12
13
# File 'lib/cin7_api/resource.rb', line 11

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

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



15
16
17
# File 'lib/cin7_api/resource.rb', line 15

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

#handle_response(response) ⇒ Object



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

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



19
20
21
# File 'lib/cin7_api/resource.rb', line 19

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

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



23
24
25
# File 'lib/cin7_api/resource.rb', line 23

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