Class: Booqable::ResourceProxy
- Inherits:
-
Object
- Object
- Booqable::ResourceProxy
- 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.
Instance Method Summary collapse
-
#all(params = {}) ⇒ Object
Alias for list.
-
#create(attrs = {}) ⇒ Hash
Create a new resource.
-
#delete(id) ⇒ Object
Delete an existing resource.
-
#find(id, params = {}) ⇒ Hash
Find a specific resource by ID.
-
#initialize(client, resource_name) ⇒ ResourceProxy
constructor
Initialize a new resource proxy.
-
#list(params = {}) ⇒ Array, Enumerator
List all resources.
-
#update(id, attrs = {}) ⇒ Hash
Update an existing resource.
Constructor Details
#initialize(client, resource_name) ⇒ ResourceProxy
Initialize a new resource proxy
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
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.
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.
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.
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.
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.
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 |