Class: BaseCRM::ProductsService

Inherits:
Object
  • Object
show all
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

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

#allEnumerable

Retrieve all products

get ‘/products’

If you want to use filtering or sorting (see #where).

Returns:

  • (Enumerable)

    Paginated resource you can use to iterate over all the resources.



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

Parameters:

  • product (Product, Hash)

    Either object of the Product type or Hash. This object’s attributes describe the object to be created.

Returns:

  • (Product)

    The resulting object represting created resource.



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

Parameters:

  • id (Integer)

    Unique identifier of a Product

Returns:

  • (Boolean)

    Status of the operation.



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

Parameters:

  • id (Integer)

    Unique identifier of a Product

Returns:

  • (Product)

    Searched resource object.



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>

Parameters:

  • product (Product, Hash)

    Either object of the Product type or Hash. This object’s attributes describe the object to be updated.

Returns:

  • (Product)

    The resulting object represting updated resource.



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

Parameters:

  • options (Hash) (defaults to: {})

    Search options

Options Hash (options):

  • :ids (String)

    Comma-separated list of product IDs to be returned in a request.

  • :organization_id (Integer)

    Unique identifier of an organization.

  • :owner_id (Integer)

    Unique identifier of the user the product is owned by. Returns all products owned by the user.

  • :page (Integer) — default: 1

    Page number to start from. Page numbering starts at 1, and omitting the ‘page` parameter will return the first page.

  • :per_page (Integer) — default: 25

    Number of records to return per page. Default limit is 25 and the maximum number that can be returned is 100.

  • :sort_by (String) — default: id:asc

    A field to sort by. Default ordering is ascending. If you want to change the sort ordering to descending, append ‘:desc` to the field e.g. `sort_by=value:desc`.

  • :name (String)

    Name of the product.

  • :sku (String)

    SKU of the product.

  • :active (Boolean)

    Indicator of whether or not the product is active.

Returns:

  • (Array<Product>)

    The list of Products for the first page, unless otherwise specified.



38
39
40
41
42
# File 'lib/basecrm/services/products_service.rb', line 38

def where(options = {})
  _, _, root = @client.get("/products", options)

  root[:items].map{ |item| Product.new(item[:data]) }
end