Skip to content

Commit 8eb0a64

Browse files
authored
Add update methods for resources and tests (#194)
* Add update methods for resources and tests * Ignore lock files generated by Appraisal * Fix class attribute defaults for older versions of ActiveSupport
1 parent dd1573a commit 8eb0a64

81 files changed

Lines changed: 3048 additions & 3857 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ doc
1313
.bundle
1414

1515
Gemfile.lock
16+
gemfiles/*.lock
1617

1718
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
1819
#

lib/hubspot/connection.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,23 @@ def post_json(path, opts)
1414
no_parse = opts[:params].delete(:no_parse) { false }
1515

1616
url = generate_url(path, opts[:params])
17-
response = post(url, { body: opts[:body].to_json, headers: { 'Content-Type' => 'application/json' }, format: :json, read_timeout: read_timeout(opts), open_timeout: open_timeout(opts) })
17+
response = post(
18+
url,
19+
body: opts[:body].to_json,
20+
headers: { 'Content-Type' => 'application/json' },
21+
format: :json,
22+
read_timeout: read_timeout(opts),
23+
open_timeout: open_timeout(opts)
24+
)
25+
1826
log_request_and_response url, response, opts[:body]
1927
raise(Hubspot::RequestError.new(response)) unless response.success?
2028

2129
no_parse ? response : response.parsed_response
2230
end
2331

2432
def put_json(path, options)
33+
no_parse = options[:params].delete(:no_parse) { false }
2534
url = generate_url(path, options[:params])
2635

2736
response = put(
@@ -34,7 +43,9 @@ def put_json(path, options)
3443
)
3544

3645
log_request_and_response(url, response, options[:body])
37-
handle_response(response)
46+
raise(Hubspot::RequestError.new(response)) unless response.success?
47+
48+
no_parse ? response : response.parsed_response
3849
end
3950

4051
def delete_json(path, opts)

lib/hubspot/resource.rb

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
module Hubspot
22
class Resource
33

4-
class_attribute :id_field, instance_writer: false, default: "id"
5-
class_attribute :property_name_field, instance_writer: false, default: "property"
6-
class_attribute :update_method, instance_writer: false, default: "put"
4+
class_attribute :id_field, instance_writer: false
5+
class_attribute :property_name_field, instance_writer: false
6+
class_attribute :update_method, instance_writer: false
7+
8+
self.id_field = "id"
9+
self.property_name_field = "property"
10+
self.update_method = "put"
711

812
class << self
913
def from_result(result)
@@ -24,6 +28,28 @@ def create(properties = {})
2428
response = Hubspot::Connection.post_json(create_path, params: {}, body: request)
2529
from_result(response)
2630
end
31+
32+
def update(id, properties = {})
33+
begin
34+
update!(id, properties)
35+
rescue Hubspot::RequestError => e
36+
false
37+
end
38+
end
39+
40+
def update!(id, properties = {})
41+
request = {
42+
properties: Hubspot::Utils.hash_to_properties(properties.stringify_keys, key_name: property_name_field)
43+
}
44+
45+
if update_method == "put"
46+
response = Hubspot::Connection.put_json(update_path, params: { id: id, no_parse: true }, body: request)
47+
else
48+
response = Hubspot::Connection.post_json(update_path, params: { id: id, no_parse: true }, body: request)
49+
end
50+
51+
response.success?
52+
end
2753
end
2854

2955
def initialize(id_or_properties = nil)
@@ -99,27 +125,38 @@ def save
99125
else
100126
response = Hubspot::Connection.post_json(update_path, params: { id: @id }, body: request)
101127
end
128+
129+
update_from_changes
102130
else
103131
response = Hubspot::Connection.post_json(create_path, params: {}, body: request)
104132

105133
# Grab the new ID from the response
106134
@id = response[id_field]
107-
end
108135

109-
# Update the fields with the response
110-
initialize_from(response.with_indifferent_access)
136+
# Update the fields with the response
137+
initialize_from(response.with_indifferent_access)
138+
end
111139

112140
@persisted = true
113141
true
114142
end
115143

144+
def update(properties)
145+
if properties && !properties.is_a?(Hash)
146+
raise ArgumentError, "When assigning properties, you must pass a hash as an argument."
147+
end
148+
149+
@changes = @changes.merge(properties)
150+
save
151+
end
152+
116153
def delete
117154
raise(Hubspot::InvalidParams.new("Resource must have an ID")) if @id.nil?
118155

119156
Hubspot::Connection.delete_json(delete_path, id: @id)
120157

121158
@deleted = true
122-
@changes = {}
159+
@changes = HashWithIndifferentAccess.new
123160
true
124161
end
125162

@@ -184,7 +221,17 @@ def initialize_from(response)
184221
add_accessors(@properties.keys)
185222

186223
# Clear any changes
187-
@changes = {}
224+
@changes = HashWithIndifferentAccess.new
225+
end
226+
227+
def update_from_changes
228+
@changes.each do |k, v|
229+
@properties[k] ||= {}
230+
@properties[k]["value"] = v
231+
end
232+
233+
# Clear any changes
234+
@changes = HashWithIndifferentAccess.new
188235
end
189236

190237
def add_accessors(keys)

spec/fixtures/vcr_cassettes/Hubspot_Company_add_contact_with_a_valid_company_ID_and_contact_ID_adds_the_contact_to_the_company.yml

Lines changed: 56 additions & 56 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/Hubspot_Company_add_contact_with_a_valid_company_ID_and_contact_ID_returns_success.yml

Lines changed: 38 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)