Support for PUT and DELETE.

This commit is contained in:
Zack Hobson 2009-12-22 16:39:56 -08:00
parent 6449b66318
commit 998850e411
5 changed files with 80 additions and 17 deletions

View File

@ -5,25 +5,11 @@ module Faraday
module NetHttp
extend Faraday::Connection::Options
def _post(uri, data, request_headers)
def _perform(method, uri, data, request_headers)
http = Net::HTTP.new(uri.host, uri.port)
response_class.new do |resp|
post_data = post_encode(data)
http_resp = http.post(uri.path, post_data, request_headers) do |chunk|
resp.process(chunk)
end
http_resp.each_header do |key, value|
resp.headers[key] = value
end
end
end
def _get(uri, request_headers)
http = Net::HTTP.new(uri.host, uri.port)
response_class.new do |resp|
http_resp = http.get(uri.path, request_headers) do |chunk|
resp.process(chunk)
end
http_resp = http.send_request(method, uri.path, data, request_headers)
resp.process http_resp.body
http_resp.each_header do |key, value|
resp.headers[key] = value
end
@ -32,6 +18,22 @@ module Faraday
raise Faraday::Error::ConnectionFailed, "connection refused"
end
def _put(uri, data, request_headers)
_perform('PUT', uri, post_encode(data), request_headers)
end
def _post(uri, data, request_headers)
_perform('POST', uri, post_encode(data), request_headers)
end
def _get(uri, request_headers)
_perform('GET', uri, nil, request_headers)
end
def _delete(uri, request_headers)
_perform('DELETE', uri, nil, request_headers)
end
def post_encode data
create_post_params data
end

View File

@ -33,6 +33,14 @@ module Faraday
_perform(:get, uri, :headers => request_headers)
end
def _put(uri, data, request_headers)
_perform(:put, uri, :headers => request_headers, :params => data)
end
def _delete(uri, request_headers)
_perform(:delete, uri, :headers => request_headers)
end
def _perform method, uri, params
response_class.new do |resp|
is_async = in_parallel?

View File

@ -46,6 +46,19 @@ module Faraday
_post build_uri(uri), params, headers
end
# Override in a subclass, or include an adapter
#
# def _put(uri, post_params, headers)
# end
#
def put(uri, params = {}, headers = {})
_put build_uri(uri), params, headers
end
def delete(uri, params = {}, headers = {})
_delete build_uri(uri, params), headers
end
def response_class
@response_class || Response
end

View File

@ -6,6 +6,34 @@ class AdapterTest < Faraday::TestCase
end
Faraday::Adapter.loaded_adapters.each do |adapter|
describe "#delete with #{adapter} adapter" do
before do
@connection.extend adapter
end
it "retrieves the response body with YajlResponse" do
@connection.response_class = Faraday::Response::YajlResponse
assert_equal({'deleted' => true},
@connection.delete('delete_me').body)
end
end
describe "#put with #{adapter} adapter" do
before do
@connection.extend adapter
end
it "sends params" do
assert_equal 'hello zack', @connection.put('hello', 'name' => 'zack').body
end
it "retrieves the response body with YajlResponse" do
@connection.response_class = Faraday::Response::YajlResponse
assert_equal({'name' => 'zack'},
@connection.put('echo_name', 'name' => 'zack').body)
end
end
describe "#post with #{adapter} adapter" do
before do
@connection.extend adapter

View File

@ -13,6 +13,18 @@ post '/hello' do
"hello #{params[:name]}"
end
put '/hello' do
"hello #{params[:name]}"
end
post '/echo_name' do
%/{"name":#{params[:name].inspect}}/
end
put '/echo_name' do
%/{"name":#{params[:name].inspect}}/
end
delete '/delete_me' do
%/{"deleted":true}/
end