Class: BaseCRM::OrdersService

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

Constant Summary collapse

OPTS_KEYS_TO_PERSIST =
Set[:deal_id, :discount]

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ OrdersService

Returns a new instance of OrdersService.



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

def initialize(client)
  @client = client
end

Instance Method Details

#allEnumerable

Retrieve all orders

get ‘/orders’

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

def all
  PaginatedResource.new(self)
end

#create(order) ⇒ Order

Create an order

post ‘/orders’

Create a new order for a deal User needs to have access to the deal to create an order Each deal can have at most one order and error is returned when attempting to create more

Parameters:

  • order (Order, Hash)

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

Returns:

  • (Order)

    The resulting object represting created resource.



51
52
53
54
55
56
57
58
# File 'lib/basecrm/services/orders_service.rb', line 51

def create(order)
  validate_type!(order)

  attributes = sanitize(order)
  _, _, root = @client.post("/orders", attributes)

  Order.new(root[:data])
end

#destroy(id) ⇒ Boolean

Delete an order

delete ‘/orders/BaseCRM#id

Delete an existing order and remove all of the associated line items in a single call If the specified order does not exist, the request will return an error This operation cannot be undone

Parameters:

  • id (Integer)

    Unique identifier of a Order

Returns:

  • (Boolean)

    Status of the operation.



108
109
110
111
# File 'lib/basecrm/services/orders_service.rb', line 108

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

#find(id) ⇒ Order

Retrieve a single order

get ‘/orders/BaseCRM#id

Returns a single order available to the user, according to the unique order ID provided If the specified order does not exist, the request will return an error

Parameters:

  • id (Integer)

    Unique identifier of a Order

Returns:

  • (Order)

    Searched resource object.



70
71
72
73
74
# File 'lib/basecrm/services/orders_service.rb', line 70

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

  Order.new(root[:data])
end

#update(order) ⇒ Order

Update an order

put ‘/orders/BaseCRM#id

Updates order information If the specified order does not exist, the request will return an error

Parameters:

  • order (Order, Hash)

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

Returns:

  • (Order)

    The resulting object represting updated resource.



86
87
88
89
90
91
92
93
94
95
# File 'lib/basecrm/services/orders_service.rb', line 86

def update(order)
  validate_type!(order)
  params = extract_params!(order, :id)
  id = params[:id]

  attributes = sanitize(order)
  _, _, root = @client.put("/orders/#{id}", attributes)

  Order.new(root[:data])
end

#where(options = {}) ⇒ Array<Order>

Retrieve all orders

get ‘/orders’

Returns all orders 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 order IDs to be returned in a request.

  • :deal_id (Integer)

    ID of the deal order is associated to.

  • :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<Order>)

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



34
35
36
37
38
# File 'lib/basecrm/services/orders_service.rb', line 34

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

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