From ef639040c33011d45ee68139a7d9665a80f9cb82 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Thu, 22 May 2008 21:41:21 -0700 Subject: [PATCH] updated to latest version of rest-client --- deps/rest-client/README | 2 ++ deps/rest-client/Rakefile | 2 +- deps/rest-client/lib/rest_client.rb | 20 ++++++++++++++------ deps/rest-client/spec/rest_client_spec.rb | 15 ++++++++++++++- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/deps/rest-client/README b/deps/rest-client/README index 3942c12..7c8e1e6 100644 --- a/deps/rest-client/README +++ b/deps/rest-client/README @@ -39,6 +39,8 @@ using curl at the command line. Better yet, require gem from within your Written by Adam Wiggins (adam at heroku dot com) +Patches contributed by: Chris Anderson, Greg Borenstein + Released under the MIT License: http://www.opensource.org/licenses/mit-license.php http://rest-client.heroku.com diff --git a/deps/rest-client/Rakefile b/deps/rest-client/Rakefile index e79818d..868cbc5 100644 --- a/deps/rest-client/Rakefile +++ b/deps/rest-client/Rakefile @@ -30,7 +30,7 @@ require 'rake/gempackagetask' require 'rake/rdoctask' require 'fileutils' -version = "0.2" +version = "0.3" name = "rest-client" spec = Gem::Specification.new do |s| diff --git a/deps/rest-client/lib/rest_client.rb b/deps/rest-client/lib/rest_client.rb index c852963..522ac26 100644 --- a/deps/rest-client/lib/rest_client.rb +++ b/deps/rest-client/lib/rest_client.rb @@ -42,8 +42,8 @@ module RestClient def initialize(args) @method = args[:method] or raise ArgumentError, "must pass :method" @url = args[:url] or raise ArgumentError, "must pass :url" - @payload = args[:payload] @headers = args[:headers] || {} + @payload = process_payload(args[:payload]) @user = args[:user] @password = args[:password] end @@ -57,8 +57,7 @@ module RestClient def execute_inner uri = parse_url(url) - uri_path = uri.respond_to?(:request_uri) ? uri.request_uri : uri.path - transmit uri, net_http_class(method).new(uri_path, make_headers(headers)), payload + transmit uri, net_http_class(method).new(uri.request_uri, make_headers(headers)), payload end def make_headers(user_headers) @@ -80,13 +79,22 @@ module RestClient end # A redirect was encountered; caught by execute to retry with the new url. - class Redirect < Exception; end + class Redirect < RuntimeError; end # Request failed with an unhandled http error code. - class RequestFailed < Exception; end + class RequestFailed < RuntimeError; end # Authorization is required to access the resource specified. - class Unauthorized < Exception; end + class Unauthorized < RuntimeError; end + + def process_payload(p=nil) + unless p.is_a?(Hash) + p + else + @headers[:content_type] = 'application/x-www-form-urlencoded' + p.keys.map { |k| "#{k}=#{URI.escape(p[k].to_s)}" }.join("&") + end + end def transmit(uri, req, payload) setup_credentials(req) diff --git a/deps/rest-client/spec/rest_client_spec.rb b/deps/rest-client/spec/rest_client_spec.rb index 7980beb..cfaf932 100644 --- a/deps/rest-client/spec/rest_client_spec.rb +++ b/deps/rest-client/spec/rest_client_spec.rb @@ -28,7 +28,7 @@ describe RestClient do @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload') @uri = mock("uri") - @uri.stub!(:path).and_return('/resource') + @uri.stub!(:request_uri).and_return('/resource') @uri.stub!(:host).and_return('some') @uri.stub!(:port).and_return(80) end @@ -98,6 +98,19 @@ describe RestClient do @request.transmit(@uri, 'req', nil) end + it "passes non-hash payloads straight through" do + @request.process_payload("x").should == "x" + end + + it "converts a hash payload to urlencoded data" do + @request.process_payload(:a => 'b c').should == "a=b%20c" + end + + it "set urlencoded content_type header on hash payloads" do + @request.process_payload(:a => 1) + @request.headers[:content_type].should == 'application/x-www-form-urlencoded' + end + it "sets up the credentials prior to the request" do http = mock("net::http connection") Net::HTTP.should_receive(:start).and_yield(http)