1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00

adding :block_response parameter

This commit is contained in:
Julien Kirch 2010-12-07 19:50:54 +01:00
parent 937340bf3d
commit 8a9c6d7383
5 changed files with 30 additions and 5 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file