Class: BaseCRM::ProductsService
- Inherits:
-
Object
- Object
- BaseCRM::ProductsService
- Defined in:
- lib/basecrm/services/products_service.rb
Constant Summary collapse
- OPTS_KEYS_TO_PERSIST =
Set[:name, :description, :sku, :active, :cost, :cost_currency, :prices, :max_discount, :max_markup]
Instance Method Summary collapse
-
#all ⇒ Enumerable
Retrieve all products.
-
#create(product) ⇒ Product
Create a product.
-
#destroy(id) ⇒ Boolean
Delete a product.
-
#find(id) ⇒ Product
Retrieve a single product.
-
#initialize(client) ⇒ ProductsService
constructor
A new instance of ProductsService.
-
#update(product) ⇒ Product
Update a product.
-
#where(options = {}) ⇒ Array<Product>
Retrieve all products.
Constructor Details
#initialize(client) ⇒ ProductsService
Returns a new instance of ProductsService.
7 8 9 |
# File 'lib/basecrm/services/products_service.rb', line 7 def initialize(client) @client = client end |
Instance Method Details
#all ⇒ Enumerable
Retrieve all products
get ‘/products’
If you want to use filtering or sorting (see #where).
17 18 19 |
# File 'lib/basecrm/services/products_service.rb', line 17 def all PaginatedResource.new(self) end |
#create(product) ⇒ Product
Create a product
post ‘/products’
Create a new product
53 54 55 56 57 58 59 60 |
# File 'lib/basecrm/services/products_service.rb', line 53 def create(product) validate_type!(product) attributes = sanitize(product) _, _, root = @client.post("/products", attributes) Product.new(root[:data]) end |
#destroy(id) ⇒ Boolean
Delete a product
delete ‘/products/BaseCRM#id’
Delete an existing product from the catalog Existing orders and line items are not affected If the specified product does not exist, the request will return an error This operation cannot be undone Products can be removed only by an account administrator
115 116 117 118 |
# File 'lib/basecrm/services/products_service.rb', line 115 def destroy(id) status, _, _ = @client.delete("/products/#{id}") status == 204 end |
#find(id) ⇒ Product
Retrieve a single product
get ‘/products/BaseCRM#id’
Returns a single product, according to the unique product ID provided If the specified product does not exist, the request will return an error
72 73 74 75 76 |
# File 'lib/basecrm/services/products_service.rb', line 72 def find(id) _, _, root = @client.get("/products/#{id}") Product.new(root[:data]) end |
#update(product) ⇒ Product
Update a product
put ‘/products/BaseCRM#id’
Updates product information If the specified product does not exist, the request will return an error <figure class=“notice”><p>In order to modify prices used on a record, you need to supply the entire set prices
are replaced every time they are used in a request </p></figure>
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/basecrm/services/products_service.rb', line 91 def update(product) validate_type!(product) params = extract_params!(product, :id) id = params[:id] attributes = sanitize(product) _, _, root = @client.put("/products/#{id}", attributes) Product.new(root[:data]) end |
#where(options = {}) ⇒ Array<Product>
Retrieve all products
get ‘/products’
Returns all products available to the user according to the parameters provided
38 39 40 41 42 |
# File 'lib/basecrm/services/products_service.rb', line 38 def where( = {}) _, _, root = @client.get("/products", ) root[:items].map{ |item| Product.new(item[:data]) } end |