hubspot-ruby 0.1.8 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -22
  3. data/Gemfile.lock +14 -11
  4. data/LICENSE.txt +0 -2
  5. data/README.md +8 -6
  6. data/RELEASING.md +4 -5
  7. data/Rakefile +2 -17
  8. data/hubspot-ruby.gemspec +20 -157
  9. data/lib/hubspot-ruby.rb +10 -0
  10. data/lib/hubspot/blog.rb +1 -2
  11. data/lib/hubspot/company.rb +149 -0
  12. data/lib/hubspot/company_properties.rb +59 -0
  13. data/lib/hubspot/config.rb +10 -4
  14. data/lib/hubspot/connection.rb +12 -4
  15. data/lib/hubspot/contact.rb +52 -31
  16. data/lib/hubspot/contact_list.rb +16 -16
  17. data/lib/hubspot/contact_properties.rb +53 -6
  18. data/lib/hubspot/deal.rb +2 -2
  19. data/lib/hubspot/deal_pipeline.rb +43 -0
  20. data/lib/hubspot/deal_properties.rb +59 -0
  21. data/lib/hubspot/engagement.rb +133 -0
  22. data/lib/hubspot/exceptions.rb +1 -1
  23. data/lib/hubspot/form.rb +20 -20
  24. data/lib/hubspot/owner.rb +49 -0
  25. data/lib/hubspot/properties.rb +119 -0
  26. data/lib/hubspot/railtie.rb +10 -0
  27. data/lib/hubspot/topic.rb +2 -2
  28. data/lib/hubspot/utils.rb +106 -3
  29. data/spec/lib/hubspot/blog_spec.rb +2 -0
  30. data/spec/lib/hubspot/company_properties_spec.rb +239 -0
  31. data/spec/lib/hubspot/company_spec.rb +190 -0
  32. data/spec/lib/hubspot/config_spec.rb +1 -1
  33. data/spec/lib/hubspot/connection_spec.rb +33 -27
  34. data/spec/lib/hubspot/contact_list_spec.rb +61 -61
  35. data/spec/lib/hubspot/contact_properties_spec.rb +234 -3
  36. data/spec/lib/hubspot/contact_spec.rb +140 -71
  37. data/spec/lib/hubspot/deal_properties_spec.rb +254 -0
  38. data/spec/lib/hubspot/engagement_spec.rb +87 -0
  39. data/spec/lib/hubspot/form_spec.rb +72 -60
  40. data/spec/lib/hubspot/owner_spec.rb +56 -0
  41. data/spec/lib/hubspot/properties_spec.rb +45 -0
  42. data/spec/lib/hubspot/topic_spec.rb +0 -1
  43. data/spec/lib/hubspot/utils_spec.rb +113 -15
  44. data/spec/lib/tasks/properties_spec.rb +90 -0
  45. data/spec/live/companies_integration_spec.rb +23 -0
  46. data/spec/live/companies_properties_integration_spec.rb +120 -0
  47. data/spec/live/contacts_integration_spec.rb +1 -1
  48. data/spec/live/contacts_properties_integration_spec.rb +120 -0
  49. data/spec/live/deal_properties_integration_spec.rb +123 -0
  50. data/spec/live/deals_integration_spec.rb +1 -1
  51. data/spec/support/cassette_helper.rb +1 -1
  52. data/spec/support/tests_helper.rb +3 -3
  53. metadata +39 -78
  54. data/.document +0 -5
  55. data/lib/hubspot/version.rb +0 -9
  56. data/spec/fixtures/vcr_cassettes/add_contacts_to_lists.yml +0 -281
  57. data/spec/fixtures/vcr_cassettes/blog_list.yml +0 -180
  58. data/spec/fixtures/vcr_cassettes/blog_posts.yml +0 -107
  59. data/spec/fixtures/vcr_cassettes/blog_posts_list.yml +0 -6463
  60. data/spec/fixtures/vcr_cassettes/contact_create.yml +0 -64
  61. data/spec/fixtures/vcr_cassettes/contact_create_existing_email.yml +0 -62
  62. data/spec/fixtures/vcr_cassettes/contact_create_invalid_email.yml +0 -63
  63. data/spec/fixtures/vcr_cassettes/contact_create_with_params.yml +0 -64
  64. data/spec/fixtures/vcr_cassettes/contact_destroy.yml +0 -131
  65. data/spec/fixtures/vcr_cassettes/contact_example.yml +0 -32
  66. data/spec/fixtures/vcr_cassettes/contact_find_by_email.yml +0 -59
  67. data/spec/fixtures/vcr_cassettes/contact_find_by_email_batch_mode.yml +0 -509
  68. data/spec/fixtures/vcr_cassettes/contact_find_by_id.yml +0 -59
  69. data/spec/fixtures/vcr_cassettes/contact_find_by_id_batch_mode.yml +0 -33
  70. data/spec/fixtures/vcr_cassettes/contact_find_by_utk.yml +0 -83
  71. data/spec/fixtures/vcr_cassettes/contact_find_by_utk_batch_mode.yml +0 -33
  72. data/spec/fixtures/vcr_cassettes/contact_list_batch_find.yml +0 -65
  73. data/spec/fixtures/vcr_cassettes/contact_list_destroy.yml +0 -63
  74. data/spec/fixtures/vcr_cassettes/contact_list_example.yml +0 -33
  75. data/spec/fixtures/vcr_cassettes/contact_list_find.yml +0 -96
  76. data/spec/fixtures/vcr_cassettes/contact_list_refresh.yml +0 -33
  77. data/spec/fixtures/vcr_cassettes/contact_list_update.yml +0 -36
  78. data/spec/fixtures/vcr_cassettes/contact_update.yml +0 -66
  79. data/spec/fixtures/vcr_cassettes/contacts_among_list.yml +0 -189
  80. data/spec/fixtures/vcr_cassettes/create_form.yml +0 -39
  81. data/spec/fixtures/vcr_cassettes/create_list.yml +0 -36
  82. data/spec/fixtures/vcr_cassettes/create_list_with_filters.yml +0 -36
  83. data/spec/fixtures/vcr_cassettes/deal_create.yml +0 -61
  84. data/spec/fixtures/vcr_cassettes/deal_example.yml +0 -166
  85. data/spec/fixtures/vcr_cassettes/deal_find.yml +0 -115
  86. data/spec/fixtures/vcr_cassettes/destroy_deal.yml +0 -221
  87. data/spec/fixtures/vcr_cassettes/fail_to_create_form.yml +0 -35
  88. data/spec/fixtures/vcr_cassettes/fail_to_create_list.yml +0 -35
  89. data/spec/fixtures/vcr_cassettes/field_among_form.yml +0 -34
  90. data/spec/fixtures/vcr_cassettes/fields_among_form.yml +0 -35
  91. data/spec/fixtures/vcr_cassettes/find_all_contacts.yml +0 -297
  92. data/spec/fixtures/vcr_cassettes/find_all_dynamic_lists.yml +0 -104
  93. data/spec/fixtures/vcr_cassettes/find_all_forms.yml +0 -15378
  94. data/spec/fixtures/vcr_cassettes/find_all_lists.yml +0 -138
  95. data/spec/fixtures/vcr_cassettes/find_all_recent_contacts.yml +0 -33
  96. data/spec/fixtures/vcr_cassettes/find_all_recent_updated_deals.yml +0 -130
  97. data/spec/fixtures/vcr_cassettes/find_all_stastic_lists.yml +0 -21876
  98. data/spec/fixtures/vcr_cassettes/form_destroy.yml +0 -64
  99. data/spec/fixtures/vcr_cassettes/form_example.yml +0 -39
  100. data/spec/fixtures/vcr_cassettes/form_find.yml +0 -69
  101. data/spec/fixtures/vcr_cassettes/form_post.yml +0 -31
  102. data/spec/fixtures/vcr_cassettes/form_submit_data.yml +0 -130
  103. data/spec/fixtures/vcr_cassettes/form_update.yml +0 -77
  104. data/spec/fixtures/vcr_cassettes/one_month_blog_posts_filter_state.yml +0 -19127
  105. data/spec/fixtures/vcr_cassettes/one_month_blog_posts_list.yml +0 -34963
  106. data/spec/fixtures/vcr_cassettes/remove_contacts_from_lists.yml +0 -315
  107. data/spec/fixtures/vcr_cassettes/topic_list.yml +0 -48
  108. data/spec/fixtures/vcr_cassettes/topics_list.yml +0 -86
@@ -56,7 +56,7 @@ module Hubspot
56
56
  # {https://developers.hubspot.com/docs/methods/contacts/delete_contact}
57
57
  # @return [TrueClass] true
58
58
  def destroy!
59
- response = Hubspot::Connection.delete_json(DEAL_PATH, {deal_id: deal_id})
59
+ Hubspot::Connection.delete_json(DEAL_PATH, {deal_id: deal_id})
60
60
  @destroyed = true
61
61
  end
62
62
 
@@ -74,7 +74,7 @@ module Hubspot
74
74
  # @return [Hubspot::Deal] self
75
75
  def update!(params)
76
76
  query = {"properties" => Hubspot::Utils.hash_to_properties(params.stringify_keys!, key_name: 'name')}
77
- response = Hubspot::Connection.put_json(UPDATE_DEAL_PATH, params: { deal_id: deal_id }, body: query)
77
+ Hubspot::Connection.put_json(UPDATE_DEAL_PATH, params: { deal_id: deal_id }, body: query)
78
78
  @properties.merge!(params)
79
79
  self
80
80
  end
@@ -0,0 +1,43 @@
1
+ require 'hubspot/utils'
2
+
3
+ module Hubspot
4
+ #
5
+ # HubSpot Deals API
6
+ #
7
+ # {http://developers.hubspot.com/docs/methods/deal-pipelines/overview}
8
+ #
9
+ class DealPipeline
10
+ PIPELINES_PATH = "/deals/v1/pipelines"
11
+ PIPELINE_PATH = "/deals/v1/pipelines/:pipeline_id"
12
+
13
+ attr_reader :active
14
+ attr_reader :display_order
15
+ attr_reader :label
16
+ attr_reader :pipeline_id
17
+ attr_reader :stages
18
+
19
+ def initialize(response_hash)
20
+ @active = response_hash["active"]
21
+ @display_order = response_hash["displayOrder"]
22
+ @label = response_hash["label"]
23
+ @pipeline_id = response_hash["pipelineId"]
24
+ @stages = response_hash["stages"]
25
+ end
26
+
27
+ class << self
28
+ def find(pipeline_id)
29
+ response = Hubspot::Connection.get_json(PIPELINE_PATH, { pipeline_id: pipeline_id })
30
+ new(response)
31
+ end
32
+
33
+ def all
34
+ response = Hubspot::Connection.get_json(PIPELINES_PATH, {})
35
+ response.map { |p| new(p) }
36
+ end
37
+ end
38
+
39
+ def [](stage)
40
+ @stages[stage]
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,59 @@
1
+ module Hubspot
2
+ class DealProperties < Properties
3
+
4
+ ALL_PROPERTIES_PATH = '/deals/v1/properties'
5
+ ALL_GROUPS_PATH = '/deals/v1/groups'
6
+ CREATE_PROPERTY_PATH = '/deals/v1/properties/'
7
+ UPDATE_PROPERTY_PATH = '/deals/v1/properties/named/:property_name'
8
+ DELETE_PROPERTY_PATH = '/deals/v1/properties/named/:property_name'
9
+ CREATE_GROUP_PATH = '/deals/v1/groups/'
10
+ UPDATE_GROUP_PATH = '/deals/v1/groups/named/:group_name'
11
+ DELETE_GROUP_PATH = '/deals/v1/groups/named/:group_name'
12
+
13
+ class << self
14
+ def add_default_parameters(opts={})
15
+ superclass.add_default_parameters(opts)
16
+ end
17
+
18
+ def all(opts={}, filter={})
19
+ superclass.all(ALL_PROPERTIES_PATH, opts, filter)
20
+ end
21
+
22
+ def groups(opts={}, filter={})
23
+ superclass.groups(ALL_GROUPS_PATH, opts, filter)
24
+ end
25
+
26
+ def create!(params={})
27
+ superclass.create!(CREATE_PROPERTY_PATH, params)
28
+ end
29
+
30
+ def update!(property_name, params={})
31
+ superclass.update!(UPDATE_PROPERTY_PATH, property_name, params)
32
+ end
33
+
34
+ def delete!(property_name)
35
+ superclass.delete!(DELETE_PROPERTY_PATH, property_name)
36
+ end
37
+
38
+ def create_group!(params={})
39
+ superclass.create_group!(CREATE_GROUP_PATH, params)
40
+ end
41
+
42
+ def update_group!(group_name, params={})
43
+ superclass.update_group!(UPDATE_GROUP_PATH, group_name, params)
44
+ end
45
+
46
+ def delete_group!(group_name)
47
+ superclass.delete_group!(DELETE_GROUP_PATH, group_name)
48
+ end
49
+
50
+ def same?(src, dst)
51
+ superclass.same?(src, dst)
52
+ end
53
+
54
+ def valid_params(params)
55
+ superclass.valid_params(params)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,133 @@
1
+ require 'hubspot/utils'
2
+
3
+ module Hubspot
4
+ #
5
+ # HubSpot Engagements API
6
+ #
7
+ # {http://developers.hubspot.com/docs/methods/engagements/create_engagement}
8
+ #
9
+ class Engagement
10
+ CREATE_ENGAGMEMENT_PATH = '/engagements/v1/engagements'
11
+ ENGAGEMENT_PATH = '/engagements/v1/engagements/:engagement_id'
12
+ GET_ASSOCIATED_ENGAGEMENTS = '/engagements/v1/engagements/associated/:objectType/:objectId/paged'
13
+
14
+ attr_reader :id
15
+ attr_reader :engagement
16
+ attr_reader :associations
17
+ attr_reader :metadata
18
+
19
+ def initialize(response_hash)
20
+
21
+ @engagement = response_hash["engagement"]
22
+ @associations = response_hash["associations"]
23
+ @metadata = response_hash["metadata"]
24
+ @id = engagement["id"]
25
+ end
26
+
27
+ class << self
28
+ def create!(params={})
29
+ response = Hubspot::Connection.post_json(CREATE_ENGAGMEMENT_PATH, params: {}, body: params )
30
+ new(HashWithIndifferentAccess.new(response))
31
+ end
32
+
33
+ def find(engagement_id)
34
+ begin
35
+ response = Hubspot::Connection.get_json(ENGAGEMENT_PATH, { engagement_id: engagement_id })
36
+ new(HashWithIndifferentAccess.new(response))
37
+ rescue Hubspot::RequestError => ex
38
+ if ex.response.code == 404
39
+ return nil
40
+ else
41
+ raise ex
42
+ end
43
+ end
44
+ end
45
+
46
+ def find_by_company(company_id)
47
+ find_by_association company_id, 'COMPANY'
48
+ end
49
+
50
+ def find_by_contact(contact_id)
51
+ find_by_association contact_id, 'CONTACT'
52
+ end
53
+
54
+ def find_by_association(association_id, association_type)
55
+ path = GET_ASSOCIATED_ENGAGEMENTS
56
+ params = { objectType: association_type, objectId: association_id }
57
+ raise Hubspot::InvalidParams, 'expecting Integer parameter' unless association_id.try(:is_a?, Integer)
58
+ raise Hubspot::InvalidParams, 'expecting String parameter' unless association_type.try(:is_a?, String)
59
+
60
+ engagements = []
61
+ begin
62
+ response = Hubspot::Connection.get_json(path, params)
63
+ engagements = response["results"].try(:map) { |engagement| new(engagement) }
64
+ rescue => e
65
+ raise e unless e.message =~ /not found/
66
+ end
67
+ engagements
68
+ end
69
+ end
70
+
71
+ # Archives the engagement in hubspot
72
+ # {http://developers.hubspot.com/docs/methods/engagements/delete-engagement}
73
+ # @return [TrueClass] true
74
+ def destroy!
75
+ Hubspot::Connection.delete_json(ENGAGEMENT_PATH, {engagement_id: id})
76
+ @destroyed = true
77
+ end
78
+
79
+ def destroyed?
80
+ !!@destroyed
81
+ end
82
+
83
+ def [](property)
84
+ @properties[property]
85
+ end
86
+
87
+ # Updates the properties of an engagement
88
+ # {http://developers.hubspot.com/docs/methods/engagements/update_engagement}
89
+ # @param params [Hash] hash of properties to update
90
+ # @return [Hubspot::Engagement] self
91
+ def update!(params)
92
+ data = {
93
+ engagement: engagement,
94
+ associations: associations,
95
+ metadata: metadata
96
+ }
97
+
98
+ Hubspot::Connection.put_json(ENGAGEMENT_PATH, params: { engagement_id: id }, body: data)
99
+ self
100
+ end
101
+ end
102
+
103
+ class EngagementNote < Engagement
104
+ def body
105
+ metadata['body']
106
+ end
107
+
108
+ def contact_ids
109
+ associations['contactIds']
110
+ end
111
+
112
+ class << self
113
+ def create!(contact_id, note_body, owner_id = nil)
114
+ data = {
115
+ engagement: {
116
+ type: 'NOTE'
117
+ },
118
+ associations: {
119
+ contactIds: [contact_id]
120
+ },
121
+ metadata: {
122
+ body: note_body
123
+ }
124
+ }
125
+
126
+ # if the owner id has been provided, append it to the engagement
127
+ data[:engagement][:owner_id] = owner_id if owner_id
128
+
129
+ super(data)
130
+ end
131
+ end
132
+ end
133
+ end
@@ -15,4 +15,4 @@ module Hubspot
15
15
  class ContactExistsError < RequestError; end
16
16
  class InvalidParams < StandardError; end
17
17
  class ApiError < StandardError; end
18
- end
18
+ end
@@ -5,16 +5,16 @@ module Hubspot
5
5
  # {https://developers.hubspot.com/docs/methods/forms/forms_overview}
6
6
  #
7
7
  class Form
8
- FORMS_PATH = '/contacts/v1/forms'
9
- FORM_PATH = '/contacts/v1/forms/:form_guid'
10
- FIELDS_PATH = '/contacts/v1/fields/:form_guid'
11
- FIELD_PATH = FIELDS_PATH + '/:field_name'
12
- SUBMIT_DATA_PATH = '/uploads/form/v2/:portal_id/:form_guid'
8
+ FORMS_PATH = '/forms/v2/forms' # '/contacts/v1/forms'
9
+ FORM_PATH = '/forms/v2/forms/:form_guid' # '/contacts/v1/forms/:form_guid'
10
+ FIELDS_PATH = '/forms/v2/fields/:form_guid' # '/contacts/v1/fields/:form_guid'
11
+ FIELD_PATH = FIELDS_PATH + '/:field_name'
12
+ SUBMIT_DATA_PATH = '/uploads/form/v2/:portal_id/:form_guid'
13
13
 
14
14
  class << self
15
15
  # {https://developers.hubspot.com/docs/methods/forms/create_form}
16
16
  def create!(opts={})
17
- response = Hubspot::Connection.post_json(FORMS_PATH, params: {}, body: opts)
17
+ response = Hubspot::Connection.post_json(FORMS_PATH, params: {}, body: opts)
18
18
  new(response)
19
19
  end
20
20
 
@@ -35,7 +35,7 @@ module Hubspot
35
35
  attr_reader :properties
36
36
 
37
37
  def initialize(hash)
38
- self.send(:assign_properties, hash)
38
+ self.send(:assign_properties, hash)
39
39
  end
40
40
 
41
41
  # {https://developers.hubspot.com/docs/methods/forms/get_fields}
@@ -45,26 +45,26 @@ module Hubspot
45
45
  field_name = opts.delete(:only) { nil }
46
46
 
47
47
  if field_name
48
- field_name = field_name.to_s
49
- if bypass_cache || @fields.nil? || @fields.empty?
50
- response = Hubspot::Connection.get_json(FIELD_PATH, { form_guid: @guid, field_name: field_name })
51
- response
52
- else
53
- @fields.detect { |f| f['name'] == field_name }
54
- end
48
+ field_name = field_name.to_s
49
+ if bypass_cache || @fields.nil? || @fields.empty?
50
+ response = Hubspot::Connection.get_json(FIELD_PATH, { form_guid: @guid, field_name: field_name })
51
+ response
52
+ else
53
+ @fields.detect { |f| f['name'] == field_name }
54
+ end
55
55
  else
56
- if bypass_cache || @fields.nil? || @fields.empty?
56
+ if bypass_cache || @fields.nil? || @fields.empty?
57
57
  response = Hubspot::Connection.get_json(FIELDS_PATH, { form_guid: @guid })
58
58
  @fields = response
59
59
  end
60
60
  @fields
61
61
  end
62
62
  end
63
-
63
+
64
64
  # {https://developers.hubspot.com/docs/methods/forms/submit_form}
65
- def submit(opts={})
65
+ def submit(opts={})
66
66
  response = Hubspot::FormsConnection.submit(SUBMIT_DATA_PATH, params: { form_guid: @guid }, body: opts)
67
- [204, 302].include?(response.code)
67
+ [204, 302, 200].include?(response.code)
68
68
  end
69
69
 
70
70
  # {https://developers.hubspot.com/docs/methods/forms/update_form}
@@ -88,8 +88,8 @@ module Hubspot
88
88
 
89
89
  def assign_properties(hash)
90
90
  @guid = hash['guid']
91
- @fields = hash['fields']
91
+ @fields = hash['formFieldGroups'].inject([]){ |result, fg| result | fg['fields'] }
92
92
  @properties = hash
93
93
  end
94
94
  end
95
- end
95
+ end
@@ -0,0 +1,49 @@
1
+ module Hubspot
2
+ #
3
+ # HubSpot Owners API
4
+ #
5
+ # {http://developers.hubspot.com/docs/methods/owners/get_owners}
6
+ #
7
+ # TODO: Create an Owner
8
+ # TODO: Update an Owner
9
+ # TODO: Delete an Owner
10
+ class Owner
11
+ GET_OWNERS_PATH = '/owners/v2/owners' # GET
12
+ CREATE_OWNER_PATH = '/owners/v2/owners' # POST
13
+ UPDATE_OWNER_PATH = '/owners/v2/owners/:owner_id' # PUT
14
+ DELETE_OWNER_PATH = '/owners/v2/owners/:owner_id' # DELETE
15
+
16
+
17
+ attr_reader :properties, :owner_id, :email
18
+
19
+ def initialize(property_hash)
20
+ @properties = property_hash
21
+ @owner_id = @properties['ownerId']
22
+ @email = @properties['email']
23
+ end
24
+
25
+ def [](property)
26
+ @properties[property]
27
+ end
28
+
29
+ class << self
30
+ def all(include_inactive=false)
31
+ path = GET_OWNERS_PATH
32
+ params = { includeInactive: include_inactive }
33
+ response = Hubspot::Connection.get_json(path, params)
34
+ response.map { |r| new(r) }
35
+ end
36
+
37
+ def find_by_email(email, include_inactive=false)
38
+ path = GET_OWNERS_PATH
39
+ params = { email: email, includeInactive: include_inactive }
40
+ response = Hubspot::Connection.get_json(path, params)
41
+ response.blank? ? nil : new(response.first)
42
+ end
43
+
44
+ def find_by_emails(emails, include_inactive=false)
45
+ emails.map { |email| find_by_email(email, include_inactive) }.reject(&:blank?)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,119 @@
1
+ module Hubspot
2
+ class Properties
3
+
4
+ PROPERTY_SPECS = {
5
+ group_field_names: %w(name displayName displayOrder properties),
6
+ field_names: %w(name groupName description fieldType formField type displayOrder label options),
7
+ valid_field_types: %w(textarea select text date file number radio checkbox),
8
+ valid_types: %w(string number bool datetime enumeration),
9
+ options: %w(description value label hidden displayOrder)
10
+ }
11
+
12
+ class << self
13
+ # TODO: properties can be set as configuration
14
+ # TODO: find the way how to set a list of Properties + merge same property key if present from opts
15
+ def add_default_parameters(opts={})
16
+ properties = 'email'
17
+ opts.merge(property: properties)
18
+ end
19
+
20
+ def all(path, opts={}, filter={})
21
+ response = Hubspot::Connection.get_json(path, opts)
22
+ filter_results(response, :groupName, filter[:include], filter[:exclude])
23
+ end
24
+
25
+ def groups(path, opts={}, filter={})
26
+ response = Hubspot::Connection.get_json(path, opts)
27
+ filter_results(response, :name, filter[:include], filter[:exclude])
28
+ end
29
+
30
+ def create!(path, params={})
31
+ post_data = valid_property_params(params)
32
+ return nil if post_data.blank?
33
+ Hubspot::Connection.post_json(path, params: {}, body: post_data)
34
+ end
35
+
36
+ def update!(path, property_name, params={})
37
+ post_data = valid_property_params(params)
38
+ return nil if post_data.blank?
39
+ Hubspot::Connection.put_json(path, params: { property_name: property_name }, body: post_data)
40
+ end
41
+
42
+ def delete!(path, property_name)
43
+ response = Hubspot::Connection.delete_json(path, property_name: property_name)
44
+ response.parsed_response
45
+ end
46
+
47
+ def create_group!(path, params={})
48
+ post_data = valid_group_params(params)
49
+ return nil if post_data.blank?
50
+ Hubspot::Connection.post_json(path, params: {}, body: post_data)
51
+ end
52
+
53
+ def update_group!(path, group_name, params={})
54
+ post_data = valid_group_params(params)
55
+ return nil if post_data.blank?
56
+ Hubspot::Connection.put_json(path, params: { group_name: group_name }, body: post_data)
57
+ end
58
+
59
+ def delete_group!(path, group_name)
60
+ response = Hubspot::Connection.delete_json(path, group_name: group_name)
61
+ response.parsed_response
62
+ end
63
+
64
+ def same?(src, dst)
65
+ src_params = valid_params(src)
66
+ dst_params = valid_params(dst)
67
+ src_params.eql?(dst_params)
68
+ # hash_same?(src_params, dst_params)
69
+ end
70
+
71
+ def valid_params(params={})
72
+ valid_property_params(params)
73
+ end
74
+
75
+ private
76
+
77
+ def filter_results(results, key, include, exclude)
78
+ key = key.to_s
79
+ results.select { |result|
80
+ (include.blank? || include.include?(result[key])) &&
81
+ (exclude.blank? || !exclude.include?(result[key]))
82
+ }
83
+ end
84
+
85
+ def valid_property_params(params)
86
+ return {} if params.blank?
87
+ result = params.slice(*PROPERTY_SPECS[:field_names])
88
+ result.delete('fieldType') unless check_field_type(result['fieldType'])
89
+ result.delete('type') unless check_type(result['type'])
90
+ result['options'] = valid_option_params(result['options'])
91
+ result
92
+ end
93
+
94
+ def valid_group_params(params)
95
+ return {} if params.blank?
96
+ result = params.slice(*PROPERTY_SPECS[:group_field_names])
97
+ result['properties'] = valid_property_params(result['properties']) unless result['properties'].blank?
98
+ result
99
+ end
100
+
101
+ def check_field_type(val)
102
+ return true if PROPERTY_SPECS[:valid_field_types].include?(val)
103
+ puts "Invalid field type: #{val}"
104
+ false
105
+ end
106
+
107
+ def check_type(val)
108
+ return true if PROPERTY_SPECS[:valid_types].include?(val)
109
+ puts "Invalid type: #{val}"
110
+ false
111
+ end
112
+
113
+ def valid_option_params(options)
114
+ return [] if options.blank?
115
+ options.map { |o| o.slice(*PROPERTY_SPECS[:options]) }
116
+ end
117
+ end
118
+ end
119
+ end