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
- #create(params) ⇒ Object
- #delete(id = nil) ⇒ Object
- #delete_note(id) ⇒ Object
- #email ⇒ Object
- #fetch(id = nil) ⇒ Object (also: #get)
- #fields ⇒ Object
-
#id ⇒ Object
convenience methods.
-
#initialize(nimble) ⇒ Contact
constructor
A new instance of Contact.
- #note(note, preview = nil) ⇒ Object
- #notes(params = nil) ⇒ Object
-
#save ⇒ Object
Checks for duplicates by email.
- #task(due_date, subject, notes = nil) ⇒ Object
- #update(fields) ⇒ Object
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
61 62 63 64 65 66 67 |
# File 'lib/nimble/contact.rb', line 61 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 |
#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
102 103 104 105 |
# File 'lib/nimble/contact.rb', line 102 def delete id=nil id ||= self.id @nimble.delete "contact/#{id}" end |
#delete_note(id) ⇒ Object
98 99 100 |
# File 'lib/nimble/contact.rb', line 98 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
69 70 71 72 73 74 75 |
# File 'lib/nimble/contact.rb', line 69 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
88 89 90 91 92 93 94 95 96 |
# File 'lib/nimble/contact.rb', line 88 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
84 85 86 |
# File 'lib/nimble/contact.rb', line 84 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
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/nimble/contact.rb', line 107 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
78 79 80 81 82 |
# File 'lib/nimble/contact.rb', line 78 def update fields self.contact = @nimble.put "contact/#{self.id}", { fields: fields } return nil unless self.contact self end |