Skip to content

Commit

Permalink
Add update customer functionality, untested
Browse files Browse the repository at this point in the history
  • Loading branch information
Don Goodman-Wilson committed Jun 10, 2016
1 parent d86a43e commit 25df8b9
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/dev_mate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,44 @@ def self.FindCustomerWithFilters(email: nil, first_name: nil, last_name: nil, li
return response_object['data']
end
end

def self.UpdateCustomer(customer)

return nil unless customer['id']

id = customer['id']

new_customer = {}
new_customer[:email] = customer[email] if customer[email]
new_customer[:first_name] = customer[first_name] if customer[first_name]
new_customer[:last_name] = customer[last_name] if customer[last_name]
new_customer[:notes] = customer[notes] if customer[notes]

data = {:data => new_customer}

response = self.put("/customers/#{id}", :body => data.to_json, :headers => { "Authorization" => @@auth_header })

#TODO handle timeouts!
response_object = JSON.parse response.body

unless response.code == 201
#sad path
errors = response_object["errors"]

case response.code
when 400
raise BadRequestError, "#{errors[0]["title"]} #{errors[0]["detail"]}"
when 401
raise UnauthorizedError, errors[0]["title"]
when 404
raise NotFoundError, "#{errors[0]["title"]} #{errors[0]["detail"]}"
when 409
raise ConflictError, "#{errors[0]["title"]} #{errors[0]["detail"]}"
end
end

# return response.body on success
response_object = JSON.parse response.body
return response_object['data']
end
end

0 comments on commit 25df8b9

Please sign in to comment.