Skip to content

Commit

Permalink
Add update methods for resources and tests (#194)
Browse files Browse the repository at this point in the history
* Add update methods for resources and tests

* Ignore lock files generated by Appraisal

* Fix class attribute defaults for older versions of ActiveSupport
  • Loading branch information
cbisnett authored May 4, 2019
1 parent dd1573a commit 8eb0a64
Show file tree
Hide file tree
Showing 81 changed files with 3,048 additions and 3,857 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ doc
.bundle

Gemfile.lock
gemfiles/*.lock

# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
#
Expand Down
15 changes: 13 additions & 2 deletions lib/hubspot/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@ def post_json(path, opts)
no_parse = opts[:params].delete(:no_parse) { false }

url = generate_url(path, opts[:params])
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) })
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)
)

log_request_and_response url, response, opts[:body]
raise(Hubspot::RequestError.new(response)) unless response.success?

no_parse ? response : response.parsed_response
end

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

response = put(
Expand All @@ -34,7 +43,9 @@ def put_json(path, options)
)

log_request_and_response(url, response, options[:body])
handle_response(response)
raise(Hubspot::RequestError.new(response)) unless response.success?

no_parse ? response : response.parsed_response
end

def delete_json(path, opts)
Expand Down
63 changes: 55 additions & 8 deletions lib/hubspot/resource.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
module Hubspot
class Resource

class_attribute :id_field, instance_writer: false, default: "id"
class_attribute :property_name_field, instance_writer: false, default: "property"
class_attribute :update_method, instance_writer: false, default: "put"
class_attribute :id_field, instance_writer: false
class_attribute :property_name_field, instance_writer: false
class_attribute :update_method, instance_writer: false

self.id_field = "id"
self.property_name_field = "property"
self.update_method = "put"

class << self
def from_result(result)
Expand All @@ -24,6 +28,28 @@ def create(properties = {})
response = Hubspot::Connection.post_json(create_path, params: {}, body: request)
from_result(response)
end

def update(id, properties = {})
begin
update!(id, properties)
rescue Hubspot::RequestError => e
false
end
end

def update!(id, properties = {})
request = {
properties: Hubspot::Utils.hash_to_properties(properties.stringify_keys, key_name: property_name_field)
}

if update_method == "put"
response = Hubspot::Connection.put_json(update_path, params: { id: id, no_parse: true }, body: request)
else
response = Hubspot::Connection.post_json(update_path, params: { id: id, no_parse: true }, body: request)
end

response.success?
end
end

def initialize(id_or_properties = nil)
Expand Down Expand Up @@ -99,27 +125,38 @@ def save
else
response = Hubspot::Connection.post_json(update_path, params: { id: @id }, body: request)
end

update_from_changes
else
response = Hubspot::Connection.post_json(create_path, params: {}, body: request)

# Grab the new ID from the response
@id = response[id_field]
end

# Update the fields with the response
initialize_from(response.with_indifferent_access)
# Update the fields with the response
initialize_from(response.with_indifferent_access)
end

@persisted = true
true
end

def update(properties)
if properties && !properties.is_a?(Hash)
raise ArgumentError, "When assigning properties, you must pass a hash as an argument."
end

@changes = @changes.merge(properties)
save
end

def delete
raise(Hubspot::InvalidParams.new("Resource must have an ID")) if @id.nil?

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

@deleted = true
@changes = {}
@changes = HashWithIndifferentAccess.new
true
end

Expand Down Expand Up @@ -184,7 +221,17 @@ def initialize_from(response)
add_accessors(@properties.keys)

# Clear any changes
@changes = {}
@changes = HashWithIndifferentAccess.new
end

def update_from_changes
@changes.each do |k, v|
@properties[k] ||= {}
@properties[k]["value"] = v
end

# Clear any changes
@changes = HashWithIndifferentAccess.new
end

def add_accessors(keys)
Expand Down

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8eb0a64

Please sign in to comment.