Skip to content

Commit

Permalink
Merge branch 'fix-issue-96'
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Breton committed May 26, 2017
2 parents 2bfc11c + f0aa4c7 commit f69bed3
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions lib/mailjet/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,22 +245,24 @@ def persisted?

def save(options)
if persisted?
# case where the entity is updated
response = connection(options)[attributes[:id]].put(formatted_payload, default_headers, options[:perform_api_call])
else
# case where the entity is created
response = connection(options).post(formatted_payload, default_headers, options[:perform_api_call])
end

if options[:perform_api_call]
if self.resource_path.include? 'send'
self.attributes = ActiveSupport::JSON.decode(response)
return true
if options[:perform_api_call] && !persisted?
# get attributes only for entity creation
self.attributes = if self.resource_path == 'send'
ActiveSupport::JSON.decode(response)
else
parse_api_json(response).first
end

self.attributes = parse_api_json(response).first
return true
else
return true
end

return true

rescue Mailjet::ApiError => e
if e.code.to_s == "304"
return true # When you save a record twice it should not raise error
Expand All @@ -280,8 +282,19 @@ def attributes=(attribute_hash = {})
end

def update_attributes(attribute_hash = {}, options = {})
self.attributes = attribute_hash
save(options)
self.attributes.deep_merge!(attribute_hash) do |key, old, new|
if old.is_a?(Array) && new.is_a?(Array)
# uniqueness of hashes based on their value of "Name"
(new + old).uniq do |data|
data["Name"]
end
else
new
end
end

opts = self.class.change_resource_path(options)
save(opts)
end

def delete(call)
Expand Down

0 comments on commit f69bed3

Please sign in to comment.