Class: Prodigi::Resource

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

Direct Known Subclasses

OrderResource, ProductResource, QuoteResource

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Resource

Returns a new instance of Resource.



5
6
7
# File 'lib/prodigi/resource.rb', line 5

def initialize(client) 
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/prodigi/resource.rb', line 3

def client
  @client
end

Instance Method Details

#default_headersObject



29
30
31
# File 'lib/prodigi/resource.rb', line 29

def default_headers
  { "X-API-Key": client.api_key }
end

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



25
26
27
# File 'lib/prodigi/resource.rb', line 25

def delete_request(url, params: {}, headers: {})
  handle_response client.connection.delete(url, params, default_headers.merge(headers))
end

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



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

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

#handle_response(response) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/prodigi/resource.rb', line 33

def handle_response(response)
  message = response.body["statusText"]
  case response.status
  when 400
    raise Error, "Bad request: the request is malformed in some manner. #{message}"
  when 401
    raise Error, "Unauthorised: your credentials are missing or incorrect. #{message}"
  when 403
    raise Error, message
  when 404
    raise Error, "Not found: the resource does not exist (or does not exist in your account). #{message}"
  when 429
    raise Error, message
  when 500
    raise Error, "Internal server error: Please contact [email protected]. #{message}"
  end

  response
end

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



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

def patch_request(url, body:, headers: {})
  handle_response client.connection.patch(url, body, default_headers.merge(headers))
end

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



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

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

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



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

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