Skip to content

Commit

Permalink
Param body new options
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerleite committed Jul 10, 2012
1 parent 55a7d19 commit 06d6c4c
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lib/restfolia/entry_point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ def get(params = nil)

# Public: Post data to EntryPoint's url.
#
# params - Hash object with data to be encoded as JSON and send as
# request body.
# params - You have some options.
# String param is passed direct as body.
# Object that responds to :body is passed direct as body.
# Hash object with data to be encoded as JSON.
#
# Examples
#
Expand All @@ -91,7 +93,7 @@ def get(params = nil)
# Restfolia::HTTP::Behaviour methods for more details.
# Raises URI::InvalidURIError if url attribute is invalid.
def post(params)
body = MultiJson.encode(params)
body = read_body(params)

args = self.configuration.merge(:body => body)
http_resp = Restfolia::HTTP::Request.do_request(:post, self.url, args)
Expand All @@ -100,8 +102,10 @@ def post(params)

# Public: Put data to EntryPoint's url.
#
# params - Hash object with data to be encoded as JSON and send as
# request body.
# params - You have some options.
# String param is passed direct as body.
# Object that responds to :body is passed direct as body.
# Hash object with data to be encoded as JSON.
#
# Examples
#
Expand All @@ -118,7 +122,7 @@ def post(params)
# Restfolia::HTTP::Behaviour methods for more details.
# Raises URI::InvalidURIError if url attribute is invalid.
def put(params)
body = MultiJson.encode(params)
body = read_body(params)

args = self.configuration.merge(:body => body)
http_resp = Restfolia::HTTP::Request.do_request(:put, self.url, args)
Expand Down Expand Up @@ -150,6 +154,20 @@ def inspect
"#<#{self.class} @url=\"#{@url}\" @rel=\"#{@rel}\">"
end

protected

def read_body(params)
if params && params.is_a?(String)
params
elsif params && params.respond_to?(:body)
params.body
elsif params && params.kind_of?(Hash)
MultiJson.encode(params)
else
raise RuntimeError, "Invalid params #{params.inspect} body."
end
end

end


Expand Down

0 comments on commit 06d6c4c

Please sign in to comment.