Skip to content

Commit

Permalink
Fix InstanceMethods methods
Browse files Browse the repository at this point in the history
  • Loading branch information
plribeiro3000 committed Jun 18, 2013
1 parent 77c467c commit 81faf27
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/api-client/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ def delete(id, header = {})
build(params)
end

# Removes the root node attribute if found.
#
# @param [Hash] attributes the hash with attributes.
# @return [Hash] the hash with attributes without the root node.
def remove_root(attributes = {})
attributes = attributes[self.root_node.to_sym] if attributes.key?(self.root_node.to_sym)
attributes = attributes[self.root_node.to_s] if attributes.key?(self.root_node.to_s)
Expand Down
30 changes: 27 additions & 3 deletions lib/api-client/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module ApiClient
module InstanceMethods
# Update an object based on a hash of attributes.
#
# @param [Hash] params hash of attributes.
# @param [Hash] attributes hash of attributes.
# @return [Base] the update_attributes object.
def update_attributes(attributes)
hash = remove_root(attributes)
Expand All @@ -14,41 +14,65 @@ def update_attributes(attributes)
self
end

# Make a get requisition and update the object with the response.
#
# @param [Hash] header hash with the header options.
# @return [Base] the object updated.
def get(header = {})
url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
response = ApiClient::Dispatcher.get(url, header)
attributes = ApiClient::Parser.response(response, url)
update_attributes(attributes)
end

# Make a post requisition and update the object with the response.
#
# @param [Hash] header hash with the header options.
# @return [Base] the object updated.
def post(header = {})
url = "#{ApiClient.config.path}#{self.class.resource_path}"
response = ApiClient::Dispatcher.post(url, self.to_hash, header)
attributes = ApiClient::Parser.response(response, url)
update_attributes(attributes)
end

# Make a put requisition and update the object with the response.
#
# @param [Hash] header hash with the header options.
# @return [Base] the object updated.
def put(header = {})
url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
response = ApiClient::Dispatcher.put(url, self.to_hash, header)
attributes = ApiClient::Parser.response(response, url)
update_attributes(attributes)
end

# Make a patch requisition and update the object with the response.
#
# @param [Hash] header hash with the header options.
# @return [Base] the object updated.
def patch(header = {})
url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
response = ApiClient::Dispatcher.post(url, self.to_hash, header)
response = ApiClient::Dispatcher.patch(url, self.to_hash, header)
attributes = ApiClient::Parser.response(response, url)
update_attributes(attributes)
end

# Make a delete requisition and update the object with the response.
#
# @param [Hash] header hash with the header options.
# @return [Base] the object updated.
def delete(header = {})
url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
response = ApiClient::Dispatcher.post(url, header)
response = ApiClient::Dispatcher.delete(url, header)
attributes = ApiClient::Parser.response(response, url)
update_attributes(attributes)
end

# Removes the root node attribute if found.
#
# @param [Hash] attributes the hash with attributes.
# @return [Hash] the hash with attributes without the root node.
def remove_root(attributes = {})
attributes = attributes[self.class.root_node.to_sym] if attributes.key?(self.class.root_node.to_sym)
attributes = attributes[self.class.root_node.to_s] if attributes.key?(self.class.root_node.to_s)
Expand Down

0 comments on commit 81faf27

Please sign in to comment.