Class: SpreeClient::API::V1::Resources
- Inherits:
-
Object
- Object
- SpreeClient::API::V1::Resources
- Defined in:
- lib/spree_client/api/v1/resources.rb
Direct Known Subclasses
Products, Properties, StockItems, StockLocations, StockMovements, Taxonomies, Variants
Instance Attribute Summary collapse
-
#api ⇒ SpreeClient::API::V1
readonly
Spree API client instance.
- #default_args ⇒ Hash readonly
-
#response ⇒ HTTParty::Response
readonly
Every method caches the response so it can be inspected afterwards.
Instance Method Summary collapse
-
#create(resource) ⇒ Boolean
Creates a resource.
-
#delete(resource) ⇒ Boolean
Deletes a resource.
-
#index(**q) ⇒ Object
Gets all products or filter with params.
-
#initialize(**args) ⇒ Resources
constructor
Initialize.
-
#new(**args) ⇒ Struct
Initialize a new resource.
-
#show(resource) ⇒ Object
Get a single product by ID.
-
#update(resource) ⇒ Boolean
Updates a resource.
Constructor Details
#initialize(**args) ⇒ Resources
Initialize
19 20 21 22 |
# File 'lib/spree_client/api/v1/resources.rb', line 19 def initialize(**args) @api = args.delete :api @default_args = args end |
Instance Attribute Details
#api ⇒ SpreeClient::API::V1 (readonly)
Spree API client instance.
9 10 11 |
# File 'lib/spree_client/api/v1/resources.rb', line 9 def api @api end |
#default_args ⇒ Hash (readonly)
14 15 16 |
# File 'lib/spree_client/api/v1/resources.rb', line 14 def default_args @default_args end |
#response ⇒ HTTParty::Response (readonly)
Every method caches the response so it can be inspected afterwards.
12 13 14 |
# File 'lib/spree_client/api/v1/resources.rb', line 12 def response @response end |
Instance Method Details
#create(resource) ⇒ Boolean
Creates a resource
65 66 67 68 69 70 71 72 73 |
# File 'lib/spree_client/api/v1/resources.rb', line 65 def create(resource) resource = new(**resource) unless resource.is_a? resource_class @response = api.class.post endpoint(resource), body: params(resource), headers: api.headers response.created? end |
#delete(resource) ⇒ Boolean
Deletes a resource
95 96 97 98 99 |
# File 'lib/spree_client/api/v1/resources.rb', line 95 def delete(resource) @response = spree.class.delete endpoint(resource), headers: api.headers response.no_content? end |
#index(**q) ⇒ Object
Gets all products or filter with params
Filters:
ids: comma-separated list of IDs q: Ransack search params (mutually exclusive with ids)
Pagination:
page: page number per_page: results per page
45 46 47 48 49 50 51 |
# File 'lib/spree_client/api/v1/resources.rb', line 45 def index(**q) @response = api.class.get endpoint(q), query: q.slice(:ids, :q, :page, :per_page), headers: api.headers response.ok? end |
#new(**args) ⇒ Struct
Initialize a new resource
28 29 30 |
# File 'lib/spree_client/api/v1/resources.rb', line 28 def new(**args) resource_class.new(**args) end |
#show(resource) ⇒ Object
Get a single product by ID
54 55 56 57 58 |
# File 'lib/spree_client/api/v1/resources.rb', line 54 def show(resource) @response = api.class.get endpoint(resource), headers: api.headers response.ok? end |
#update(resource) ⇒ Boolean
Updates a resource
80 81 82 83 84 85 86 87 88 |
# File 'lib/spree_client/api/v1/resources.rb', line 80 def update(resource) resource = new(**resource) unless resource.is_a? resource_class @response = api.class.patch endpoint(resource), body: params(resource), headers: api.headers response.ok? end |