class SpreeClient::API::V1::Resources

Attributes

api[R]

Spree API client instance. @return [SpreeClient::API::V1]

default_args[R]

@return [Hash]

response[R]

Every method caches the response so it can be inspected afterwards. @return [HTTParty::Response] The API response

Public Class Methods

new(**args) click to toggle source

Initialize

@param [SpreeClient::API::V1] :api An API client instance

   # File lib/spree_client/api/v1/resources.rb
19 def initialize(**args)
20   @api = args.delete :api
21   @default_args = args
22 end

Public Instance Methods

create(resource) click to toggle source

Creates a resource

@see SpreeClient::API::V1#headers @param [Struct,Hash] Model instance @return [Boolean]

   # File lib/spree_client/api/v1/resources.rb
65 def create(resource)
66   resource = new **resource unless resource.is_a? resource_class
67 
68   @response = api.class.post endpoint(resource),
69                              body: params(resource),
70                              headers: api.headers
71 
72   response.created?
73 end
delete(resource) click to toggle source

Deletes a resource

@see SpreeClient::API::V1#headers @param [Struct,Hash] @return [Boolean]

   # File lib/spree_client/api/v1/resources.rb
95 def delete(resource)
96   @response = spree.class.delete endpoint(resource), headers: api.headers
97 
98   response.no_content?
99 end
index(**q) click to toggle source

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

@param [Hash] Query params

   # File lib/spree_client/api/v1/resources.rb
45 def index(**q)
46   @response = api.class.get endpoint(q),
47                             query: q.slice(:ids, :q, :page, :per_page),
48                             headers: api.headers
49 
50   response.ok?
51 end
new(**args) click to toggle source

Initialize a new resource

@param [Hash] Attributes @return [Struct]

   # File lib/spree_client/api/v1/resources.rb
28 def new(**args)
29   resource_class.new **args
30 end
show(resource) click to toggle source

Get a single product by ID

   # File lib/spree_client/api/v1/resources.rb
54 def show(resource)
55   @response = api.class.get endpoint(resource), headers: api.headers
56 
57   response.ok?
58 end
update(resource) click to toggle source

Updates a resource

@see SpreeClient::API::V1#headers @param [Struct,Hash] @return [Boolean]

   # File lib/spree_client/api/v1/resources.rb
80 def update(resource)
81   resource = new resource unless resource.is_a? resource_class
82 
83   @response = api.class.patch endpoint(resource),
84                               body: params(resource),
85                               headers: api.headers
86 
87   response.ok?
88 end

Private Instance Methods

endpoint(resource = {}) click to toggle source

Backend endpoint

@param [Struct] @return [String]

    # File lib/spree_client/api/v1/resources.rb
116 def endpoint(resource = {})
117   endpoint = self.class::ENDPOINT + (resource.dig(:id) ? '/:id' : '')
118 
119   parameterize_with endpoint, resource
120 end
parameterize_with(endpoint, resource = {}) click to toggle source

Generates a URL with :parameter replaced with values

@param [String] @param [Struct,Hash]

    # File lib/spree_client/api/v1/resources.rb
126 def parameterize_with(endpoint, resource = {})
127   default_args.merge(resource.to_h.compact).inject(endpoint) do |e, pair|
128     e.gsub ':' + pair.first.to_s, pair.last.to_s
129   end + '.json'
130 end
params(resource = nil) click to toggle source

Strong parameters

@see SpreeClient#authenticity_token @param [Struct,Nil] @return [Hash]

    # File lib/spree_client/api/v1/resources.rb
108 def params(resource = nil)
109   { resource_name => default_args.merge(resource.to_h.compact) }.to_json
110 end
resource_class() click to toggle source
    # File lib/spree_client/api/v1/resources.rb
132 def resource_class
133   self.class::RESOURCE
134 end
resource_name() click to toggle source
    # File lib/spree_client/api/v1/resources.rb
136 def resource_name
137   self.class::NAME
138 end