Class: BaseCRM::ContactsService

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

Constant Summary collapse

OPTS_KEYS_TO_PERSIST =
Set[:address, :contact_id, :custom_fields, :customer_status, :description, :email, :facebook, :fax, :first_name, :industry, :is_organization, :last_name, :linkedin, :mobile, :name, :owner_id, :phone, :prospect_status, :skype, :tags, :title, :twitter, :website]

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ ContactsService

Returns a new instance of ContactsService.



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

def initialize(client)
  @client = client
end

Instance Method Details

#allEnumerable

Retrieve all contacts

get ‘/contacts’

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

def all
  PaginatedResource.new(self)
end

#create(contact) ⇒ Contact

Create a contact

post ‘/contacts’

Create a new contact A contact may represent a single individual or an organization

Parameters:

  • contact (Contact, Hash)

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

Returns:

  • (Contact)

    The resulting object represting created resource.



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

def create(contact)
  validate_type!(contact)

  attributes = sanitize(contact)
  _, _, root = @client.post("/contacts", attributes)

  Contact.new(root[:data])
end

#destroy(id) ⇒ Boolean

Delete a contact

delete ‘/contacts/BaseCRM#id

Delete an existing contact If the specified contact does not exist, the request will return an error This operation cannot be undone

Parameters:

  • id (Integer)

    Unique identifier of a Contact

Returns:

  • (Boolean)

    Status of the operation.



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

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

#find(id) ⇒ Contact

Retrieve a single contact

get ‘/contacts/BaseCRM#id

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

Parameters:

  • id (Integer)

    Unique identifier of a Contact

Returns:

  • (Contact)

    Searched resource object.



77
78
79
80
81
# File 'lib/basecrm/services/contacts_service.rb', line 77

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

  Contact.new(root[:data])
end

#update(contact) ⇒ Contact

Update a contact

put ‘/contacts/BaseCRM#id

Updates contact information If the specified contact does not exist, the request will return an error Notice When updating contact tags, you need to provide all tags Any missing tag will be removed from a contact’s tags

Parameters:

  • contact (Contact, Hash)

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

Returns:

  • (Contact)

    The resulting object represting updated resource.



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

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

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

  Contact.new(root[:data])
end

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

Retrieve all contacts

get ‘/contacts’

Returns all contacts 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 code or equivalent

  • :contact_id (Integer)

    The unique identifier of the organization that the contact belongs to.

  • :email (String)

    Email address of the contact.

  • :first_name (String)

    First name of the contact.

  • :ids (String)

    Comma-separated list of the IDs for the contacts you want to be returned in your request.

  • :is_organization (Boolean)

    Indicates whether or not this contact refers to an organization or an individual.

  • :last_name (String)

    Last name of the contact.

  • :name (String)

    Name of the contact.

  • :page (Integer) — default: 1

    The page number to start from. Page numbering is 1-based and omitting the ‘page` parameter will return the first page.

  • :per_page (Integer) — default: 25

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

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

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

Returns:

  • (Array<Contact>)

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



42
43
44
45
46
# File 'lib/basecrm/services/contacts_service.rb', line 42

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

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