Class: AdvisorsCommandClient::Models::ContactCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/advisors_command_client/models/contact_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ContactCollection

Returns a new instance of ContactCollection.



4
5
6
# File 'lib/advisors_command_client/models/contact_collection.rb', line 4

def initialize(args = {})
  @connection = args[:connection]
end

Instance Method Details

#create(params) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/advisors_command_client/models/contact_collection.rb', line 34

def create(params)
  contact = AdvisorsCommandClient::Models::Contact.new(params)
  resp = @connection.post("contacts.json", { contact: contact.as_json })

  if resp.success?
    contact.id = resp.body['id']
    return contact
  else
    return false
  end
end

#find(contact_id) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/advisors_command_client/models/contact_collection.rb', line 25

def find(contact_id)
  resp = @connection.get("contacts/#{contact_id}")
  if resp.success?
    return AdvisorsCommandClient::Models::Contact.load(resp.body, @connection)
  else
    return nil
  end
end

#search(query) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/advisors_command_client/models/contact_collection.rb', line 8

def search(query)
  response = @connection.get('search', {search: query, from: 'oro_contact'})
  if response.success?
    return Parallel.map(Array(response.body['data'])) do |obj|
      begin
        next unless obj['record_string']
        self.find(obj['record_id'].to_i)
      rescue Faraday::Error::ParsingError
        puts "Error parsing response for contact ID: #{obj['record_id']}"
        next nil
      end
    end.compact
  else
    raise ::AdvisorsCommandClient::SearchError, "Error connecting to advisors command."
  end
end

#update(contact_id, params) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/advisors_command_client/models/contact_collection.rb', line 46

def update(contact_id, params)
  contact = AdvisorsCommandClient::Models::Contact.new(params.merge(id: contact_id))
  resp = @connection.put("contacts/#{contact_id}.json", { contact: contact.as_json })

  if resp.success?
    return contact
  else
    return false
  end
end