hubspot-ruby 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/hubspot-ruby.gemspec +42 -5
  3. data/lib/hubspot-ruby.rb +3 -0
  4. data/lib/hubspot/blog.rb +14 -48
  5. data/lib/hubspot/connection.rb +95 -0
  6. data/lib/hubspot/contact.rb +74 -91
  7. data/lib/hubspot/contact_list.rb +127 -0
  8. data/lib/hubspot/contact_properties.rb +12 -0
  9. data/lib/hubspot/deal.rb +29 -25
  10. data/lib/hubspot/exceptions.rb +2 -0
  11. data/lib/hubspot/form.rb +88 -4
  12. data/lib/hubspot/topic.rb +8 -24
  13. data/lib/hubspot/utils.rb +0 -51
  14. data/lib/hubspot/version.rb +1 -1
  15. data/spec/fixtures/vcr_cassettes/add_contacts_to_lists.yml +281 -0
  16. data/spec/fixtures/vcr_cassettes/contact_create.yml +34 -2
  17. data/spec/fixtures/vcr_cassettes/contact_create_existing_email.yml +34 -2
  18. data/spec/fixtures/vcr_cassettes/contact_create_invalid_email.yml +35 -3
  19. data/spec/fixtures/vcr_cassettes/contact_create_with_params.yml +34 -2
  20. data/spec/fixtures/vcr_cassettes/contact_destroy.yml +36 -4
  21. data/spec/fixtures/vcr_cassettes/contact_find_by_email_batch_mode.yml +509 -0
  22. data/spec/fixtures/vcr_cassettes/contact_find_by_id_batch_mode.yml +33 -0
  23. data/spec/fixtures/vcr_cassettes/contact_find_by_utk_batch_mode.yml +33 -0
  24. data/spec/fixtures/vcr_cassettes/contact_list_batch_find.yml +65 -0
  25. data/spec/fixtures/vcr_cassettes/contact_list_destroy.yml +63 -0
  26. data/spec/fixtures/vcr_cassettes/contact_list_example.yml +33 -0
  27. data/spec/fixtures/vcr_cassettes/contact_list_find.yml +96 -0
  28. data/spec/fixtures/vcr_cassettes/contact_list_refresh.yml +33 -0
  29. data/spec/fixtures/vcr_cassettes/contact_list_update.yml +36 -0
  30. data/spec/fixtures/vcr_cassettes/contacts_among_list.yml +189 -0
  31. data/spec/fixtures/vcr_cassettes/create_form.yml +39 -0
  32. data/spec/fixtures/vcr_cassettes/create_list.yml +36 -0
  33. data/spec/fixtures/vcr_cassettes/create_list_with_filters.yml +36 -0
  34. data/spec/fixtures/vcr_cassettes/deal_create.yml +29 -0
  35. data/spec/fixtures/vcr_cassettes/deal_find.yml +56 -0
  36. data/spec/fixtures/vcr_cassettes/destroy_deal.yml +110 -0
  37. data/spec/fixtures/vcr_cassettes/fail_to_create_form.yml +35 -0
  38. data/spec/fixtures/vcr_cassettes/fail_to_create_list.yml +35 -0
  39. data/spec/fixtures/vcr_cassettes/field_among_form.yml +34 -0
  40. data/spec/fixtures/vcr_cassettes/fields_among_form.yml +35 -0
  41. data/spec/fixtures/vcr_cassettes/find_all_contacts.yml +39 -0
  42. data/spec/fixtures/vcr_cassettes/find_all_dynamic_lists.yml +104 -0
  43. data/spec/fixtures/vcr_cassettes/find_all_forms.yml +15378 -0
  44. data/spec/fixtures/vcr_cassettes/find_all_lists.yml +138 -0
  45. data/spec/fixtures/vcr_cassettes/find_all_recent_contacts.yml +33 -0
  46. data/spec/fixtures/vcr_cassettes/find_all_stastic_lists.yml +21876 -0
  47. data/spec/fixtures/vcr_cassettes/form_destroy.yml +64 -0
  48. data/spec/fixtures/vcr_cassettes/form_example.yml +39 -0
  49. data/spec/fixtures/vcr_cassettes/form_find.yml +69 -0
  50. data/spec/fixtures/vcr_cassettes/form_submit_data.yml +130 -0
  51. data/spec/fixtures/vcr_cassettes/form_update.yml +77 -0
  52. data/spec/fixtures/vcr_cassettes/one_month_blog_posts_filter_state.yml +5168 -0
  53. data/spec/fixtures/vcr_cassettes/remove_contacts_from_lists.yml +315 -0
  54. data/spec/lib/hubspot/blog_spec.rb +4 -3
  55. data/spec/lib/hubspot/connection_spec.rb +112 -0
  56. data/spec/lib/hubspot/contact_list_spec.rb +249 -0
  57. data/spec/lib/hubspot/contact_properties_spec.rb +8 -0
  58. data/spec/lib/hubspot/contact_spec.rb +110 -54
  59. data/spec/lib/hubspot/deal_spec.rb +11 -0
  60. data/spec/lib/hubspot/form_spec.rb +157 -10
  61. data/spec/lib/hubspot/utils_spec.rb +0 -67
  62. data/spec/live/deals_integration_spec.rb +35 -0
  63. data/spec/spec_helper.rb +2 -0
  64. data/spec/support/tests_helper.rb +17 -0
  65. metadata +70 -33
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de21d5dd95f68e46b0d289c9cae19de49423df0d
4
- data.tar.gz: afe20a8aeb443ba86a5742140ce488fc50312bfc
3
+ metadata.gz: 1be19f1656874dfa75e7c0477b26b3c9d615710d
4
+ data.tar.gz: a70226ee0a0bac7a0fc14d4b7bf8c3a0d4fdac2a
5
5
  SHA512:
6
- metadata.gz: 45f1b32ab613d5f38f362c2e4aac42b36fbf1cf8c5b4d38ceac935ca259b90fa637d0d49cd717c10015e859fb1dbb826fa79f0ec5d094f42cb811e6df3c916f8
7
- data.tar.gz: 20cc23dc732a1c21869e2736cb5c39bd8cecd8a1e07ade008b82a79b3ec404b173528c348a9d96617986c75496d61f62c79583fd436ff463859203ee34cf4501
6
+ metadata.gz: 120f944669ecb856d410a9490043a7e57a8ad66bbea4cc1dcfaaffbf8c8e0d741e243538b2fdc52bf760430277b54476ab1b79b4202ac18be63b6883c95dd4fe
7
+ data.tar.gz: 5a5412877b5a4cc50d56321c85b40c9750e4c7ea865beef1d98c0664740656815f0cec6385e1a8c386acf8806b5b800aa480189cadd81cfa90a40e7a5e0da5b5
data/hubspot-ruby.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: hubspot-ruby 0.1.7 ruby lib
5
+ # stub: hubspot-ruby 0.1.8 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "hubspot-ruby"
9
- s.version = "0.1.7"
9
+ s.version = "0.1.8"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Andrew DiMichele"]
14
- s.date = "2015-01-12"
14
+ s.date = "2015-05-12"
15
15
  s.description = "hubspot-ruby is a wrapper for the HubSpot REST API"
16
16
  s.email = "hello@omadahealth.com"
17
17
  s.extra_rdoc_files = [
@@ -32,13 +32,17 @@ Gem::Specification.new do |s|
32
32
  "lib/hubspot-ruby.rb",
33
33
  "lib/hubspot/blog.rb",
34
34
  "lib/hubspot/config.rb",
35
+ "lib/hubspot/connection.rb",
35
36
  "lib/hubspot/contact.rb",
37
+ "lib/hubspot/contact_list.rb",
38
+ "lib/hubspot/contact_properties.rb",
36
39
  "lib/hubspot/deal.rb",
37
40
  "lib/hubspot/exceptions.rb",
38
41
  "lib/hubspot/form.rb",
39
42
  "lib/hubspot/topic.rb",
40
43
  "lib/hubspot/utils.rb",
41
44
  "lib/hubspot/version.rb",
45
+ "spec/fixtures/vcr_cassettes/add_contacts_to_lists.yml",
42
46
  "spec/fixtures/vcr_cassettes/blog_list.yml",
43
47
  "spec/fixtures/vcr_cassettes/blog_posts.yml",
44
48
  "spec/fixtures/vcr_cassettes/blog_posts_list.yml",
@@ -49,34 +53,67 @@ Gem::Specification.new do |s|
49
53
  "spec/fixtures/vcr_cassettes/contact_destroy.yml",
50
54
  "spec/fixtures/vcr_cassettes/contact_example.yml",
51
55
  "spec/fixtures/vcr_cassettes/contact_find_by_email.yml",
56
+ "spec/fixtures/vcr_cassettes/contact_find_by_email_batch_mode.yml",
52
57
  "spec/fixtures/vcr_cassettes/contact_find_by_id.yml",
58
+ "spec/fixtures/vcr_cassettes/contact_find_by_id_batch_mode.yml",
53
59
  "spec/fixtures/vcr_cassettes/contact_find_by_utk.yml",
60
+ "spec/fixtures/vcr_cassettes/contact_find_by_utk_batch_mode.yml",
61
+ "spec/fixtures/vcr_cassettes/contact_list_batch_find.yml",
62
+ "spec/fixtures/vcr_cassettes/contact_list_destroy.yml",
63
+ "spec/fixtures/vcr_cassettes/contact_list_example.yml",
64
+ "spec/fixtures/vcr_cassettes/contact_list_find.yml",
65
+ "spec/fixtures/vcr_cassettes/contact_list_refresh.yml",
66
+ "spec/fixtures/vcr_cassettes/contact_list_update.yml",
54
67
  "spec/fixtures/vcr_cassettes/contact_update.yml",
68
+ "spec/fixtures/vcr_cassettes/contacts_among_list.yml",
69
+ "spec/fixtures/vcr_cassettes/create_form.yml",
70
+ "spec/fixtures/vcr_cassettes/create_list.yml",
71
+ "spec/fixtures/vcr_cassettes/create_list_with_filters.yml",
55
72
  "spec/fixtures/vcr_cassettes/deal_create.yml",
56
73
  "spec/fixtures/vcr_cassettes/deal_example.yml",
57
74
  "spec/fixtures/vcr_cassettes/deal_find.yml",
58
75
  "spec/fixtures/vcr_cassettes/destroy_deal.yml",
76
+ "spec/fixtures/vcr_cassettes/fail_to_create_form.yml",
77
+ "spec/fixtures/vcr_cassettes/fail_to_create_list.yml",
78
+ "spec/fixtures/vcr_cassettes/field_among_form.yml",
79
+ "spec/fixtures/vcr_cassettes/fields_among_form.yml",
59
80
  "spec/fixtures/vcr_cassettes/find_all_contacts.yml",
81
+ "spec/fixtures/vcr_cassettes/find_all_dynamic_lists.yml",
82
+ "spec/fixtures/vcr_cassettes/find_all_forms.yml",
83
+ "spec/fixtures/vcr_cassettes/find_all_lists.yml",
84
+ "spec/fixtures/vcr_cassettes/find_all_recent_contacts.yml",
60
85
  "spec/fixtures/vcr_cassettes/find_all_recent_updated_deals.yml",
86
+ "spec/fixtures/vcr_cassettes/find_all_stastic_lists.yml",
87
+ "spec/fixtures/vcr_cassettes/form_destroy.yml",
88
+ "spec/fixtures/vcr_cassettes/form_example.yml",
89
+ "spec/fixtures/vcr_cassettes/form_find.yml",
61
90
  "spec/fixtures/vcr_cassettes/form_post.yml",
91
+ "spec/fixtures/vcr_cassettes/form_submit_data.yml",
92
+ "spec/fixtures/vcr_cassettes/form_update.yml",
62
93
  "spec/fixtures/vcr_cassettes/one_month_blog_posts_filter_state.yml",
63
94
  "spec/fixtures/vcr_cassettes/one_month_blog_posts_list.yml",
95
+ "spec/fixtures/vcr_cassettes/remove_contacts_from_lists.yml",
64
96
  "spec/fixtures/vcr_cassettes/topic_list.yml",
65
97
  "spec/fixtures/vcr_cassettes/topics_list.yml",
66
98
  "spec/lib/hubspot-ruby_spec.rb",
67
99
  "spec/lib/hubspot/blog_spec.rb",
68
100
  "spec/lib/hubspot/config_spec.rb",
101
+ "spec/lib/hubspot/connection_spec.rb",
102
+ "spec/lib/hubspot/contact_list_spec.rb",
103
+ "spec/lib/hubspot/contact_properties_spec.rb",
69
104
  "spec/lib/hubspot/contact_spec.rb",
70
105
  "spec/lib/hubspot/deal_spec.rb",
71
106
  "spec/lib/hubspot/form_spec.rb",
72
107
  "spec/lib/hubspot/topic_spec.rb",
73
108
  "spec/lib/hubspot/utils_spec.rb",
74
109
  "spec/live/contacts_integration_spec.rb",
110
+ "spec/live/deals_integration_spec.rb",
75
111
  "spec/spec_helper.rb",
76
- "spec/support/cassette_helper.rb"
112
+ "spec/support/cassette_helper.rb",
113
+ "spec/support/tests_helper.rb"
77
114
  ]
78
115
  s.homepage = "http://github.com/omadahealth/hubspot-ruby"
79
- s.rubygems_version = "2.4.1"
116
+ s.rubygems_version = "2.2.2"
80
117
  s.summary = "hubspot-ruby is a wrapper for the HubSpot REST API"
81
118
 
82
119
  if s.respond_to? :specification_version then
data/lib/hubspot-ruby.rb CHANGED
@@ -2,7 +2,10 @@ require 'active_support/core_ext'
2
2
  require 'httparty'
3
3
  require 'hubspot/exceptions'
4
4
  require 'hubspot/config'
5
+ require 'hubspot/connection'
5
6
  require 'hubspot/contact'
7
+ require 'hubspot/contact_properties'
8
+ require 'hubspot/contact_list'
6
9
  require 'hubspot/form'
7
10
  require 'hubspot/blog'
8
11
  require 'hubspot/topic'
data/lib/hubspot/blog.rb CHANGED
@@ -1,16 +1,8 @@
1
- require 'hubspot/utils'
2
- require 'httparty'
3
-
4
1
  module Hubspot
5
2
  #
6
3
  # HubSpot Contacts API
7
4
  #
8
- # {https://developers.hubspot.com/docs/endpoints#contacts-api}
9
- #
10
5
  class Blog
11
- class InvalidParams < StandardError
12
- end
13
-
14
6
  BLOG_LIST_PATH = "/content/api/v2/blogs"
15
7
  BLOG_POSTS_PATH = "/content/api/v2/blog-posts"
16
8
  GET_BLOG_BY_ID_PATH = "/content/api/v2/blogs/:blog_id"
@@ -19,30 +11,18 @@ module Hubspot
19
11
  # Lists the blogs
20
12
  # {https://developers.hubspot.com/docs/methods/blogv2/get_blogs}
21
13
  # No param filtering is currently implemented
22
- # @return [Hubspot::Blog, []] the first 20 blogs or empty_array
14
+ # @return [Hubspot::Blog] the first 20 blogs or empty_array
23
15
  def list
24
- url = Hubspot::Utils.generate_url(BLOG_LIST_PATH)
25
- resp = HTTParty.get(url, format: :json)
26
- if resp.success?
27
- resp.parsed_response['objects'].map do |blog_hash|
28
- Blog.new(blog_hash)
29
- end
30
- else
31
- []
32
- end
16
+ response = Hubspot::Connection.get_json(BLOG_LIST_PATH, {})
17
+ response['objects'].map { |b| new(b) }
33
18
  end
34
19
 
35
20
  # Finds a specific blog by its ID
36
21
  # {https://developers.hubspot.com/docs/methods/blogv2/get_blogs_blog_id}
37
- # @return Hubspot::Blog or nil
22
+ # @return Hubspot::Blog
38
23
  def find_by_id(id)
39
- url = Hubspot::Utils.generate_url(GET_BLOG_BY_ID_PATH, blog_id: id)
40
- resp = HTTParty.get(url, format: :json)
41
- if resp.success?
42
- Blog.new(resp.parsed_response)
43
- else
44
- nil
45
- end
24
+ response = Hubspot::Connection.get_json(GET_BLOG_BY_ID_PATH, { blog_id: id })
25
+ new(response)
46
26
  end
47
27
  end
48
28
 
@@ -61,7 +41,7 @@ module Hubspot
61
41
  # defaults to returning the last 2 months worth of published blog posts
62
42
  # in date descending order (i.e. most recent first)
63
43
  # {https://developers.hubspot.com/docs/methods/blogv2/get_blog_posts}
64
- # @return [Hubspot::BlogPost] or []
44
+ # @return [Hubspot::BlogPost]
65
45
  def posts(params = {})
66
46
  default_params = {
67
47
  content_group_id: self["id"],
@@ -69,23 +49,14 @@ module Hubspot
69
49
  created__gt: Time.now - 2.month,
70
50
  state: 'PUBLISHED'
71
51
  }
72
- raise InvalidParams.new('params must be passed as a hash') unless params.is_a?(Hash)
52
+ raise Hubspot::InvalidParams.new('params must be passed as a hash') unless params.is_a?(Hash)
73
53
  params = default_params.merge(params)
74
54
 
75
- raise InvalidParams.new('State parameter was invalid') unless [false, 'PUBLISHED', 'DRAFT'].include?(params[:state])
55
+ raise Hubspot::InvalidParams.new('State parameter was invalid') unless [false, 'PUBLISHED', 'DRAFT'].include?(params[:state])
76
56
  params.each { |k, v| params.delete(k) if v == false }
77
57
 
78
- url = Hubspot::Utils.generate_url(BLOG_POSTS_PATH, params)
79
-
80
- resp = HTTParty.get(url, format: :json)
81
- if resp.success?
82
- blog_post_objects = resp.parsed_response['objects']
83
- blog_post_objects.map do |blog_post_hash|
84
- BlogPost.new(blog_post_hash)
85
- end
86
- else
87
- []
88
- end
58
+ response = Hubspot::Connection.get_json(BLOG_POSTS_PATH, params)
59
+ response['objects'].map { |p| BlogPost.new(p) }
89
60
  end
90
61
  end
91
62
 
@@ -94,15 +65,10 @@ module Hubspot
94
65
 
95
66
  # Returns a specific blog post by ID
96
67
  # {https://developers.hubspot.com/docs/methods/blogv2/get_blog_posts_blog_post_id}
97
- # @return [Hubspot::BlogPost] or nil
68
+ # @return Hubspot::BlogPost
98
69
  def self.find_by_blog_post_id(id)
99
- url = Hubspot::Utils.generate_url(GET_BLOG_POST_BY_ID_PATH, blog_post_id: id)
100
- resp = HTTParty.get(url, format: :json)
101
- if resp.success?
102
- BlogPost.new(resp.parsed_response)
103
- else
104
- nil
105
- end
70
+ response = Hubspot::Connection.get_json(GET_BLOG_POST_BY_ID_PATH, { blog_post_id: id })
71
+ new(response)
106
72
  end
107
73
 
108
74
  def initialize(response_hash)
@@ -0,0 +1,95 @@
1
+ module Hubspot
2
+ class Connection
3
+ include HTTParty
4
+
5
+ class << self
6
+ def get_json(path, opts)
7
+ url = generate_url(path, opts)
8
+ response = get(url, format: :json)
9
+ raise(Hubspot::RequestError.new(response)) unless response.success?
10
+ response.parsed_response
11
+ end
12
+
13
+ def post_json(path, opts)
14
+ no_parse = opts[:params].delete(:no_parse) { false }
15
+
16
+ url = generate_url(path, opts[:params])
17
+ response = post(url, body: opts[:body].to_json, headers: { 'Content-Type' => 'application/json' }, format: :json)
18
+ raise(Hubspot::RequestError.new(response)) unless response.success?
19
+
20
+ no_parse ? response : response.parsed_response
21
+ end
22
+
23
+ def put_json(path, opts)
24
+ url = generate_url(path, opts[:params])
25
+ response = put(url, body: opts[:body].to_json, headers: { 'Content-Type' => 'application/json' }, format: :json)
26
+ raise(Hubspot::RequestError.new(response)) unless response.success?
27
+ response.parsed_response
28
+ end
29
+
30
+ def delete_json(path, opts)
31
+ url = generate_url(path, opts)
32
+ response = delete(url, format: :json)
33
+ raise(Hubspot::RequestError.new(response)) unless response.success?
34
+ response
35
+ end
36
+
37
+ protected
38
+
39
+ def generate_url(path, params={}, options={})
40
+ Hubspot::Config.ensure! :hapikey
41
+ path = path.clone
42
+ params = params.clone
43
+ base_url = options[:base_url] || Hubspot::Config.base_url
44
+ params["hapikey"] = Hubspot::Config.hapikey unless options[:hapikey] == false
45
+
46
+ if path =~ /:portal_id/
47
+ Hubspot::Config.ensure! :portal_id
48
+ params["portal_id"] = Hubspot::Config.portal_id if path =~ /:portal_id/
49
+ end
50
+
51
+ params.each do |k,v|
52
+ if path.match(":#{k}")
53
+ path.gsub!(":#{k}",v.to_s)
54
+ params.delete(k)
55
+ end
56
+ end
57
+ raise(Hubspot::MissingInterpolation.new("Interpolation not resolved")) if path =~ /:/
58
+
59
+ query = params.map do |k,v|
60
+ v.is_a?(Array) ? v.map { |value| param_string(k,value) } : param_string(k,v)
61
+ end.join("&")
62
+
63
+ path += "?" if query.present?
64
+ base_url + path + query
65
+ end
66
+
67
+ # convert into milliseconds since epoch
68
+ def converted_value(value)
69
+ value.is_a?(Time) ? (value.to_i * 1000) : value
70
+ end
71
+
72
+ def param_string(key,value)
73
+ case key
74
+ when /range/
75
+ raise "Value must be a range" unless value.is_a?(Range)
76
+ "#{key}=#{converted_value(value.begin)}&#{key}=#{converted_value(value.end)}"
77
+ when /^batch_(.*)$/
78
+ key = $1.gsub(/(_.)/) { |w| w.last.upcase }
79
+ "#{key}=#{converted_value(value)}"
80
+ else
81
+ "#{key}=#{converted_value(value)}"
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ class FormsConnection < Connection
88
+ follow_redirects false
89
+
90
+ def self.submit(path, opts)
91
+ url = generate_url(path, opts[:params], { base_url: 'https://forms.hubspot.com' })
92
+ post(url, body: opts[:body].to_json, headers: { 'Content-Type' => 'application/x-www-form-urlencoded' })
93
+ end
94
+ end
95
+ end
@@ -1,116 +1,104 @@
1
- require 'hubspot/utils'
2
- require 'httparty'
3
-
4
1
  module Hubspot
5
2
  #
6
3
  # HubSpot Contacts API
7
4
  #
8
- # {https://developers.hubspot.com/docs/endpoints#contacts-api}
5
+ # {https://developers.hubspot.com/docs/methods/contacts/contacts-overview}
9
6
  #
7
+ # TODO: work on all endpoints that can specify contact properties, property mode etc... as params. cf pending specs
10
8
  class Contact
11
- CREATE_CONTACT_PATH = "/contacts/v1/contact"
12
- GET_CONTACT_BY_EMAIL_PATH = "/contacts/v1/contact/email/:contact_email/profile"
13
- GET_CONTACT_BY_ID_PATH = "/contacts/v1/contact/vid/:contact_id/profile"
14
- GET_CONTACT_BY_UTK_PATH = "/contacts/v1/contact/utk/:contact_utk/profile"
15
- UPDATE_CONTACT_PATH = "/contacts/v1/contact/vid/:contact_id/profile"
16
- DESTROY_CONTACT_PATH = "/contacts/v1/contact/vid/:contact_id"
17
- CONTACTS_PATH = "/contacts/v1/lists/all/contacts/all"
9
+ CREATE_CONTACT_PATH = "/contacts/v1/contact"
10
+ GET_CONTACT_BY_EMAIL_PATH = "/contacts/v1/contact/email/:contact_email/profile"
11
+ GET_CONTACTS_BY_EMAIL_PATH = "/contacts/v1/contact/emails/batch"
12
+ GET_CONTACT_BY_ID_PATH = "/contacts/v1/contact/vid/:contact_id/profile"
13
+ CONTACT_BATCH_PATH = '/contacts/v1/contact/vids/batch'
14
+ GET_CONTACT_BY_UTK_PATH = "/contacts/v1/contact/utk/:contact_utk/profile"
15
+ GET_CONTACTS_BY_UTK_PATH = '/contacts/v1/contact/utks/batch'
16
+ UPDATE_CONTACT_PATH = "/contacts/v1/contact/vid/:contact_id/profile"
17
+ DESTROY_CONTACT_PATH = "/contacts/v1/contact/vid/:contact_id"
18
+ CONTACTS_PATH = "/contacts/v1/lists/all/contacts/all"
19
+ RECENT_CONTACTS_PATH = '/contacts/v1/lists/recently_updated/contacts/recent'
18
20
 
19
21
  class << self
20
- # Creates a new contact
21
22
  # {https://developers.hubspot.com/docs/methods/contacts/create_contact}
22
- # @param email [Hash] unique email of the new contact
23
- # @param params [Hash] hash of properties to set on the contact
24
- # @return [Hubspot::Contact] the newly created contact
25
23
  def create!(email, params={})
26
24
  params_with_email = params.stringify_keys.merge("email" => email)
27
- url = Hubspot::Utils.generate_url(CREATE_CONTACT_PATH)
28
25
  post_data = {properties: Hubspot::Utils.hash_to_properties(params_with_email)}
29
- resp = HTTParty.post(url, body: post_data.to_json, format: :json)
30
- raise(Hubspot::ContactExistsError.new(resp, "Contact already exists with email: #{email}")) if resp.code == 409
31
- raise(Hubspot::RequestError.new(resp, "Cannot create contact with email: #{email}")) unless resp.success?
32
- Hubspot::Contact.new(resp.parsed_response)
26
+ response = Hubspot::Connection.post_json(CREATE_CONTACT_PATH, params: {}, body: post_data )
27
+ new(response)
33
28
  end
34
29
 
35
- # Finds a contact by email
36
- # {https://developers.hubspot.com/docs/methods/contacts/get_contact_by_email}
37
- # @param email [String] the email of the contact to find
38
- # @return [Hubspot::Contact, nil] the contact found or nil
39
- def find_by_email(email)
40
- url = Hubspot::Utils.generate_url(GET_CONTACT_BY_EMAIL_PATH, {contact_email: email})
41
- resp = HTTParty.get(url, format: :json)
42
- if resp.success?
43
- Hubspot::Contact.new(resp.parsed_response)
44
- else
45
- nil
30
+ # {https://developers.hubspot.com/docs/methods/contacts/get_contacts}
31
+ # {https://developers.hubspot.com/docs/methods/contacts/get_recently_updated_contacts}
32
+ def all(opts={})
33
+ recent = opts.delete(:recent) { false }
34
+ path, opts =
35
+ if recent
36
+ [RECENT_CONTACTS_PATH, Hubspot::ContactProperties.add_default_parameters(opts)]
37
+ else
38
+ [CONTACTS_PATH, opts]
46
39
  end
47
- end
48
40
 
49
- # Finds a contact by vid
50
- # @param vid [String] the vid of the contact to find
51
- # @return [Hubspot::Contact, nil] the contact found or nil
52
- def find_by_id(vid)
53
- url = Hubspot::Utils.generate_url(GET_CONTACT_BY_ID_PATH, {contact_id: vid})
54
- resp = HTTParty.get(url, format: :json)
55
- if resp.success?
56
- Hubspot::Contact.new(resp.parsed_response)
57
- else
58
- nil
59
- end
41
+ response = Hubspot::Connection.get_json(path, opts)
42
+ response['contacts'].map { |c| new(c) }
60
43
  end
61
44
 
62
- # Finds a contact by its User Token (hubspotutk cookie value)
63
- # {https://developers.hubspot.com/docs/methods/contacts/get_contact_by_utk}
64
- # @param utk [String] hubspotutk cookie value
65
- # @return [Hubspot::Contact, nil] the contact found or nil
66
- def find_by_utk(utk)
67
- url = Hubspot::Utils.generate_url(GET_CONTACT_BY_UTK_PATH, {contact_utk: utk})
68
- resp = HTTParty.get(url, format: :json)
69
- if resp.success?
70
- Hubspot::Contact.new(resp.parsed_response)
71
- else
72
- nil
45
+ # TODO: create or update a contact
46
+ # PATH /contacts/v1/contact/createOrUpdate/email/:contact_email
47
+ # API endpoint: https://developers.hubspot.com/docs/methods/contacts/create_or_update
48
+ # + batch mode: https://developers.hubspot.com/docs/methods/contacts/batch_create_or_update
49
+
50
+ # NOTE: problem with batch api endpoint
51
+ # {https://developers.hubspot.com/docs/methods/contacts/get_contact}
52
+ # {https://developers.hubspot.com/docs/methods/contacts/get_batch_by_vid}
53
+ def find_by_id(vids)
54
+ batch_mode, path, params = case vids
55
+ when Integer then [false, GET_CONTACT_BY_ID_PATH, { contact_id: vids }]
56
+ when Array then [true, CONTACT_BATCH_PATH, { batch_vid: vids }]
57
+ else raise Hubspot::InvalidParams, 'expecting Integer or Array of Integers parameter'
73
58
  end
74
- end
75
59
 
76
- # TODO: Get all contacts
77
- # {https://developers.hubspot.com/docs/methods/contacts/get_contacts}
78
- # @param count [Fixnum] number of contacts per page; default 20; max 100
79
- # @param vidOffset [Fixnum] page through the contacts
80
- # @return [Hubspot::ContactCollection] the paginated collection of contacts
81
- def all(opts = {})
82
- url = Hubspot::Utils.generate_url(CONTACTS_PATH, opts)
83
- request = HTTParty.get(url, format: :json)
60
+ response = Hubspot::Connection.get_json(path, params)
61
+ raise Hubspot::ApiError if batch_mode
62
+ new(response)
63
+ end
84
64
 
85
- raise(Hubspot::RequestError.new(request)) unless request.success?
65
+ # {https://developers.hubspot.com/docs/methods/contacts/get_contact_by_email}
66
+ # {https://developers.hubspot.com/docs/methods/contacts/get_batch_by_email}
67
+ def find_by_email(emails)
68
+ batch_mode, path, params = case emails
69
+ when String then [false, GET_CONTACT_BY_EMAIL_PATH, { contact_email: emails }]
70
+ when Array then [true, GET_CONTACTS_BY_EMAIL_PATH, { batch_email: emails }]
71
+ else raise Hubspot::InvalidParams, 'expecting String or Array of Strings parameter'
72
+ end
86
73
 
87
- found = request.parsed_response['contacts']
88
- return found.map{|h| new(h) }
74
+ response = Hubspot::Connection.get_json(path, params)
75
+ if batch_mode
76
+ #TODO: transform response
77
+ response
78
+ else
79
+ new(response)
80
+ end
89
81
  end
90
82
 
91
- # TODO: Get recently updated and created contacts
92
- # {https://developers.hubspot.com/docs/methods/contacts/get_recently_updated_contacts}
93
- # @param count [Fixnum] number of contacts per page; max 100
94
- # @return [Hubspot::ContactCollection] the paginated collection of contacts
95
- def recent(count=100)
96
- raise NotImplementedError
83
+ # NOTE: problem with batch api endpoint
84
+ # {https://developers.hubspot.com/docs/methods/contacts/get_contact_by_utk}
85
+ # {https://developers.hubspot.com/docs/methods/contacts/get_batch_by_utk}
86
+ def find_by_utk(utks)
87
+ batch_mode, path, params = case utks
88
+ when String then [false, GET_CONTACT_BY_UTK_PATH, { contact_utk: utks }]
89
+ when Array then [true, GET_CONTACTS_BY_UTK_PATH, { batch_utk: utks }]
90
+ else raise Hubspot::InvalidParams, 'expecting String or Array of Strings parameter'
91
+ end
92
+
93
+ response = Hubspot::Connection.get_json(path, params)
94
+ raise Hubspot::ApiError if batch_mode
95
+ new(response)
97
96
  end
98
97
 
99
- # TODO: Search for contacts by various crieria
100
98
  # {https://developers.hubspot.com/docs/methods/contacts/search_contacts}
101
- # @param query [String] The search term for what you're searching for
102
- # @param count [Fixnum] number of contacts per page; max 100
103
- # @return [Hubspot::ContactCollection] the collection of contacts; no pagination
104
99
  def search(query, count=100)
105
100
  raise NotImplementedError
106
101
  end
107
-
108
- # TODO: Get statistics about all contacts
109
- # {https://developers.hubspot.com/docs/methods/contacts/get_contact_statistics}
110
- # @return [Hash] hash of statistics
111
- def statistics
112
- raise NotImplementedError
113
- end
114
102
  end
115
103
 
116
104
  attr_reader :properties
@@ -138,11 +126,8 @@ module Hubspot
138
126
  # @param params [Hash] hash of properties to update
139
127
  # @return [Hubspot::Contact] self
140
128
  def update!(params)
141
- params.stringify_keys!
142
- url = Hubspot::Utils.generate_url(UPDATE_CONTACT_PATH, {contact_id: vid})
143
- query = {"properties" => Hubspot::Utils.hash_to_properties(params)}
144
- resp = HTTParty.post(url, body: query.to_json, format: :json)
145
- raise(Hubspot::RequestError.new(resp)) unless resp.success?
129
+ query = {"properties" => Hubspot::Utils.hash_to_properties(params.stringify_keys!)}
130
+ response = Hubspot::Connection.post_json(UPDATE_CONTACT_PATH, params: { contact_id: vid }, body: query)
146
131
  @properties.merge!(params)
147
132
  self
148
133
  end
@@ -151,9 +136,7 @@ module Hubspot
151
136
  # {https://developers.hubspot.com/docs/methods/contacts/delete_contact}
152
137
  # @return [TrueClass] true
153
138
  def destroy!
154
- url = Hubspot::Utils.generate_url(DESTROY_CONTACT_PATH, {contact_id: vid})
155
- resp = HTTParty.delete(url, format: :json)
156
- raise(Hubspot::RequestError.new(resp)) unless resp.success?
139
+ response = Hubspot::Connection.delete_json(DESTROY_CONTACT_PATH, { contact_id: vid })
157
140
  @destroyed = true
158
141
  end
159
142