Class: SpreeClient::API::V1::Resources

Inherits:
Object
  • Object
show all
Defined in:
lib/spree_client/api/v1/resources.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Resources

Initialize

Parameters:



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

#apiSpreeClient::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_argsHash (readonly)

Returns:

  • (Hash)


14
15
16
# File 'lib/spree_client/api/v1/resources.rb', line 14

def default_args
  @default_args
end

#responseHTTParty::Response (readonly)

Every method caches the response so it can be inspected afterwards.

Returns:

  • (HTTParty::Response)

    The API response



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

Parameters:

  • Model (Struct, Hash)

    instance

Returns:

  • (Boolean)

See Also:



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

Parameters:

  • (Struct, Hash)

Returns:

  • (Boolean)

See Also:



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

Parameters:

  • Query (Hash)

    params



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

Parameters:

  • Attributes (Hash)

Returns:

  • (Struct)


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

Parameters:

  • (Struct, Hash)

Returns:

  • (Boolean)

See Also:



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