Class: NimbleApi::Contact
- Inherits:
-
Object
- Object
- NimbleApi::Contact
- Defined in:
- lib/nimble/contact.rb
Instance Attribute Summary collapse
-
#contact ⇒ Object
Returns the value of attribute contact.
Instance Method Summary collapse
-
#by_email(email) ⇒ Object
Searches contacts for matching email, sets self.contact to matching result.
-
#by_name(first_name, last_name) ⇒ Object
Searches contacts for matching name, sets self.contact to first matching result.
- #create(params) ⇒ Object
-
#delete(id = nil) ⇒ Object
Delete contact with id of self.contact, or by id as argument.
-
#delete_note(id) ⇒ Object
Delete not by id for self.contact.
- #email ⇒ Object
-
#fetch(id = nil) ⇒ Object
(also: #get)
Gets contact by id and sets self.contact to result.
- #fields ⇒ Object
-
#id ⇒ Object
convenience methods.
-
#initialize(nimble) ⇒ Contact
constructor
A new instance of Contact.
-
#note(note, preview = nil) ⇒ Object
Adds note to contact.
-
#notes(params = nil) ⇒ Object
Returns notes for contact, based on params if provided.
-
#save ⇒ Object
Checks for duplicates by email.
-
#task(due_date, subject, notes = nil) ⇒ Object
Create new task for contac.
-
#update(fields) ⇒ Object
Update with param ‘fields’ set to passed in param hash.
Constructor Details
#initialize(nimble) ⇒ Contact
Returns a new instance of Contact.
6 7 8 |
# File 'lib/nimble/contact.rb', line 6 def initialize(nimble) @nimble = nimble end |
Instance Attribute Details
#contact ⇒ Object
Returns the value of attribute contact.
5 6 7 |
# File 'lib/nimble/contact.rb', line 5 def contact @contact end |
Instance Method Details
#by_email(email) ⇒ Object
Searches contacts for matching email, sets self.contact to matching result
62 63 64 65 66 67 68 |
# File 'lib/nimble/contact.rb', line 62 def by_email email query = { "and" => [{ "email" => { "is"=> email } },{ "record type"=> { "is"=> "person" }} ] } resp = @nimble.get 'contacts', { :query => query.to_json } self.contact = resp['resources'].first return nil unless self.contact self end |
#by_name(first_name, last_name) ⇒ Object
Searches contacts for matching name, sets self.contact to first matching result
71 72 73 74 75 76 77 |
# File 'lib/nimble/contact.rb', line 71 def by_name first_name, last_name query = { "and" => [{ "first name" => { "is"=> first_name } }, { "last name" => { "is"=> last_name } }, { "record type"=> { "is"=> "person" }} ] } resp = @nimble.get 'contacts', { :query => query.to_json } self.contact = resp['resources'].first return nil unless self.contact self end |
#create(params) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/nimble/contact.rb', line 10 def create params # email is used as unique id @email = params['email'] if params['address'] address = {"city" => params['address'], "street" => "", "zip" => "", "country" => ""}.to_json end @person = { "fields" => { "first name" => [{"value" => params['first name'],"modifier" => ""}], "last name" => [{ "value"=> params['last name'],"modifier" => ""}], "parent company" => [{ "modifier"=>"","value"=>params['company']}], "linkedin" => [{"modifier"=>"", "value"=>params['linkedin']}], "URL" => [{ "modifier"=>"other", "value"=>params['website']}], "email" => [{"modifier"=>"work", "value"=>params['email']}], "lead status" => [{"modifier"=>"", "value"=>"Not Qualified"}], "title" => [{"modifier" => "", "value" => params['headline']}], "address" => [ { "modifier" => "work","value" => address }] }, "tags" => [params['tags']], "record_type" => "person" } # remove empty fields @person['fields'].keys.each {|k| @person['fields'].delete(k) if @person['fields'][k][0]['value'].nil? } self end |
#delete(id = nil) ⇒ Object
Delete contact with id of self.contact, or by id as argument
118 119 120 121 |
# File 'lib/nimble/contact.rb', line 118 def delete id=nil id ||= self.id @nimble.delete "contact/#{id}" end |
#delete_note(id) ⇒ Object
Delete not by id for self.contact
113 114 115 |
# File 'lib/nimble/contact.rb', line 113 def delete_note id @nimble.delete "contact/notes/#{id}" end |
#email ⇒ Object
41 42 43 |
# File 'lib/nimble/contact.rb', line 41 def email self.contact['fields']['email'].first['value'] end |
#fetch(id = nil) ⇒ Object Also known as: get
Gets contact by id and sets self.contact to result
80 81 82 83 84 85 86 |
# File 'lib/nimble/contact.rb', line 80 def fetch id=nil id ||= self.id resp = @nimble.get "contact/#{id}" self.contact = resp['resources'].first return nil unless self.contact self end |
#fields ⇒ Object
45 46 47 |
# File 'lib/nimble/contact.rb', line 45 def fields self.contact['fields'] end |
#id ⇒ Object
convenience methods
37 38 39 |
# File 'lib/nimble/contact.rb', line 37 def id self.contact['id'] end |
#note(note, preview = nil) ⇒ Object
Adds note to contact. Preview set to 64 chars substring or can be provided
102 103 104 105 106 107 108 109 110 |
# File 'lib/nimble/contact.rb', line 102 def note note, preview=nil preview ||= note[0..64] params = { contact_ids: [ self.id ], note: note, note_preview: preview } @nimble.post "contacts/notes", params end |
#notes(params = nil) ⇒ Object
Returns notes for contact, based on params if provided
97 98 99 |
# File 'lib/nimble/contact.rb', line 97 def notes params=nil @nimble.get "contact/#{self.id}/notes", params end |
#save ⇒ Object
Checks for duplicates by email
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/nimble/contact.rb', line 50 def save raise 'must call contact.create(params) before save' unless @person raise 'must set email address for unique checking before save' unless @email if @email raise "#{@email} already exists!" unless self.by_email(@email).nil? end self.contact = @nimble.post 'contact', @person return nil unless self.contact self end |
#task(due_date, subject, notes = nil) ⇒ Object
Create new task for contac. due_date argument is parsed by Chronic and can be of format ‘next week’, ‘tomorrow’, ‘Friday’, ‘Oct 10, 2014’, etc.
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/nimble/contact.rb', line 125 def task due_date, subject, notes=nil due = Chronic.parse(due_date).strftime('%Y-%m-%d %H:%M') params = { related_to: [ self.id ], subject: subject, due_date: due } params[:notes] = notes if notes @nimble.post "activities/task", params end |
#update(fields) ⇒ Object
Update with param ‘fields’ set to passed in param hash
90 91 92 93 94 |
# File 'lib/nimble/contact.rb', line 90 def update fields self.contact = @nimble.put "contact/#{self.id}", { fields: fields } return nil unless self.contact self end |