From 1b4e350a7a40081cf9f70370419a2fa42af30769 Mon Sep 17 00:00:00 2001 From: Rinaldi Fonseca Date: Sun, 12 Feb 2012 23:55:02 -0200 Subject: [PATCH] Added support to specify a body request given a Hash --- lib/httpi/request.rb | 8 +++++++- spec/httpi/request_spec.rb | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/httpi/request.rb b/lib/httpi/request.rb index f9971a2..5300cb2 100644 --- a/lib/httpi/request.rb +++ b/lib/httpi/request.rb @@ -62,7 +62,13 @@ def gzip headers["Accept-Encoding"] = "gzip,deflate" end - attr_accessor :body, :open_timeout, :read_timeout + attr_accessor :open_timeout, :read_timeout + attr_reader :body + + # Sets a body request given a String or a Hash. + def body=(params) + @body = params.kind_of?(Hash) ? Rack::Utils.build_query(params) : params + end # Returns the HTTPI::Authentication object. def auth diff --git a/spec/httpi/request_spec.rb b/spec/httpi/request_spec.rb index cec1367..2a186fd 100644 --- a/spec/httpi/request_spec.rb +++ b/spec/httpi/request_spec.rb @@ -101,10 +101,15 @@ end describe "#body" do - it "lets you specify the HTTP request body" do + it "lets you specify the HTTP request body using a String" do request.body = "xml" request.body.should == "xml" end + + it "lets you specify the HTTP request body using a Hash" do + request.body = {:foo => :bar, :baz => :foo} + request.body.should == "foo=bar&baz=foo" + end end describe "#open_timeout" do