Class: BaseCRM::DealsService

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

Constant Summary collapse

OPTS_KEYS_TO_PERSIST =
Set[:contact_id, :currency, :custom_fields, :hot, :loss_reason_id, :name, :owner_id, :source_id, :stage_id, :tags, :value, :last_stage_change_at, :estimated_close_date, :customized_win_likelihood]

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ DealsService

Returns a new instance of DealsService.



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

def initialize(client)
  @client = client
end

Instance Method Details

#allEnumerable

Retrieve all deals

get ‘/deals’

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

def all
  PaginatedResource.new(self)
end

#create(deal) ⇒ Deal

Create a deal

post ‘/deals’

Create a new deal

Parameters:

  • deal (Deal, Hash)

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

Returns:

  • (Deal)

    The resulting object represting created resource.



56
57
58
59
60
61
62
63
# File 'lib/basecrm/services/deals_service.rb', line 56

def create(deal)
  validate_type!(deal)

  attributes = sanitize(deal)
  _, _, root = @client.post("/deals", attributes)

  Deal.new(root[:data])
end

#destroy(id) ⇒ Boolean

Delete a deal

delete ‘/deals/BaseCRM#id

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

Parameters:

  • id (Integer)

    Unique identifier of a Deal

Returns:

  • (Boolean)

    Status of the operation.



117
118
119
120
# File 'lib/basecrm/services/deals_service.rb', line 117

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

#find(id) ⇒ Deal

Retrieve a single deal

get ‘/deals/BaseCRM#id

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

Parameters:

  • id (Integer)

    Unique identifier of a Deal

Returns:

  • (Deal)

    Searched resource object.



75
76
77
78
79
# File 'lib/basecrm/services/deals_service.rb', line 75

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

  Deal.new(root[:data])
end

#update(deal) ⇒ Deal

Update a deal

put ‘/deals/BaseCRM#id

Updates deal information If the specified deal does not exist, the request will return an error <figure class=“notice”> In order to modify tags used on a record, you need to supply the entire set ‘tags` are replaced every time they are used in a request </figure>

Parameters:

  • deal (Deal, Hash)

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

Returns:

  • (Deal)

    The resulting object represting updated resource.



95
96
97
98
99
100
101
102
103
104
# File 'lib/basecrm/services/deals_service.rb', line 95

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

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

  Deal.new(root[:data])
end

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

Retrieve all deals

get ‘/deals’

Returns all deals available to the user according to the parameters provided

Parameters:

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

    Search options

Options Hash (options):

  • :contact_id (Integer)

    Unique identifier of a primary contact.

  • :creator_id (Integer)

    Unique identifier of the user the deal was created by. Returns all deals created by the user.

  • :hot (Boolean)

    Indicator of whether or not the deal is hot.

  • :ids (String)

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

  • :organization_id (Integer)

    Unique identifier of an organization.

  • :owner_id (Integer)

    Unique identifier of the user the deal is owned by. Returns all deals 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`.

  • :source_id (Integer)

    Id of the Source.

  • :stage_id (Integer)

    Id of the Stage.

  • :estimated_close_date (String)

    of the deal.

Returns:

  • (Array<Deal>)

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



41
42
43
44
45
# File 'lib/basecrm/services/deals_service.rb', line 41

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

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