class BaseCRM::AssociatedContactsService

Constants

OPTS_KEYS_TO_PERSIST

Public Class Methods

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

Public Instance Methods

all(deal_id) click to toggle source

Retrieve deal's associated contacts

get '/deals/{deal_id}/associated_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/associated_contacts_service.rb, line 17
def all(deal_id)
  PaginatedResource.new(self, deal_id)
end
create(deal_id, associated_contact) click to toggle source

Create an associated contact

post '/deals/{deal_id}/associated_contacts'

Creates a deal's associated contact and its role If the specified deal or contact does not exist, the request will return an error

@param deal_id [Integer] Unique identifier of a Deal @param associated_contact [AssociatedContact, Hash] Either object of the AssociatedContact type or Hash. This object's attributes describe the object to be created. @return [AssociatedContact] The resulting object represting created resource.

# File lib/basecrm/services/associated_contacts_service.rb, line 49
def create(deal_id, associated_contact)
  validate_type!(associated_contact)

  attributes = sanitize(associated_contact)
  _, _, root = @client.post("/deals/#{deal_id}/associated_contacts", attributes)

  AssociatedContact.new(root[:data])
end
destroy(deal_id, contact_id) click to toggle source

Remove an associated contact

delete '/deals/{deal_id}/associated_contacts/{contact_id}'

Remove a deal's associated contact If a deal with the supplied unique identifier does not exist, it returns an error This operation cannot be undone

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

# File lib/basecrm/services/associated_contacts_service.rb, line 70
def destroy(deal_id, contact_id)
  status, _, _ = @client.delete("/deals/#{deal_id}/associated_contacts/#{contact_id}")
  status == 204
end
where(deal_id, options = {}) click to toggle source

Retrieve deal's associated contacts

get '/deals/{deal_id}/associated_contacts'

Returns all deal associated contacts

@param deal_id [Integer] Unique identifier of a Deal @param options [Hash] Search options @option options [Integer] :page (1) Page number to start from. Page numbering starts at 1, and omitting the `page` parameter will return the first page. @option options [Integer] :per_page (25) Number of records to return per page. Default limit is 25 and the maximum number that can be returned is 100. @return [Array<AssociatedContact>] The list of AssociatedContacts for the first page, unless otherwise specified.

# File lib/basecrm/services/associated_contacts_service.rb, line 32
def where(deal_id, options = {})
  _, _, root = @client.get("/deals/#{deal_id}/associated_contacts", options)

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

Private Instance Methods

extract_params!(associated_contact, *args) click to toggle source
# File lib/basecrm/services/associated_contacts_service.rb, line 81
def extract_params!(associated_contact, *args)
  params = associated_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(associated_contact) click to toggle source
# File lib/basecrm/services/associated_contacts_service.rb, line 87
def sanitize(associated_contact)
  associated_contact.to_h.select { |k, _| OPTS_KEYS_TO_PERSIST.include?(k) }
end
validate_type!(associated_contact) click to toggle source
# File lib/basecrm/services/associated_contacts_service.rb, line 77
def validate_type!(associated_contact)
  raise TypeError unless associated_contact.is_a?(AssociatedContact) || associated_contact.is_a?(Hash)
end