updated to latest version of rest-client

This commit is contained in:
Chris Anderson 2008-05-22 21:41:21 -07:00
parent 1145fc4042
commit ef639040c3
4 changed files with 31 additions and 8 deletions

View file

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

View file

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

View file

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

View file

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