Class: Prodigi::Resource Abstract

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

Overview

This class is abstract.

Subclass and add resource-specific methods

Base class for all API resource classes

Provides common HTTP request methods (GET, POST, PATCH, PUT, DELETE) and response handling for interacting with the Prodigi API. All specific resource classes (OrderResource, QuoteResource, ProductResource) inherit from this class.

This class handles:

  • HTTP request construction with proper headers

  • Response parsing and error handling

  • Debug logging when enabled

  • Authentication via API key headers

Examples:

Subclassing (internal use)

class OrderResource < Resource
  def list(**params)
    response = get_request("orders", params: params)
    Collection.from_response(response, key: "orders", type: Order)
  end
end

Direct Known Subclasses

OrderResource, ProductResource, QuoteResource

Constant Summary collapse

ERROR_MAP =
{
  400 => [BadRequestError, "Bad request: the request is malformed."],
  401 => [UnauthorizedError, "Unauthorised: credentials missing or incorrect."],
  403 => [ForbiddenError, nil],
  404 => [NotFoundError, "Resource does not exist."],
  429 => [RateLimitError, nil]
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Resource

Returns a new instance of Resource.



39
40
41
# File 'lib/prodigi/resource.rb', line 39

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientProdigi::Client (readonly)

The API client instance

Returns:



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

def client
  @client
end

Instance Method Details

#default_headersObject



63
64
65
# File 'lib/prodigi/resource.rb', line 63

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

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



59
60
61
# File 'lib/prodigi/resource.rb', line 59

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

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



43
44
45
# File 'lib/prodigi/resource.rb', line 43

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

#handle_response(response) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/prodigi/resource.rb', line 67

def handle_response(response)
  client.logger.debug(response.pretty_inspect) if client.debug

  raise_error_if_needed(response)

  response
end

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



51
52
53
# File 'lib/prodigi/resource.rb', line 51

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

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



47
48
49
# File 'lib/prodigi/resource.rb', line 47

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

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



55
56
57
# File 'lib/prodigi/resource.rb', line 55

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