Class: Hubspot::Contact
- Inherits:
-
Resource
- Object
- Resource
- Hubspot::Contact
- Defined in:
- lib/hubspot/contact.rb
Constant Summary collapse
- ALL_PATH =
'/crm/v3/objects/contacts'- CREATE_PATH =
'/contacts/v1/contact'- CREATE_OR_UPDATE_PATH =
'/contacts/v1/contact/createOrUpdate/email/:email'- DELETE_PATH =
'/contacts/v1/contact/vid/:id'- FIND_PATH =
'/contacts/v1/contact/vid/:id/profile'- FIND_BY_EMAIL_PATH =
'/contacts/v1/contact/email/:email/profile'- FIND_BY_USER_TOKEN_PATH =
'/contacts/v1/contact/utk/:token/profile'- MERGE_PATH =
'/contacts/v1/contact/merge-vids/:id/'- SEARCH_PATH =
'/contacts/v1/search/query'- UPDATE_PATH =
'/contacts/v1/contact/vid/:id/profile'- BATCH_UPDATE_PATH =
'/contacts/v1/contact/batch'
Class Method Summary collapse
- .all(opts = {}) ⇒ Object
- .batch_update(contacts, opts = {}) ⇒ Object
- .create(email, properties = {}) ⇒ Object
- .create_or_update(email, properties = {}) ⇒ Object
- .find_by_email(email) ⇒ Object
- .find_by_user_token(token) ⇒ Object (also: find_by_utk)
- .find_by_vid(vid) ⇒ Object
- .merge(primary, secondary) ⇒ Object
- .search(query, opts = {}) ⇒ Object
Instance Method Summary collapse
Class Method Details
.all(opts = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/hubspot/contact.rb', line 20 def all(opts = {}) Hubspot::PagedCollection.new(opts) do |, after, limit| = .merge(limit:) [:after] = after if after.present? response = Hubspot::Connection.get_json(ALL_PATH, ) contacts = response['results'].map do |result| from_result result, id_field: Hubspot::Resource.id_field end after = response.dig('paging', 'next', 'after') [contacts, after, after.present?] end end |
.batch_update(contacts, opts = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/hubspot/contact.rb', line 84 def batch_update(contacts, opts = {}) request = contacts.map do |contact| # Use the specified options or update with the changes changes = opts.empty? ? contact.changes : opts next if changes.empty? { 'vid' => contact.id, 'properties' => changes.map { |k, v| { 'property' => k, 'value' => v } } } end # Remove any objects without changes and return if there is nothing to update request.compact! return true if request.empty? Hubspot::Connection.post_json( BATCH_UPDATE_PATH, params: {}, body: request ) true end |
.create(email, properties = {}) ⇒ Object
50 51 52 |
# File 'lib/hubspot/contact.rb', line 50 def create(email, properties = {}) super(properties.merge('email' => email)) end |
.create_or_update(email, properties = {}) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/hubspot/contact.rb', line 54 def create_or_update(email, properties = {}) request = { properties: Hubspot::Utils.hash_to_properties(properties.stringify_keys, key_name: 'property') } response = Hubspot::Connection.post_json(CREATE_OR_UPDATE_PATH, params: { email: email }, body: request) from_result(response) end |
.find_by_email(email) ⇒ Object
39 40 41 42 |
# File 'lib/hubspot/contact.rb', line 39 def find_by_email(email) response = Hubspot::Connection.get_json(FIND_BY_EMAIL_PATH, email: email) from_result(response) end |
.find_by_user_token(token) ⇒ Object Also known as: find_by_utk
44 45 46 47 |
# File 'lib/hubspot/contact.rb', line 44 def find_by_user_token(token) response = Hubspot::Connection.get_json(FIND_BY_USER_TOKEN_PATH, token: token) from_result(response) end |
.find_by_vid(vid) ⇒ Object
34 35 36 37 |
# File 'lib/hubspot/contact.rb', line 34 def find_by_vid(vid) response = Hubspot::Connection.get_json(FIND_PATH, id: vid) from_result(response) end |
.merge(primary, secondary) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/hubspot/contact.rb', line 74 def merge(primary, secondary) Hubspot::Connection.post_json( MERGE_PATH, params: { id: primary.to_i, no_parse: true }, body: { 'vidToMerge' => secondary.to_i } ) true end |
.search(query, opts = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/hubspot/contact.rb', line 62 def search(query, opts = {}) Hubspot::PagedCollection.new(opts) do |, offset, limit| response = Hubspot::Connection.get_json( SEARCH_PATH, .merge(q: query, offset: offset, count: limit) ) contacts = response['contacts'].map { |result| from_result(result) } [contacts, response['offset'], response['has-more']] end end |
Instance Method Details
#[](name) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/hubspot/contact.rb', line 111 def [](name) return @changes[name] if changes.key? name name_property = @properties[name] name_property.is_a?(Hash) ? name_property['value'] : name_property end |
#merge(contact) ⇒ Object
123 124 125 |
# File 'lib/hubspot/contact.rb', line 123 def merge(contact) self.class.merge(@id, contact.to_i) end |
#name ⇒ Object
119 120 121 |
# File 'lib/hubspot/contact.rb', line 119 def name [firstname, lastname].compact.join(' ') end |