Skip to content

Commit

Permalink
Merge branch 'master' of github.com:zertico/api-client
Browse files Browse the repository at this point in the history
Conflicts:
	lib/api-client/instance_methods.rb
  • Loading branch information
plribeiro3000 committed Jun 28, 2013
2 parents 1c16a7a + 4be7171 commit 51147a6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/api-client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ def self.config

configure do |config|
config.path = ''
config.header = { 'Content-Type' => 'application/json' }
end
end
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
10 changes: 5 additions & 5 deletions lib/api-client/dispatcher/typhoeus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module ApiClient::Dispatcher::Typhoeus
# @param [Hash] header attributes of the request.
# @return [Typhoeus::Request] the response object.
def self.get(url, header = {})
::Typhoeus::Request.get(url, :headers => ApiClient.config.header.merge(header))
::Typhoeus.get(url, :headers => ApiClient.config.header.merge(header))
end

# Make a post request and returns it.
Expand All @@ -18,7 +18,7 @@ def self.get(url, header = {})
# @param [Hash] header attributes of the request.
# @return [Typhoeus::Request] the response object.
def self.post(url, args, header = {})
::Typhoeus::Request.post(url, :body => args, :headers => ApiClient.config.header.merge(header))
::Typhoeus.post(url, :body => args, :headers => ApiClient.config.header.merge(header))
end

# Make a put request and returns it.
Expand All @@ -28,7 +28,7 @@ def self.post(url, args, header = {})
# @param [Hash] header attributes of the request.
# @return [Typhoeus::Request] the response object.
def self.put(url, args, header = {})
::Typhoeus::Request.put(url, :body => args, :headers => ApiClient.config.header.merge(header))
::Typhoeus.put(url, :body => args, :headers => ApiClient.config.header.merge(header))
end

# Make a patch request and returns it.
Expand All @@ -38,7 +38,7 @@ def self.put(url, args, header = {})
# @param [Hash] header attributes of the request.
# @return [Typhoeus::Request] the response object.
def self.patch(url, args, header = {})
::Typhoeus::Request.patch(url, :body => args, :headers => ApiClient.config.header.merge(header))
::Typhoeus.patch(url, :body => args, :headers => ApiClient.config.header.merge(header))
end

# Make a delete request and returns it.
Expand All @@ -47,6 +47,6 @@ def self.patch(url, args, header = {})
# @param [Hash] header attributes of the request.
# @return [Typhoeus::Request] the response object.
def self.delete(url, header = {})
::Typhoeus::Request.delete(url, :headers => ApiClient.config.header.merge(header))
::Typhoeus.delete(url, :headers => ApiClient.config.header.merge(header))
end
end
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}"
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}"
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 51147a6

Please sign in to comment.