From 8a9c6d7383b9931bcd91271cf96a428f8cbd9920 Mon Sep 17 00:00:00 2001 From: Julien Kirch Date: Tue, 7 Dec 2010 19:50:54 +0100 Subject: [PATCH] adding :block_response parameter --- README.rdoc | 12 ++++++++++-- history.md | 1 + lib/restclient/request.rb | 12 +++++++++--- spec/request2_spec.rb | 10 ++++++++++ test.rb | 0 5 files changed, 30 insertions(+), 5 deletions(-) delete mode 100644 test.rb diff --git a/README.rdoc b/README.rdoc index e998e9d..1c7f558 100644 --- a/README.rdoc +++ b/README.rdoc @@ -12,7 +12,7 @@ of specifying actions: get, put, post, delete. require 'rest_client' RestClient.get 'http://example.com/resource' - + RestClient.get 'http://example.com/resource', {:params => {:id => 50, 'foo' => 'bar'}} RestClient.get 'https://user:password@example.com/private/resource', {:accept => :json} @@ -137,7 +137,15 @@ If you want to use non-normalized URIs, you can normalize them with the addressa == Lower-level access -For cases not covered by the general API, you can use the RestClient::Resource class which provide a lower-level API, see the class' rdoc for more information. +For cases not covered by the general API, you can use the RestClient::Request class which provide a lower-level API. + +You can: + +* specify ssl parameters +* override cookies +* manually handle the response (so you can operate on the response stream than reading it fully in memory) + +see the class' rdoc for more information. == Shell diff --git a/history.md b/history.md index 2b315e8..3b52fac 100644 --- a/history.md +++ b/history.md @@ -4,6 +4,7 @@ - fix shell for 1.9.2 - workaround when some gem monkeypatch net/http (patch provided by Ian Warshak) - DELETE requests should process parameters just like GET and HEAD +- adding :block_response parameter for manual processing # 1.6.1 diff --git a/lib/restclient/request.rb b/lib/restclient/request.rb index 2a9ef8b..a3e4172 100644 --- a/lib/restclient/request.rb +++ b/lib/restclient/request.rb @@ -16,6 +16,7 @@ module RestClient # * :headers a hash containing the request headers # * :cookies will replace possible cookies in the :headers # * :user and :password for basic auth, will be replaced by a user/password available in the :url + # * :block_response call the provided block with the HTTPResponse as parameter # * :raw_response return a low-level RawResponse instead of a Response # * :verify_ssl enable ssl verification, possible values are constants from OpenSSL::SSL # * :timeout and :open_timeout @@ -45,6 +46,7 @@ module RestClient @password = args[:password] @timeout = args[:timeout] @open_timeout = args[:open_timeout] + @block_response = args[:block_response] @raw_response = args[:raw_response] || false @verify_ssl = args[:verify_ssl] || false @ssl_client_cert = args[:ssl_client_cert] || nil @@ -164,9 +166,13 @@ module RestClient log_request net.start do |http| - res = http.request(req, payload ? payload.to_s : nil) { |http_response| fetch_body(http_response) } - log_response res - process_result res, & block + if @block_response + http.request(req, payload ? payload.to_s : nil, & @block_response) + else + res = http.request(req, payload ? payload.to_s : nil) { |http_response| fetch_body(http_response) } + log_response res + process_result res, & block + end end rescue EOFError raise RestClient::ServerBrokeConnection diff --git a/spec/request2_spec.rb b/spec/request2_spec.rb index 62f4702..7a897bd 100644 --- a/spec/request2_spec.rb +++ b/spec/request2_spec.rb @@ -13,5 +13,15 @@ describe RestClient::Request do RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => :a}).body.should == 'foo' end + it "can use a block to process response" do + response_value = nil + block = Proc.new do |http_response| + response_value = http_response.body + end + stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200) + RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}, :block_response => block) + response_value.should == "foo" + end + end diff --git a/test.rb b/test.rb deleted file mode 100644 index e69de29..0000000