Class: Booqable::ResourceProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/booqable/resource_proxy.rb

Overview

Generic resource proxy for API collections

Provides a uniform interface for interacting with API resources using standard CRUD operations. Each resource proxy handles the JSON:API formatting and delegates HTTP requests to the underlying client.

Examples:

Working with orders

orders = Booqable::ResourceProxy.new(client, "orders")

# Of course, you can also just use Booqable.orders directly.

# List all orders
all_orders = orders.list

# Find a specific order
order = orders.find("123")

# Create a new order
new_order = orders.create(starts_at: "2024-01-01T00:00:00Z", stops_at: "2024-01-02T00:00:00Z", status: "draft")

# Update an existing order
updated_order = orders.update("123", status: "reserved")

# Delete an existing order
orders.delete("123")

Instance Method Summary collapse

Constructor Details

#initialize(client, resource_name) ⇒ ResourceProxy

Initialize a new resource proxy

Parameters:

  • client (Booqable::Client)

    The client instance to use for requests

  • resource_name (String, Symbol)

    The name of the resource (e.g., “orders”, “customers”)



34
35
36
37
# File 'lib/booqable/resource_proxy.rb', line 34

def initialize(client, resource_name)
  @client = client
  @resource = resource_name.to_s
end

Instance Method Details

#all(params = {}) ⇒ Object

Alias for list

See Also:



64
65
66
# File 'lib/booqable/resource_proxy.rb', line 64

def all(params = {})
  list(params)
end

#create(attrs = {}) ⇒ Hash

Create a new resource

Creates a new resource with the provided attributes using JSON:API format.

Examples:

Create a new order

new_order = orders.create(
  starts_at: "2024-01-01T00:00:00Z",
  stops_at: "2024-01-02T00:00:00Z",
  status: "draft"
)

Parameters:

  • attrs (Hash) (defaults to: {})

    Attributes for the new resource

Returns:

  • (Hash)

    The created resource data

Raises:



99
100
101
102
# File 'lib/booqable/resource_proxy.rb', line 99

def create(attrs = {})
  response = request :post, @resource, { data: { type: @resource, attributes: attrs } }
  response.data
end

#delete(id) ⇒ Object

Delete an existing resource

Deletes a resource by its unique identifier.

Examples:

Delete an order

deleted_order = orders.delete("123")

Parameters:

  • id (String, Integer)

    The unique identifier of the resource to delete

Returns:

  • (Object)

    The deleted resource data

Raises:



131
132
133
134
# File 'lib/booqable/resource_proxy.rb', line 131

def delete(id)
  response = request :delete, "#{@resource}/#{id}", {}
  response.data
end

#find(id, params = {}) ⇒ Hash

Find a specific resource by ID

Retrieves a single resource by its unique identifier.

Examples:

Find an order by ID

order = orders.find("123", include: "customer,items")

Parameters:

  • id (String, Integer)

    The unique identifier of the resource

  • params (Hash) (defaults to: {})

    Additional query parameters

Options Hash (params):

  • :include (String)

    Related resources to include

Returns:

  • (Hash)

    The resource data

Raises:



80
81
82
83
# File 'lib/booqable/resource_proxy.rb', line 80

def find(id, params = {})
  response = request :get, "#{@resource}/#{id}", params
  response.data
end

#list(params = {}) ⇒ Array, Enumerator

List all resources

Retrieves a collection of resources with optional filtering and pagination. Uses the JSON:API specification for query parameters.

Examples:

List orders with filters

orders.list(
  include: "customer",
  filter: { status: "active" },
  sort: "-created_at"
)

Parameters:

  • params (Hash) (defaults to: {})

    Query parameters for filtering, sorting, and pagination

Options Hash (params):

  • :include (String)

    Related resources to include (e.g., “customer,items”)

  • :filter (Hash)

    Filter criteria (e.g., { status: “active” })

  • :sort (String)

    Sort criteria (e.g., “created_at”, “-updated_at”)

  • :page (Hash)

    Pagination parameters (e.g., { number: 1, size: 25 })

Returns:

  • (Array, Enumerator)

    Collection of resources or enumerator for auto-pagination



57
58
59
# File 'lib/booqable/resource_proxy.rb', line 57

def list(params = {})
  paginate @resource, params
end

#update(id, attrs = {}) ⇒ Hash

Update an existing resource

Updates a resource with the provided attributes using JSON:API format.

Examples:

Update an order

updated_order = orders.update("123", status: "reserved")

Parameters:

  • id (String, Integer)

    The unique identifier of the resource to update

  • attrs (Hash) (defaults to: {})

    Attributes to update

Returns:

  • (Hash)

    The updated resource data

Raises:



116
117
118
119
# File 'lib/booqable/resource_proxy.rb', line 116

def update(id, attrs = {})
  response = request :put, "#{@resource}/#{id}", { data: { type: @resource, id: id, attributes: attrs } }
  response.data
end