Class: BaseCRM::LineItemsService

Inherits:
Object
  • Object
show all
Defined in:
lib/basecrm/services/line_items_service.rb

Constant Summary collapse

OPTS_KEYS_TO_PERSIST =
Set[:product_id, :value, :variation, :currency, :quantity]

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ LineItemsService

Returns a new instance of LineItemsService.



7
8
9
# File 'lib/basecrm/services/line_items_service.rb', line 7

def initialize(client)
  @client = client
end

Instance Method Details

#all(order_id) ⇒ Enumerable

Retrieve order’s line items

get ‘/orders/order_id/line_items’

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/line_items_service.rb', line 17

def all(order_id)
  PaginatedResource.new(self, order_id)
end

#create(order_id, line_item) ⇒ LineItem

Create a line item

post ‘/orders/order_id/line_items’

Adds a new line item to an existing order Line items correspond to products in the catalog, so first you must create products Because products allow defining different prices in different currencies, when creating a line item, the parameter currency is required

Parameters:

  • order_id (Integer)

    Unique identifier of a Order

  • line_item (LineItem, Hash)

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

Returns:

  • (LineItem)

    The resulting object represting created resource.



54
55
56
57
58
59
60
61
# File 'lib/basecrm/services/line_items_service.rb', line 54

def create(order_id, line_item)
  validate_type!(line_item)

  attributes = sanitize(line_item)
  _, _, root = @client.post("/orders/#{order_id}/line_items", attributes)

  LineItem.new(root[:data])
end

#destroy(order_id, id) ⇒ Boolean

Delete a line item

delete ‘/orders/order_id/line_items/BaseCRM#id

Remove an order’s line item This operation cannot be undone

Parameters:

  • order_id (Integer)

    Unique identifier of a Order

  • id (Integer)

    Unique identifier of a LineItem

Returns:

  • (Boolean)

    Status of the operation.



90
91
92
93
# File 'lib/basecrm/services/line_items_service.rb', line 90

def destroy(order_id, id)
  status, _, _ = @client.delete("/orders/#{order_id}/line_items/#{id}")
  status == 204
end

#find(order_id, id) ⇒ LineItem

Retrieve a single line item

get ‘/orders/order_id/line_items/BaseCRM#id

Returns a single line item of an order, according to the unique line item ID provided

Parameters:

  • order_id (Integer)

    Unique identifier of a Order

  • id (Integer)

    Unique identifier of a LineItem

Returns:

  • (LineItem)

    Searched resource object.



73
74
75
76
77
# File 'lib/basecrm/services/line_items_service.rb', line 73

def find(order_id, id)
  _, _, root = @client.get("/orders/#{order_id}/line_items/#{id}")

  LineItem.new(root[:data])
end

#where(order_id, options = {}) ⇒ Array<LineItem>

Retrieve order’s line items

get ‘/orders/order_id/line_items’

Returns all line items associated to order

Parameters:

  • order_id (Integer)

    Unique identifier of a Order

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

    Search options

Options Hash (options):

  • :ids (String)

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

  • :quantity (Integer)

    Quantity of line item.

  • :value (Integer)

    Value of line item.

  • :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`.

Returns:

  • (Array<LineItem>)

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



36
37
38
39
40
# File 'lib/basecrm/services/line_items_service.rb', line 36

def where(order_id, options = {})
  _, _, root = @client.get("/orders/#{order_id}/line_items", options)

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