class BaseCRM::ContactsService

Constants

OPTS_KEYS_TO_PERSIST

Public Class Methods

new(client) click to toggle source
# File lib/basecrm/services/contacts_service.rb, line 7
def initialize(client)
  @client = client
end

Public Instance Methods

all() click to toggle source

Retrieve all contacts

get '/contacts'

If you want to use filtering or sorting (see where). @return [Enumerable] Paginated resource you can use to iterate over all the resources.

# File lib/basecrm/services/contacts_service.rb, line 17
def all
  PaginatedResource.new(self)
end
create(contact) click to toggle source

Create a contact

post '/contacts'

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

@param contact [Contact, Hash] Either object of the Contact type or Hash. This object's attributes describe the object to be created. @return [Contact] The resulting object represting created resource.

# 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) click to toggle source

Delete a contact

delete '/contacts/{id}'

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

@param id [Integer] Unique identifier of a Contact @return [Boolean] Status of the operation.

# File lib/basecrm/services/contacts_service.rb, line 117
def destroy(id)
  status, _, _ = @client.delete("/contacts/#{id}")
  status == 204
end
find(id) click to toggle source

Retrieve a single contact

get '/contacts/{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

@param id [Integer] Unique identifier of a Contact @return [Contact] Searched resource object.

# File lib/basecrm/services/contacts_service.rb, line 77
def find(id)
  _, _, root = @client.get("/contacts/#{id}")

  Contact.new(root[:data])
end
update(contact) click to toggle source

Update a contact

put '/contacts/{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

@param contact [Contact, Hash] Either object of the Contact type or Hash. This object's attributes describe the object to be updated. @return [Contact] The resulting object represting updated resource.

# 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 = {}) click to toggle source

Retrieve all contacts

get '/contacts'

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

@param options [Hash] Search options @option options [String] :“address” City name. @option options [String] :“address” Country name. @option options [String] :“address” Zip code or equivalent @option options [Integer] :contact_id The unique identifier of the organization that the contact belongs to. @option options [String] :email Email address of the contact. @option options [String] :first_name First name of the contact. @option options [String] :ids Comma-separated list of the IDs for the contacts you want to be returned in your request. @option options [Boolean] :is_organization Indicates whether or not this contact refers to an organization or an individual. @option options [String] :last_name Last name of the contact. @option options [String] :name Name of the contact. @option options [Integer] :page (1) The page number to start from. Page numbering is 1-based and omitting the `page` parameter will return the first page. @option options [Integer] :per_page (25) The number of records to return per page. Default limit is 25 and maximum number that can be returned is 100. @option options [String] :sort_by (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`. @return [Array<Contact>] The list of Contacts for the first page, unless otherwise specified.

# 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

Private Instance Methods

extract_params!(contact, *args) click to toggle source
# File lib/basecrm/services/contacts_service.rb, line 128
def extract_params!(contact, *args)
  params = contact.to_h.select{ |k, _| args.include?(k) }
  raise ArgumentError, "one of required attributes is missing. Expected: #{args.join(',')}" if params.count != args.length
  params
end
sanitize(contact) click to toggle source
# File lib/basecrm/services/contacts_service.rb, line 134
def sanitize(contact)
  contact.to_h.select { |k, _| OPTS_KEYS_TO_PERSIST.include?(k) }
end
validate_type!(contact) click to toggle source
# File lib/basecrm/services/contacts_service.rb, line 124
def validate_type!(contact)
  raise TypeError unless contact.is_a?(Contact) || contact.is_a?(Hash)
end