Class: BaseCRM::LeadsService

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

Constant Summary collapse

OPTS_KEYS_TO_PERSIST =
Set[:address, :custom_fields, :description, :email, :facebook, :fax, :first_name, :industry, :last_name, :linkedin, :mobile, :organization_name, :owner_id, :phone, :skype, :source_id, :status, :tags, :title, :twitter, :website]

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ LeadsService

Returns a new instance of LeadsService.



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

def initialize(client)
  @client = client
end

Instance Method Details

#allEnumerable

Retrieve all leads

get ‘/leads’

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

def all
  PaginatedResource.new(self)
end

#create(lead) ⇒ Lead

Create a lead

post ‘/leads’

Creates a new lead A lead may represent a single individual or an organization

Parameters:

  • lead (Lead, Hash)

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

Returns:

  • (Lead)

    The resulting object represting created resource.



59
60
61
62
63
64
65
66
# File 'lib/basecrm/services/leads_service.rb', line 59

def create(lead)
  validate_type!(lead)

  attributes = sanitize(lead)
  _, _, root = @client.post("/leads", attributes)

  Lead.new(root[:data])
end

#destroy(id) ⇒ Boolean

Delete a lead

delete ‘/leads/BaseCRM#id

Delete an existing lead If the specified lead does not exist, this query returns an error This operation cannot be undone

Parameters:

  • id (Integer)

    Unique identifier of a Lead

Returns:

  • (Boolean)

    Status of the operation.



120
121
122
123
# File 'lib/basecrm/services/leads_service.rb', line 120

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

#find(id) ⇒ Lead

Retrieve a single lead

get ‘/leads/BaseCRM#id

Returns a single lead available to the user, according to the unique lead ID provided If the specified lead does not exist, this query returns an error

Parameters:

  • id (Integer)

    Unique identifier of a Lead

Returns:

  • (Lead)

    Searched resource object.



78
79
80
81
82
# File 'lib/basecrm/services/leads_service.rb', line 78

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

  Lead.new(root[:data])
end

#update(lead) ⇒ Lead

Update a lead

put ‘/leads/BaseCRM#id

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

Parameters:

  • lead (Lead, Hash)

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

Returns:

  • (Lead)

    The resulting object represting updated resource.



98
99
100
101
102
103
104
105
106
107
# File 'lib/basecrm/services/leads_service.rb', line 98

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

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

  Lead.new(root[:data])
end

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

Retrieve all leads

get ‘/leads’

Returns all leads available to the user, according to the parameters provided

Parameters:

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

    Search options

Options Hash (options):

  • :"address[city]" (String)

    City name.

  • :"address[country]" (String)

    Country name.

  • :"address[postal_code]" (String)

    Zip or Postal code

  • :creator_id (Integer)

    User ID. Returns all leads created by that user.

  • :first_name (String)

    First name of the lead.

  • :ids (String)

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

  • :last_name (String)

    Last name of the lead.

  • :organization_name (String)

    Organization name of the lead.

  • :owner_id (Integer)

    User ID. Returns all leads owned by that user.

  • :source_id (Integer)

    ID of the Source.

  • :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. The default limit is 25 and the maximum number that can be returned is 100.

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

    A field to sort by. The default order is ascending. If you want to change the sort order to descending, append ‘:desc` to the field e.g. `sort_by=last_name:desc`.

  • :status (String)

    Status of the lead.

Returns:

  • (Array<Lead>)

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



43
44
45
46
47
# File 'lib/basecrm/services/leads_service.rb', line 43

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

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