adamwiggins-rest-client 0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +121 -0
- data/Rakefile +85 -0
- data/bin/restclient +87 -0
- data/lib/restclient/exceptions.rb +84 -0
- data/lib/restclient/request.rb +178 -0
- data/lib/restclient/resource.rb +138 -0
- data/lib/restclient/response.rb +35 -0
- data/rest-client.gemspec +20 -0
- data/spec/base.rb +4 -0
- data/spec/exceptions_spec.rb +54 -0
- data/spec/request_spec.rb +296 -0
- data/spec/resource_spec.rb +75 -0
- data/spec/response_spec.rb +36 -0
- data/spec/restclient_spec.rb +48 -0
- metadata +66 -0
data/README.rdoc
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
= REST Client -- simple DSL for accessing REST resources
|
2
|
+
|
3
|
+
A simple REST client for Ruby, inspired by the Sinatra's microframework style
|
4
|
+
of specifying actions: get, put, post, delete.
|
5
|
+
|
6
|
+
== Usage: Raw URL
|
7
|
+
|
8
|
+
require 'rest_client'
|
9
|
+
|
10
|
+
RestClient.get 'http://example.com/resource'
|
11
|
+
RestClient.get 'https://user:password@example.com/private/resource'
|
12
|
+
|
13
|
+
RestClient.post 'http://example.com/resource', :param1 => 'one', :nested => { :param2 => 'two' }
|
14
|
+
|
15
|
+
RestClient.delete 'http://example.com/resource'
|
16
|
+
|
17
|
+
See RestClient module docs for details.
|
18
|
+
|
19
|
+
== Usage: ActiveResource-Style
|
20
|
+
|
21
|
+
resource = RestClient::Resource.new 'http://example.com/resource'
|
22
|
+
resource.get
|
23
|
+
|
24
|
+
private_resource = RestClient::Resource.new 'https://example.com/private/resource', :user => 'adam', :password => 'secret', :timeout => 20
|
25
|
+
private_resource.put File.read('pic.jpg'), :content_type => 'image/jpg'
|
26
|
+
|
27
|
+
See RestClient::Resource module docs for details.
|
28
|
+
|
29
|
+
== Usage: Resource Nesting
|
30
|
+
|
31
|
+
site = RestClient::Resource.new('http://example.com')
|
32
|
+
site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
|
33
|
+
|
34
|
+
See RestClient::Resource docs for details.
|
35
|
+
|
36
|
+
== Shell
|
37
|
+
|
38
|
+
The restclient shell command gives an IRB session with RestClient already loaded:
|
39
|
+
|
40
|
+
$ restclient
|
41
|
+
>> RestClient.get 'http://example.com'
|
42
|
+
|
43
|
+
Specify a URL argument for get/post/put/delete on that resource:
|
44
|
+
|
45
|
+
$ restclient http://example.com
|
46
|
+
>> put '/resource', 'data'
|
47
|
+
|
48
|
+
Add a user and password for authenticated resources:
|
49
|
+
|
50
|
+
$ restclient https://example.com user pass
|
51
|
+
>> delete '/private/resource'
|
52
|
+
|
53
|
+
Create ~/.restclient for named sessions:
|
54
|
+
|
55
|
+
sinatra:
|
56
|
+
url: http://localhost:4567
|
57
|
+
rack:
|
58
|
+
url: http://localhost:9292
|
59
|
+
private_site:
|
60
|
+
url: http://example.com
|
61
|
+
username: user
|
62
|
+
password: pass
|
63
|
+
|
64
|
+
Then invoke:
|
65
|
+
|
66
|
+
$ restclient private_site
|
67
|
+
|
68
|
+
Use as a one-off, curl-style:
|
69
|
+
|
70
|
+
$ restclient get http://example.com/resource > output_body
|
71
|
+
|
72
|
+
$ restclient put http://example.com/resource < input_body
|
73
|
+
|
74
|
+
== Logging
|
75
|
+
|
76
|
+
Write calls to a log filename (can also be "stdout" or "stderr"):
|
77
|
+
|
78
|
+
RestClient.log = '/tmp/restclient.log'
|
79
|
+
|
80
|
+
Or set an environment variable to avoid modifying the code:
|
81
|
+
|
82
|
+
$ RESTCLIENT_LOG=stdout path/to/my/program
|
83
|
+
|
84
|
+
Either produces logs like this:
|
85
|
+
|
86
|
+
RestClient.get "http://some/resource"
|
87
|
+
# => 200 OK | text/html 250 bytes
|
88
|
+
RestClient.put "http://some/resource", "payload"
|
89
|
+
# => 401 Unauthorized | application/xml 340 bytes
|
90
|
+
|
91
|
+
Note that these logs are valid Ruby, so you can paste them into the restclient
|
92
|
+
shell or a script to replay your sequence of rest calls.
|
93
|
+
|
94
|
+
== Proxy
|
95
|
+
|
96
|
+
All calls to RestClient, including Resources, will use the proxy specified by
|
97
|
+
RestClient.proxy:
|
98
|
+
|
99
|
+
RestClient.proxy = "http://proxy.example.com/"
|
100
|
+
RestClient.get "http://some/resource"
|
101
|
+
# => response from some/resource as proxied through proxy.example.com
|
102
|
+
|
103
|
+
Often the proxy url is set in an environment variable, so you can do this to
|
104
|
+
use whatever proxy the system is configured to use:
|
105
|
+
|
106
|
+
RestClient.proxy = ENV['http_proxy']
|
107
|
+
|
108
|
+
== Meta
|
109
|
+
|
110
|
+
Written by Adam Wiggins (adam at heroku dot com)
|
111
|
+
|
112
|
+
Patches contributed by: Chris Anderson, Greg Borenstein, Ardekantur, Pedro
|
113
|
+
Belo, Rafael Souza, Rick Olson, Aman Gupta, Blake Mizerany, Brian Donovan, Ivan
|
114
|
+
Makfinsky, Marc-André Cournoyer, Coda Hale, and Tetsuo Watanabe
|
115
|
+
|
116
|
+
Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
|
117
|
+
|
118
|
+
http://rest-client.heroku.com
|
119
|
+
|
120
|
+
http://github.com/adamwiggins/rest-client
|
121
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'spec/rake/spectask'
|
3
|
+
|
4
|
+
desc "Run all specs"
|
5
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
6
|
+
t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
|
7
|
+
t.spec_files = FileList['spec/*_spec.rb']
|
8
|
+
end
|
9
|
+
|
10
|
+
desc "Print specdocs"
|
11
|
+
Spec::Rake::SpecTask.new(:doc) do |t|
|
12
|
+
t.spec_opts = ["--format", "specdoc", "--dry-run"]
|
13
|
+
t.spec_files = FileList['spec/*_spec.rb']
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Run all examples with RCov"
|
17
|
+
Spec::Rake::SpecTask.new('rcov') do |t|
|
18
|
+
t.spec_files = FileList['spec/*_spec.rb']
|
19
|
+
t.rcov = true
|
20
|
+
t.rcov_opts = ['--exclude', 'examples']
|
21
|
+
end
|
22
|
+
|
23
|
+
task :default => :spec
|
24
|
+
|
25
|
+
######################################################
|
26
|
+
|
27
|
+
require 'rake'
|
28
|
+
require 'rake/testtask'
|
29
|
+
require 'rake/clean'
|
30
|
+
require 'rake/gempackagetask'
|
31
|
+
require 'rake/rdoctask'
|
32
|
+
require 'fileutils'
|
33
|
+
|
34
|
+
version = "0.9"
|
35
|
+
name = "rest-client"
|
36
|
+
|
37
|
+
spec = Gem::Specification.new do |s|
|
38
|
+
s.name = name
|
39
|
+
s.version = version
|
40
|
+
s.summary = "Simple REST client for Ruby, inspired by microframework syntax for specifying actions."
|
41
|
+
s.description = "A simple REST client for Ruby, inspired by the Sinatra microframework style of specifying actions: get, put, post, delete."
|
42
|
+
s.author = "Adam Wiggins"
|
43
|
+
s.email = "adam@heroku.com"
|
44
|
+
s.homepage = "http://rest-client.heroku.com/"
|
45
|
+
s.rubyforge_project = "rest-client"
|
46
|
+
|
47
|
+
s.platform = Gem::Platform::RUBY
|
48
|
+
s.has_rdoc = true
|
49
|
+
|
50
|
+
s.files = %w(Rakefile) + Dir.glob("{lib,spec}/**/*")
|
51
|
+
s.executables = ['restclient']
|
52
|
+
|
53
|
+
s.require_path = "lib"
|
54
|
+
end
|
55
|
+
|
56
|
+
Rake::GemPackageTask.new(spec) do |p|
|
57
|
+
p.need_tar = true if RUBY_PLATFORM !~ /mswin/
|
58
|
+
end
|
59
|
+
|
60
|
+
task :install => [ :package ] do
|
61
|
+
sh %{sudo gem install pkg/#{name}-#{version}.gem}
|
62
|
+
end
|
63
|
+
|
64
|
+
task :uninstall => [ :clean ] do
|
65
|
+
sh %{sudo gem uninstall #{name}}
|
66
|
+
end
|
67
|
+
|
68
|
+
Rake::TestTask.new do |t|
|
69
|
+
t.libs << "spec"
|
70
|
+
t.test_files = FileList['spec/*_spec.rb']
|
71
|
+
t.verbose = true
|
72
|
+
end
|
73
|
+
|
74
|
+
Rake::RDocTask.new do |t|
|
75
|
+
t.rdoc_dir = 'rdoc'
|
76
|
+
t.title = "rest-client, fetch RESTful resources effortlessly"
|
77
|
+
t.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
|
78
|
+
t.options << '--charset' << 'utf-8'
|
79
|
+
t.rdoc_files.include('README.rdoc')
|
80
|
+
t.rdoc_files.include('lib/restclient.rb')
|
81
|
+
t.rdoc_files.include('lib/restclient/*.rb')
|
82
|
+
end
|
83
|
+
|
84
|
+
CLEAN.include [ 'pkg', '*.gem', '.config' ]
|
85
|
+
|
data/bin/restclient
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$:.unshift File.dirname(__FILE__) + "/../lib"
|
4
|
+
require 'restclient'
|
5
|
+
|
6
|
+
require "yaml"
|
7
|
+
|
8
|
+
def usage(why = nil)
|
9
|
+
puts "failed for reason: #{why}" if why
|
10
|
+
puts "usage: restclient [get|put|post|delete] url|name [username] [password]"
|
11
|
+
puts " The verb is optional, if you leave it off you'll get an interactive shell."
|
12
|
+
puts " put and post both take the input body on stdin."
|
13
|
+
exit(1)
|
14
|
+
end
|
15
|
+
|
16
|
+
if %w(get put post delete).include? ARGV.first
|
17
|
+
@verb = ARGV.shift
|
18
|
+
else
|
19
|
+
@verb = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
@url = ARGV.shift || 'http://localhost:4567'
|
23
|
+
|
24
|
+
config = YAML.load(File.read(ENV['HOME'] + "/.restclient")) rescue {}
|
25
|
+
|
26
|
+
@url, @username, @password = if c = config[@url]
|
27
|
+
[c['url'], c['username'], c['password']]
|
28
|
+
else
|
29
|
+
[@url, *ARGV]
|
30
|
+
end
|
31
|
+
|
32
|
+
usage("invalid url '#{@url}") unless @url =~ /^https?/
|
33
|
+
usage("too few args") unless ARGV.size < 3
|
34
|
+
|
35
|
+
def r
|
36
|
+
@r ||= RestClient::Resource.new(@url, @username, @password)
|
37
|
+
end
|
38
|
+
|
39
|
+
r # force rc to load
|
40
|
+
|
41
|
+
if @verb
|
42
|
+
begin
|
43
|
+
if %w(put post).include? @verb
|
44
|
+
puts r.send(@verb, STDIN.read)
|
45
|
+
else
|
46
|
+
puts r.send(@verb)
|
47
|
+
end
|
48
|
+
exit 0
|
49
|
+
rescue RestClient::Exception => e
|
50
|
+
puts e.response.body if e.respond_to? :response
|
51
|
+
raise
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
%w(get post put delete).each do |m|
|
56
|
+
eval <<-end_eval
|
57
|
+
def #{m}(path, *args, &b)
|
58
|
+
r[path].#{m}(*args, &b)
|
59
|
+
end
|
60
|
+
end_eval
|
61
|
+
end
|
62
|
+
|
63
|
+
def method_missing(s, *args, &b)
|
64
|
+
super unless r.respond_to?(s)
|
65
|
+
begin
|
66
|
+
r.send(s, *args, &b)
|
67
|
+
rescue RestClient::RequestFailed => e
|
68
|
+
print STDERR, e.response.body
|
69
|
+
raise e
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
require 'irb'
|
74
|
+
require 'irb/completion'
|
75
|
+
|
76
|
+
if File.exists? ".irbrc"
|
77
|
+
ENV['IRBRC'] = ".irbrc"
|
78
|
+
end
|
79
|
+
|
80
|
+
if File.exists?(rcfile = "~/.restclientrc")
|
81
|
+
load(rcfile)
|
82
|
+
end
|
83
|
+
|
84
|
+
ARGV.clear
|
85
|
+
|
86
|
+
IRB.start
|
87
|
+
exit!
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module RestClient
|
2
|
+
# This is the base RestClient exception class. Rescue it if you want to
|
3
|
+
# catch any exception that your request might raise
|
4
|
+
class Exception < RuntimeError
|
5
|
+
def message(default=nil)
|
6
|
+
self.class::ErrorMessage
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
# Base RestClient exception when there's a response available
|
11
|
+
class ExceptionWithResponse < Exception
|
12
|
+
attr_accessor :response
|
13
|
+
|
14
|
+
def initialize(response=nil)
|
15
|
+
@response = response
|
16
|
+
end
|
17
|
+
|
18
|
+
def http_code
|
19
|
+
@response.code.to_i if @response
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# A redirect was encountered; caught by execute to retry with the new url.
|
24
|
+
class Redirect < Exception
|
25
|
+
ErrorMessage = "Redirect"
|
26
|
+
|
27
|
+
attr_accessor :url
|
28
|
+
def initialize(url)
|
29
|
+
@url = url
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class NotModified < Exception
|
34
|
+
ErrorMessage = 'NotModified'
|
35
|
+
end
|
36
|
+
|
37
|
+
# Authorization is required to access the resource specified.
|
38
|
+
class Unauthorized < ExceptionWithResponse
|
39
|
+
ErrorMessage = 'Unauthorized'
|
40
|
+
end
|
41
|
+
|
42
|
+
# No resource was found at the given URL.
|
43
|
+
class ResourceNotFound < ExceptionWithResponse
|
44
|
+
ErrorMessage = 'Resource not found'
|
45
|
+
end
|
46
|
+
|
47
|
+
# The server broke the connection prior to the request completing. Usually
|
48
|
+
# this means it crashed, or sometimes that your network connection was
|
49
|
+
# severed before it could complete.
|
50
|
+
class ServerBrokeConnection < Exception
|
51
|
+
ErrorMessage = 'Server broke connection'
|
52
|
+
end
|
53
|
+
|
54
|
+
# The server took too long to respond.
|
55
|
+
class RequestTimeout < Exception
|
56
|
+
ErrorMessage = 'Request timed out'
|
57
|
+
end
|
58
|
+
|
59
|
+
# The request failed, meaning the remote HTTP server returned a code other
|
60
|
+
# than success, unauthorized, or redirect.
|
61
|
+
#
|
62
|
+
# The exception message attempts to extract the error from the XML, using
|
63
|
+
# format returned by Rails: <errors><error>some message</error></errors>
|
64
|
+
#
|
65
|
+
# You can get the status code by e.http_code, or see anything about the
|
66
|
+
# response via e.response. For example, the entire result body (which is
|
67
|
+
# probably an HTML error page) is e.response.body.
|
68
|
+
class RequestFailed < ExceptionWithResponse
|
69
|
+
def message
|
70
|
+
"HTTP status code #{http_code}"
|
71
|
+
end
|
72
|
+
|
73
|
+
def to_s
|
74
|
+
message
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# backwards compatibility
|
80
|
+
class RestClient::Request
|
81
|
+
Redirect = RestClient::Redirect
|
82
|
+
Unauthorized = RestClient::Unauthorized
|
83
|
+
RequestFailed = RestClient::RequestFailed
|
84
|
+
end
|
@@ -0,0 +1,178 @@
|
|
1
|
+
module RestClient
|
2
|
+
# This class is used internally by RestClient to send the request, but you can also
|
3
|
+
# access it internally if you'd like to use a method not directly supported by the
|
4
|
+
# main API. For example:
|
5
|
+
#
|
6
|
+
# RestClient::Request.execute(:method => :head, :url => 'http://example.com')
|
7
|
+
#
|
8
|
+
class Request
|
9
|
+
attr_reader :method, :url, :payload, :headers, :user, :password, :timeout
|
10
|
+
|
11
|
+
def self.execute(args)
|
12
|
+
new(args).execute
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(args)
|
16
|
+
@method = args[:method] or raise ArgumentError, "must pass :method"
|
17
|
+
@url = args[:url] or raise ArgumentError, "must pass :url"
|
18
|
+
@headers = args[:headers] || {}
|
19
|
+
@payload = process_payload(args[:payload])
|
20
|
+
@user = args[:user]
|
21
|
+
@password = args[:password]
|
22
|
+
@timeout = args[:timeout]
|
23
|
+
end
|
24
|
+
|
25
|
+
def execute
|
26
|
+
execute_inner
|
27
|
+
rescue Redirect => e
|
28
|
+
@url = e.url
|
29
|
+
execute
|
30
|
+
end
|
31
|
+
|
32
|
+
def execute_inner
|
33
|
+
uri = parse_url_with_auth(url)
|
34
|
+
transmit uri, net_http_request_class(method).new(uri.request_uri, make_headers(headers)), payload
|
35
|
+
end
|
36
|
+
|
37
|
+
def make_headers(user_headers)
|
38
|
+
default_headers.merge(user_headers).inject({}) do |final, (key, value)|
|
39
|
+
final[key.to_s.gsub(/_/, '-').capitalize] = value.to_s
|
40
|
+
final
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def net_http_class
|
45
|
+
if RestClient.proxy
|
46
|
+
proxy_uri = URI.parse(RestClient.proxy)
|
47
|
+
Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
|
48
|
+
else
|
49
|
+
Net::HTTP
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def net_http_request_class(method)
|
54
|
+
Net::HTTP.const_get(method.to_s.capitalize)
|
55
|
+
end
|
56
|
+
|
57
|
+
def parse_url(url)
|
58
|
+
url = "http://#{url}" unless url.match(/^http/)
|
59
|
+
URI.parse(url)
|
60
|
+
end
|
61
|
+
|
62
|
+
def parse_url_with_auth(url)
|
63
|
+
uri = parse_url(url)
|
64
|
+
@user = uri.user if uri.user
|
65
|
+
@password = uri.password if uri.password
|
66
|
+
uri
|
67
|
+
end
|
68
|
+
|
69
|
+
def process_payload(p=nil, parent_key=nil)
|
70
|
+
unless p.is_a?(Hash)
|
71
|
+
p
|
72
|
+
else
|
73
|
+
@headers[:content_type] ||= 'application/x-www-form-urlencoded'
|
74
|
+
p.keys.map do |k|
|
75
|
+
key = parent_key ? "#{parent_key}[#{k}]" : k
|
76
|
+
if p[k].is_a? Hash
|
77
|
+
process_payload(p[k], key)
|
78
|
+
else
|
79
|
+
value = URI.escape(p[k].to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
80
|
+
"#{key}=#{value}"
|
81
|
+
end
|
82
|
+
end.join("&")
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def transmit(uri, req, payload)
|
87
|
+
setup_credentials(req)
|
88
|
+
|
89
|
+
net = net_http_class.new(uri.host, uri.port)
|
90
|
+
net.use_ssl = uri.is_a?(URI::HTTPS)
|
91
|
+
net.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
92
|
+
|
93
|
+
display_log request_log
|
94
|
+
|
95
|
+
net.start do |http|
|
96
|
+
http.read_timeout = @timeout if @timeout
|
97
|
+
res = http.request(req, payload)
|
98
|
+
display_log response_log(res)
|
99
|
+
string = process_result(res)
|
100
|
+
if string
|
101
|
+
Response.new(string, res)
|
102
|
+
else
|
103
|
+
nil
|
104
|
+
end
|
105
|
+
end
|
106
|
+
rescue EOFError
|
107
|
+
raise RestClient::ServerBrokeConnection
|
108
|
+
rescue Timeout::Error
|
109
|
+
raise RestClient::RequestTimeout
|
110
|
+
end
|
111
|
+
|
112
|
+
def setup_credentials(req)
|
113
|
+
req.basic_auth(user, password) if user
|
114
|
+
end
|
115
|
+
|
116
|
+
def process_result(res)
|
117
|
+
if %w(200 201 202).include? res.code
|
118
|
+
decode res['content-encoding'], res.body
|
119
|
+
elsif %w(301 302 303).include? res.code
|
120
|
+
url = res.header['Location']
|
121
|
+
|
122
|
+
if url !~ /^http/
|
123
|
+
uri = URI.parse(@url)
|
124
|
+
uri.path = "/#{url}".squeeze('/')
|
125
|
+
url = uri.to_s
|
126
|
+
end
|
127
|
+
|
128
|
+
raise Redirect, url
|
129
|
+
elsif res.code == "304"
|
130
|
+
raise NotModified
|
131
|
+
elsif res.code == "401"
|
132
|
+
raise Unauthorized, res
|
133
|
+
elsif res.code == "404"
|
134
|
+
raise ResourceNotFound, res
|
135
|
+
else
|
136
|
+
raise RequestFailed, res
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def decode(content_encoding, body)
|
141
|
+
if content_encoding == 'gzip'
|
142
|
+
Zlib::GzipReader.new(StringIO.new(body)).read
|
143
|
+
elsif content_encoding == 'deflate'
|
144
|
+
Zlib::Inflate.new.inflate(body)
|
145
|
+
else
|
146
|
+
body
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def request_log
|
151
|
+
out = []
|
152
|
+
out << "RestClient.#{method} #{url.inspect}"
|
153
|
+
out << (payload.size > 100 ? "(#{payload.size} byte payload)".inspect : payload.inspect) if payload
|
154
|
+
out << headers.inspect.gsub(/^\{/, '').gsub(/\}$/, '') unless headers.empty?
|
155
|
+
out.join(', ')
|
156
|
+
end
|
157
|
+
|
158
|
+
def response_log(res)
|
159
|
+
"# => #{res.code} #{res.class.to_s.gsub(/^Net::HTTP/, '')} | #{(res['Content-type'] || '').gsub(/;.*$/, '')} #{(res.body) ? res.body.size : nil} bytes"
|
160
|
+
end
|
161
|
+
|
162
|
+
def display_log(msg)
|
163
|
+
return unless log_to = RestClient.log
|
164
|
+
|
165
|
+
if log_to == 'stdout'
|
166
|
+
STDOUT.puts msg
|
167
|
+
elsif log_to == 'stderr'
|
168
|
+
STDERR.puts msg
|
169
|
+
else
|
170
|
+
File.open(log_to, 'a') { |f| f.puts msg }
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def default_headers
|
175
|
+
{ :accept => 'application/xml', :accept_encoding => 'gzip, deflate' }
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
module RestClient
|
2
|
+
# A class that can be instantiated for access to a RESTful resource,
|
3
|
+
# including authentication.
|
4
|
+
#
|
5
|
+
# Example:
|
6
|
+
#
|
7
|
+
# resource = RestClient::Resource.new('http://some/resource')
|
8
|
+
# jpg = resource.get(:accept => 'image/jpg')
|
9
|
+
#
|
10
|
+
# With HTTP basic authentication:
|
11
|
+
#
|
12
|
+
# resource = RestClient::Resource.new('http://protected/resource', :user => 'user', :password => 'password')
|
13
|
+
# resource.delete
|
14
|
+
#
|
15
|
+
# With a timeout (seconds):
|
16
|
+
#
|
17
|
+
# RestClient::Resource.new('http://slow', :timeout => 10)
|
18
|
+
#
|
19
|
+
# You can also use resources to share common headers. For headers keys,
|
20
|
+
# symbols are converted to strings. Example:
|
21
|
+
#
|
22
|
+
# resource = RestClient::Resource.new('http://some/resource', :headers => { :client_version => 1 })
|
23
|
+
#
|
24
|
+
# This header will be transported as X-Client-Version (notice the X prefix,
|
25
|
+
# capitalization and hyphens)
|
26
|
+
#
|
27
|
+
# Use the [] syntax to allocate subresources:
|
28
|
+
#
|
29
|
+
# site = RestClient::Resource.new('http://example.com', :user => 'adam', :password => 'mypasswd')
|
30
|
+
# site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
|
31
|
+
#
|
32
|
+
class Resource
|
33
|
+
attr_reader :url, :options
|
34
|
+
|
35
|
+
def initialize(url, options={}, backwards_compatibility=nil)
|
36
|
+
@url = url
|
37
|
+
if options.class == Hash
|
38
|
+
@options = options
|
39
|
+
else # compatibility with previous versions
|
40
|
+
@options = { :user => options, :password => backwards_compatibility }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def get(additional_headers={})
|
45
|
+
Request.execute(options.merge(
|
46
|
+
:method => :get,
|
47
|
+
:url => url,
|
48
|
+
:headers => headers.merge(additional_headers)
|
49
|
+
))
|
50
|
+
end
|
51
|
+
|
52
|
+
def post(payload, additional_headers={})
|
53
|
+
Request.execute(options.merge(
|
54
|
+
:method => :post,
|
55
|
+
:url => url,
|
56
|
+
:payload => payload,
|
57
|
+
:headers => headers.merge(additional_headers)
|
58
|
+
))
|
59
|
+
end
|
60
|
+
|
61
|
+
def put(payload, additional_headers={})
|
62
|
+
Request.execute(options.merge(
|
63
|
+
:method => :put,
|
64
|
+
:url => url,
|
65
|
+
:payload => payload,
|
66
|
+
:headers => headers.merge(additional_headers)
|
67
|
+
))
|
68
|
+
end
|
69
|
+
|
70
|
+
def delete(additional_headers={})
|
71
|
+
Request.execute(options.merge(
|
72
|
+
:method => :delete,
|
73
|
+
:url => url,
|
74
|
+
:headers => headers.merge(additional_headers)
|
75
|
+
))
|
76
|
+
end
|
77
|
+
|
78
|
+
def to_s
|
79
|
+
url
|
80
|
+
end
|
81
|
+
|
82
|
+
def user
|
83
|
+
options[:user]
|
84
|
+
end
|
85
|
+
|
86
|
+
def password
|
87
|
+
options[:password]
|
88
|
+
end
|
89
|
+
|
90
|
+
def headers
|
91
|
+
options[:headers] || {}
|
92
|
+
end
|
93
|
+
|
94
|
+
def timeout
|
95
|
+
options[:timeout]
|
96
|
+
end
|
97
|
+
|
98
|
+
# Construct a subresource, preserving authentication.
|
99
|
+
#
|
100
|
+
# Example:
|
101
|
+
#
|
102
|
+
# site = RestClient::Resource.new('http://example.com', 'adam', 'mypasswd')
|
103
|
+
# site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
|
104
|
+
#
|
105
|
+
# This is especially useful if you wish to define your site in one place and
|
106
|
+
# call it in multiple locations:
|
107
|
+
#
|
108
|
+
# def orders
|
109
|
+
# RestClient::Resource.new('http://example.com/orders', 'admin', 'mypasswd')
|
110
|
+
# end
|
111
|
+
#
|
112
|
+
# orders.get # GET http://example.com/orders
|
113
|
+
# orders['1'].get # GET http://example.com/orders/1
|
114
|
+
# orders['1/items'].delete # DELETE http://example.com/orders/1/items
|
115
|
+
#
|
116
|
+
# Nest resources as far as you want:
|
117
|
+
#
|
118
|
+
# site = RestClient::Resource.new('http://example.com')
|
119
|
+
# posts = site['posts']
|
120
|
+
# first_post = posts['1']
|
121
|
+
# comments = first_post['comments']
|
122
|
+
# comments.post 'Hello', :content_type => 'text/plain'
|
123
|
+
#
|
124
|
+
def [](suburl)
|
125
|
+
self.class.new(concat_urls(url, suburl), options)
|
126
|
+
end
|
127
|
+
|
128
|
+
def concat_urls(url, suburl) # :nodoc:
|
129
|
+
url = url.to_s
|
130
|
+
suburl = suburl.to_s
|
131
|
+
if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/'
|
132
|
+
url + suburl
|
133
|
+
else
|
134
|
+
"#{url}/#{suburl}"
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module RestClient
|
2
|
+
# The response from RestClient looks like a string, but is actually one of
|
3
|
+
# these. 99% of the time you're making a rest call all you care about is
|
4
|
+
# the body, but on the occassion you want to fetch the headers you can:
|
5
|
+
#
|
6
|
+
# RestClient.get('http://example.com').headers[:content_type]
|
7
|
+
#
|
8
|
+
class Response < String
|
9
|
+
attr_reader :net_http_res
|
10
|
+
|
11
|
+
def initialize(string, net_http_res)
|
12
|
+
@net_http_res = net_http_res
|
13
|
+
super string
|
14
|
+
end
|
15
|
+
|
16
|
+
# HTTP status code, always 200 since RestClient throws exceptions for
|
17
|
+
# other codes.
|
18
|
+
def code
|
19
|
+
@code ||= @net_http_res.code.to_i
|
20
|
+
end
|
21
|
+
|
22
|
+
# A hash of the headers, beautified with symbols and underscores.
|
23
|
+
# e.g. "Content-type" will become :content_type.
|
24
|
+
def headers
|
25
|
+
@headers ||= self.class.beautify_headers(@net_http_res.to_hash)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.beautify_headers(headers)
|
29
|
+
headers.inject({}) do |out, (key, value)|
|
30
|
+
out[key.gsub(/-/, '_').to_sym] = value.first
|
31
|
+
out
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/rest-client.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "rest-client"
|
3
|
+
s.version = "0.9"
|
4
|
+
s.summary = "Simple REST client for Ruby, inspired by microframework syntax for specifying actions."
|
5
|
+
s.description = "A simple REST client for Ruby, inspired by the Sinatra microframework style of specifying actions: get, put, post, delete."
|
6
|
+
s.author = "Adam Wiggins"
|
7
|
+
s.email = "adam@heroku.com"
|
8
|
+
s.rubyforge_project = "rest-client"
|
9
|
+
s.homepage = "http://rest-client.heroku.com/"
|
10
|
+
s.has_rdoc = true
|
11
|
+
s.platform = Gem::Platform::RUBY
|
12
|
+
s.files = %w(Rakefile README.rdoc rest-client.gemspec
|
13
|
+
lib/restclient/request.rb lib/restclient/response.rb
|
14
|
+
lib/restclient/exceptions.rb lib/restclient/resource.rb
|
15
|
+
spec/base.rb spec/request_spec.rb spec/response_spec.rb
|
16
|
+
spec/exceptions_spec.rb spec/resource_spec.rb spec/restclient_spec.rb
|
17
|
+
bin/restclient)
|
18
|
+
s.executables = ['restclient']
|
19
|
+
s.require_path = "lib"
|
20
|
+
end
|
data/spec/base.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/base'
|
2
|
+
|
3
|
+
describe RestClient::Exception do
|
4
|
+
it "sets the exception message to ErrorMessage" do
|
5
|
+
RestClient::ResourceNotFound.new.message.should == 'Resource not found'
|
6
|
+
end
|
7
|
+
|
8
|
+
it "contains exceptions in RestClient" do
|
9
|
+
RestClient::Unauthorized.new.should be_a_kind_of(RestClient::Exception)
|
10
|
+
RestClient::ServerBrokeConnection.new.should be_a_kind_of(RestClient::Exception)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe RestClient::RequestFailed do
|
15
|
+
it "stores the http response on the exception" do
|
16
|
+
begin
|
17
|
+
raise RestClient::RequestFailed, :response
|
18
|
+
rescue RestClient::RequestFailed => e
|
19
|
+
e.response.should == :response
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "http_code convenience method for fetching the code as an integer" do
|
24
|
+
RestClient::RequestFailed.new(mock('res', :code => '502')).http_code.should == 502
|
25
|
+
end
|
26
|
+
|
27
|
+
it "shows the status code in the message" do
|
28
|
+
RestClient::RequestFailed.new(mock('res', :code => '502')).to_s.should match(/502/)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe RestClient::ResourceNotFound do
|
33
|
+
it "also has the http response attached" do
|
34
|
+
begin
|
35
|
+
raise RestClient::ResourceNotFound, :response
|
36
|
+
rescue RestClient::ResourceNotFound => e
|
37
|
+
e.response.should == :response
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "backwards compatibility" do
|
43
|
+
it "alias RestClient::Request::Redirect to RestClient::Redirect" do
|
44
|
+
RestClient::Request::Redirect.should == RestClient::Redirect
|
45
|
+
end
|
46
|
+
|
47
|
+
it "alias RestClient::Request::Unauthorized to RestClient::Unauthorized" do
|
48
|
+
RestClient::Request::Unauthorized.should == RestClient::Unauthorized
|
49
|
+
end
|
50
|
+
|
51
|
+
it "alias RestClient::Request::RequestFailed to RestClient::RequestFailed" do
|
52
|
+
RestClient::Request::RequestFailed.should == RestClient::RequestFailed
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,296 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/base'
|
2
|
+
|
3
|
+
describe RestClient::Request do
|
4
|
+
before do
|
5
|
+
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
|
6
|
+
|
7
|
+
@uri = mock("uri")
|
8
|
+
@uri.stub!(:request_uri).and_return('/resource')
|
9
|
+
@uri.stub!(:host).and_return('some')
|
10
|
+
@uri.stub!(:port).and_return(80)
|
11
|
+
|
12
|
+
@net = mock("net::http base")
|
13
|
+
@http = mock("net::http connection")
|
14
|
+
Net::HTTP.stub!(:new).and_return(@net)
|
15
|
+
@net.stub!(:start).and_yield(@http)
|
16
|
+
@net.stub!(:use_ssl=)
|
17
|
+
@net.stub!(:verify_mode=)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "requests xml mimetype" do
|
21
|
+
@request.default_headers[:accept].should == 'application/xml'
|
22
|
+
end
|
23
|
+
|
24
|
+
it "decodes an uncompressed result body by passing it straight through" do
|
25
|
+
@request.decode(nil, 'xyz').should == 'xyz'
|
26
|
+
end
|
27
|
+
|
28
|
+
it "decodes a gzip body" do
|
29
|
+
@request.decode('gzip', "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000").should == "i'm gziped\n"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "decodes a deflated body" do
|
33
|
+
@request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363").should == "some deflated text"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "processes a successful result" do
|
37
|
+
res = mock("result")
|
38
|
+
res.stub!(:code).and_return("200")
|
39
|
+
res.stub!(:body).and_return('body')
|
40
|
+
res.stub!(:[]).with('content-encoding').and_return(nil)
|
41
|
+
@request.process_result(res).should == 'body'
|
42
|
+
end
|
43
|
+
|
44
|
+
it "parses a url into a URI object" do
|
45
|
+
URI.should_receive(:parse).with('http://example.com/resource')
|
46
|
+
@request.parse_url('http://example.com/resource')
|
47
|
+
end
|
48
|
+
|
49
|
+
it "adds http:// to the front of resources specified in the syntax example.com/resource" do
|
50
|
+
URI.should_receive(:parse).with('http://example.com/resource')
|
51
|
+
@request.parse_url('example.com/resource')
|
52
|
+
end
|
53
|
+
|
54
|
+
it "extracts the username and password when parsing http://user:password@example.com/" do
|
55
|
+
URI.stub!(:parse).and_return(mock('uri', :user => 'joe', :password => 'pass1'))
|
56
|
+
@request.parse_url_with_auth('http://joe:pass1@example.com/resource')
|
57
|
+
@request.user.should == 'joe'
|
58
|
+
@request.password.should == 'pass1'
|
59
|
+
end
|
60
|
+
|
61
|
+
it "doesn't overwrite user and password (which may have already been set by the Resource constructor) if there is no user/password in the url" do
|
62
|
+
URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
|
63
|
+
@request = RestClient::Request.new(:method => 'get', :url => 'example.com', :user => 'beth', :password => 'pass2')
|
64
|
+
@request.parse_url_with_auth('http://example.com/resource')
|
65
|
+
@request.user.should == 'beth'
|
66
|
+
@request.password.should == 'pass2'
|
67
|
+
end
|
68
|
+
|
69
|
+
it "determines the Net::HTTP class to instantiate by the method name" do
|
70
|
+
@request.net_http_request_class(:put).should == Net::HTTP::Put
|
71
|
+
end
|
72
|
+
|
73
|
+
it "merges user headers with the default headers" do
|
74
|
+
@request.should_receive(:default_headers).and_return({ '1' => '2' })
|
75
|
+
@request.make_headers('3' => '4').should == { '1' => '2', '3' => '4' }
|
76
|
+
end
|
77
|
+
|
78
|
+
it "prefers the user header when the same header exists in the defaults" do
|
79
|
+
@request.should_receive(:default_headers).and_return({ '1' => '2' })
|
80
|
+
@request.make_headers('1' => '3').should == { '1' => '3' }
|
81
|
+
end
|
82
|
+
|
83
|
+
it "converts header symbols from :content_type to 'Content-type'" do
|
84
|
+
@request.should_receive(:default_headers).and_return({})
|
85
|
+
@request.make_headers(:content_type => 'abc').should == { 'Content-type' => 'abc' }
|
86
|
+
end
|
87
|
+
|
88
|
+
it "converts header values to strings" do
|
89
|
+
@request.make_headers('A' => 1)['A'].should == '1'
|
90
|
+
end
|
91
|
+
|
92
|
+
it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
|
93
|
+
@request.should_receive(:parse_url_with_auth).with('http://some/resource').and_return(@uri)
|
94
|
+
klass = mock("net:http class")
|
95
|
+
@request.should_receive(:net_http_request_class).with(:put).and_return(klass)
|
96
|
+
klass.should_receive(:new).and_return('result')
|
97
|
+
@request.should_receive(:transmit).with(@uri, 'result', 'payload')
|
98
|
+
@request.execute_inner
|
99
|
+
end
|
100
|
+
|
101
|
+
it "transmits the request with Net::HTTP" do
|
102
|
+
@http.should_receive(:request).with('req', 'payload')
|
103
|
+
@request.should_receive(:process_result)
|
104
|
+
@request.should_receive(:response_log)
|
105
|
+
@request.transmit(@uri, 'req', 'payload')
|
106
|
+
end
|
107
|
+
|
108
|
+
it "uses SSL when the URI refers to a https address" do
|
109
|
+
@uri.stub!(:is_a?).with(URI::HTTPS).and_return(true)
|
110
|
+
@net.should_receive(:use_ssl=).with(true)
|
111
|
+
@http.stub!(:request)
|
112
|
+
@request.stub!(:process_result)
|
113
|
+
@request.stub!(:response_log)
|
114
|
+
@request.transmit(@uri, 'req', 'payload')
|
115
|
+
end
|
116
|
+
|
117
|
+
it "sends nil payloads" do
|
118
|
+
@http.should_receive(:request).with('req', nil)
|
119
|
+
@request.should_receive(:process_result)
|
120
|
+
@request.stub!(:response_log)
|
121
|
+
@request.transmit(@uri, 'req', nil)
|
122
|
+
end
|
123
|
+
|
124
|
+
it "passes non-hash payloads straight through" do
|
125
|
+
@request.process_payload("x").should == "x"
|
126
|
+
end
|
127
|
+
|
128
|
+
it "converts a hash payload to urlencoded data" do
|
129
|
+
@request.process_payload(:a => 'b c+d').should == "a=b%20c%2Bd"
|
130
|
+
end
|
131
|
+
|
132
|
+
it "accepts nested hashes in payload" do
|
133
|
+
payload = @request.process_payload(:user => { :name => 'joe', :location => { :country => 'USA', :state => 'CA' }})
|
134
|
+
payload.should include('user[name]=joe')
|
135
|
+
payload.should include('user[location][country]=USA')
|
136
|
+
payload.should include('user[location][state]=CA')
|
137
|
+
end
|
138
|
+
|
139
|
+
it "set urlencoded content_type header on hash payloads" do
|
140
|
+
@request.process_payload(:a => 1)
|
141
|
+
@request.headers[:content_type].should == 'application/x-www-form-urlencoded'
|
142
|
+
end
|
143
|
+
|
144
|
+
it "sets up the credentials prior to the request" do
|
145
|
+
@http.stub!(:request)
|
146
|
+
@request.stub!(:process_result)
|
147
|
+
@request.stub!(:response_log)
|
148
|
+
|
149
|
+
@request.stub!(:user).and_return('joe')
|
150
|
+
@request.stub!(:password).and_return('mypass')
|
151
|
+
@request.should_receive(:setup_credentials).with('req')
|
152
|
+
|
153
|
+
@request.transmit(@uri, 'req', nil)
|
154
|
+
end
|
155
|
+
|
156
|
+
it "does not attempt to send any credentials if user is nil" do
|
157
|
+
@request.stub!(:user).and_return(nil)
|
158
|
+
req = mock("request")
|
159
|
+
req.should_not_receive(:basic_auth)
|
160
|
+
@request.setup_credentials(req)
|
161
|
+
end
|
162
|
+
|
163
|
+
it "setup credentials when there's a user" do
|
164
|
+
@request.stub!(:user).and_return('joe')
|
165
|
+
@request.stub!(:password).and_return('mypass')
|
166
|
+
req = mock("request")
|
167
|
+
req.should_receive(:basic_auth).with('joe', 'mypass')
|
168
|
+
@request.setup_credentials(req)
|
169
|
+
end
|
170
|
+
|
171
|
+
it "catches EOFError and shows the more informative ServerBrokeConnection" do
|
172
|
+
@http.stub!(:request).and_raise(EOFError)
|
173
|
+
lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::ServerBrokeConnection)
|
174
|
+
end
|
175
|
+
|
176
|
+
it "execute calls execute_inner" do
|
177
|
+
@request.should_receive(:execute_inner)
|
178
|
+
@request.execute
|
179
|
+
end
|
180
|
+
|
181
|
+
it "class method execute wraps constructor" do
|
182
|
+
req = mock("rest request")
|
183
|
+
RestClient::Request.should_receive(:new).with(1 => 2).and_return(req)
|
184
|
+
req.should_receive(:execute)
|
185
|
+
RestClient::Request.execute(1 => 2)
|
186
|
+
end
|
187
|
+
|
188
|
+
it "raises a Redirect with the new location when the response is in the 30x range" do
|
189
|
+
res = mock('response', :code => '301', :header => { 'Location' => 'http://new/resource' })
|
190
|
+
lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://new/resource'}
|
191
|
+
end
|
192
|
+
|
193
|
+
it "handles redirects with relative paths" do
|
194
|
+
res = mock('response', :code => '301', :header => { 'Location' => 'index' })
|
195
|
+
lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
|
196
|
+
end
|
197
|
+
|
198
|
+
it "handles redirects with absolute paths" do
|
199
|
+
@request.instance_variable_set('@url', 'http://some/place/else')
|
200
|
+
res = mock('response', :code => '301', :header => { 'Location' => '/index' })
|
201
|
+
lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://some/index' }
|
202
|
+
end
|
203
|
+
|
204
|
+
it "raises Unauthorized when the response is 401" do
|
205
|
+
res = mock('response', :code => '401')
|
206
|
+
lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized)
|
207
|
+
end
|
208
|
+
|
209
|
+
it "raises ResourceNotFound when the response is 404" do
|
210
|
+
res = mock('response', :code => '404')
|
211
|
+
lambda { @request.process_result(res) }.should raise_error(RestClient::ResourceNotFound)
|
212
|
+
end
|
213
|
+
|
214
|
+
it "raises RequestFailed otherwise" do
|
215
|
+
res = mock('response', :code => '500')
|
216
|
+
lambda { @request.process_result(res) }.should raise_error(RestClient::RequestFailed)
|
217
|
+
end
|
218
|
+
|
219
|
+
it "creates a proxy class if a proxy url is given" do
|
220
|
+
RestClient.stub!(:proxy).and_return("http://example.com/")
|
221
|
+
@request.net_http_class.should include(Net::HTTP::ProxyDelta)
|
222
|
+
end
|
223
|
+
|
224
|
+
it "creates a non-proxy class if a proxy url is not given" do
|
225
|
+
@request.net_http_class.should_not include(Net::HTTP::ProxyDelta)
|
226
|
+
end
|
227
|
+
|
228
|
+
it "logs a get request" do
|
229
|
+
RestClient::Request.new(:method => :get, :url => 'http://url').request_log.should ==
|
230
|
+
'RestClient.get "http://url"'
|
231
|
+
end
|
232
|
+
|
233
|
+
it "logs a post request with a small payload" do
|
234
|
+
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').request_log.should ==
|
235
|
+
'RestClient.post "http://url", "foo"'
|
236
|
+
end
|
237
|
+
|
238
|
+
it "logs a post request with a large payload" do
|
239
|
+
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).request_log.should ==
|
240
|
+
'RestClient.post "http://url", "(1000 byte payload)"'
|
241
|
+
end
|
242
|
+
|
243
|
+
it "logs input headers as a hash" do
|
244
|
+
RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' }).request_log.should ==
|
245
|
+
'RestClient.get "http://url", :accept=>"text/plain"'
|
246
|
+
end
|
247
|
+
|
248
|
+
it "logs a response including the status code, content type, and result body size in bytes" do
|
249
|
+
res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
|
250
|
+
res.stub!(:[]).with('Content-type').and_return('text/html')
|
251
|
+
@request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
|
252
|
+
end
|
253
|
+
|
254
|
+
it "logs a response with a nil Content-type" do
|
255
|
+
res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
|
256
|
+
res.stub!(:[]).with('Content-type').and_return(nil)
|
257
|
+
@request.response_log(res).should == "# => 200 OK | 4 bytes"
|
258
|
+
end
|
259
|
+
|
260
|
+
it "strips the charset from the response content type" do
|
261
|
+
res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
|
262
|
+
res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
|
263
|
+
@request.response_log(res).should == "# => 200 OK | text/html 4 bytes"
|
264
|
+
end
|
265
|
+
|
266
|
+
it "displays the log to stdout" do
|
267
|
+
RestClient.stub!(:log).and_return('stdout')
|
268
|
+
STDOUT.should_receive(:puts).with('xyz')
|
269
|
+
@request.display_log('xyz')
|
270
|
+
end
|
271
|
+
|
272
|
+
it "displays the log to stderr" do
|
273
|
+
RestClient.stub!(:log).and_return('stderr')
|
274
|
+
STDERR.should_receive(:puts).with('xyz')
|
275
|
+
@request.display_log('xyz')
|
276
|
+
end
|
277
|
+
|
278
|
+
it "append the log to the requested filename" do
|
279
|
+
RestClient.stub!(:log).and_return('/tmp/restclient.log')
|
280
|
+
f = mock('file handle')
|
281
|
+
File.should_receive(:open).with('/tmp/restclient.log', 'a').and_yield(f)
|
282
|
+
f.should_receive(:puts).with('xyz')
|
283
|
+
@request.display_log('xyz')
|
284
|
+
end
|
285
|
+
|
286
|
+
it "set read_timeout" do
|
287
|
+
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123)
|
288
|
+
@http.stub!(:request)
|
289
|
+
@request.stub!(:process_result)
|
290
|
+
@request.stub!(:response_log)
|
291
|
+
|
292
|
+
@http.should_receive(:read_timeout=).with(123)
|
293
|
+
|
294
|
+
@request.transmit(@uri, 'req', nil)
|
295
|
+
end
|
296
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/base'
|
2
|
+
|
3
|
+
describe RestClient::Resource do
|
4
|
+
before do
|
5
|
+
@resource = RestClient::Resource.new('http://some/resource', :user => 'jane', :password => 'mypass', :headers => { 'X-Something' => '1'})
|
6
|
+
end
|
7
|
+
|
8
|
+
context "Resource delegation" do
|
9
|
+
it "GET" do
|
10
|
+
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => { 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
11
|
+
@resource.get
|
12
|
+
end
|
13
|
+
|
14
|
+
it "POST" do
|
15
|
+
RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
16
|
+
@resource.post 'abc', :content_type => 'image/jpg'
|
17
|
+
end
|
18
|
+
|
19
|
+
it "PUT" do
|
20
|
+
RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'abc', :headers => { :content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
21
|
+
@resource.put 'abc', :content_type => 'image/jpg'
|
22
|
+
end
|
23
|
+
|
24
|
+
it "DELETE" do
|
25
|
+
RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => { 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
|
26
|
+
@resource.delete
|
27
|
+
end
|
28
|
+
|
29
|
+
it "overrides resource headers" do
|
30
|
+
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => { 'X-Something' => '2'}, :user => 'jane', :password => 'mypass')
|
31
|
+
@resource.get 'X-Something' => '2'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it "can instantiate with no user/password" do
|
36
|
+
@resource = RestClient::Resource.new('http://some/resource')
|
37
|
+
end
|
38
|
+
|
39
|
+
it "is backwards compatible with previous constructor" do
|
40
|
+
@resource = RestClient::Resource.new('http://some/resource', 'user', 'pass')
|
41
|
+
@resource.user.should == 'user'
|
42
|
+
@resource.password.should == 'pass'
|
43
|
+
end
|
44
|
+
|
45
|
+
it "concatinates urls, inserting a slash when it needs one" do
|
46
|
+
@resource.concat_urls('http://example.com', 'resource').should == 'http://example.com/resource'
|
47
|
+
end
|
48
|
+
|
49
|
+
it "concatinates urls, using no slash if the first url ends with a slash" do
|
50
|
+
@resource.concat_urls('http://example.com/', 'resource').should == 'http://example.com/resource'
|
51
|
+
end
|
52
|
+
|
53
|
+
it "concatinates urls, using no slash if the second url starts with a slash" do
|
54
|
+
@resource.concat_urls('http://example.com', '/resource').should == 'http://example.com/resource'
|
55
|
+
end
|
56
|
+
|
57
|
+
it "concatinates even non-string urls, :posts + 1 => 'posts/1'" do
|
58
|
+
@resource.concat_urls(:posts, 1).should == 'posts/1'
|
59
|
+
end
|
60
|
+
|
61
|
+
it "offers subresources via []" do
|
62
|
+
parent = RestClient::Resource.new('http://example.com')
|
63
|
+
parent['posts'].url.should == 'http://example.com/posts'
|
64
|
+
end
|
65
|
+
|
66
|
+
it "transports options to subresources" do
|
67
|
+
parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password')
|
68
|
+
parent['posts'].user.should == 'user'
|
69
|
+
parent['posts'].password.should == 'password'
|
70
|
+
end
|
71
|
+
|
72
|
+
it "prints its url with to_s" do
|
73
|
+
RestClient::Resource.new('x').to_s.should == 'x'
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/base'
|
2
|
+
|
3
|
+
describe RestClient::Response do
|
4
|
+
before do
|
5
|
+
@net_http_res = mock('net http response')
|
6
|
+
@response = RestClient::Response.new('abc', @net_http_res)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "behaves like string" do
|
10
|
+
@response.should == 'abc'
|
11
|
+
end
|
12
|
+
|
13
|
+
it "fetches the numeric response code" do
|
14
|
+
@net_http_res.should_receive(:code).and_return('200')
|
15
|
+
@response.code.should == 200
|
16
|
+
end
|
17
|
+
|
18
|
+
it "beautifies the headers by turning the keys to symbols" do
|
19
|
+
h = RestClient::Response.beautify_headers('content-type' => [ 'x' ])
|
20
|
+
h.keys.first.should == :content_type
|
21
|
+
end
|
22
|
+
|
23
|
+
it "beautifies the headers by turning the values to strings instead of one-element arrays" do
|
24
|
+
h = RestClient::Response.beautify_headers('x' => [ 'text/html' ] )
|
25
|
+
h.values.first.should == 'text/html'
|
26
|
+
end
|
27
|
+
|
28
|
+
it "fetches the headers" do
|
29
|
+
@net_http_res.should_receive(:to_hash).and_return('content-type' => [ 'text/html' ])
|
30
|
+
@response.headers.should == { :content_type => 'text/html' }
|
31
|
+
end
|
32
|
+
|
33
|
+
it "can access the net http result directly" do
|
34
|
+
@response.net_http_res.should == @net_http_res
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/base'
|
2
|
+
|
3
|
+
describe RestClient do
|
4
|
+
describe "API" do
|
5
|
+
it "GET" do
|
6
|
+
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {})
|
7
|
+
RestClient.get('http://some/resource')
|
8
|
+
end
|
9
|
+
|
10
|
+
it "POST" do
|
11
|
+
RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'payload', :headers => {})
|
12
|
+
RestClient.post('http://some/resource', 'payload')
|
13
|
+
end
|
14
|
+
|
15
|
+
it "PUT" do
|
16
|
+
RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'payload', :headers => {})
|
17
|
+
RestClient.put('http://some/resource', 'payload')
|
18
|
+
end
|
19
|
+
|
20
|
+
it "DELETE" do
|
21
|
+
RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {})
|
22
|
+
RestClient.delete('http://some/resource')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "logging" do
|
27
|
+
after do
|
28
|
+
RestClient.log = nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it "gets the log source from the RESTCLIENT_LOG environment variable" do
|
32
|
+
ENV.stub!(:[]).with('RESTCLIENT_LOG').and_return('from env')
|
33
|
+
RestClient.log = 'from class method'
|
34
|
+
RestClient.log.should == 'from env'
|
35
|
+
end
|
36
|
+
|
37
|
+
it "sets a destination for log output, used if no environment variable is set" do
|
38
|
+
ENV.stub!(:[]).with('RESTCLIENT_LOG').and_return(nil)
|
39
|
+
RestClient.log = 'from class method'
|
40
|
+
RestClient.log.should == 'from class method'
|
41
|
+
end
|
42
|
+
|
43
|
+
it "returns nil (no logging) if neither are set (default)" do
|
44
|
+
ENV.stub!(:[]).with('RESTCLIENT_LOG').and_return(nil)
|
45
|
+
RestClient.log.should == nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: adamwiggins-rest-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.9"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Wiggins
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-11 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: "A simple REST client for Ruby, inspired by the Sinatra microframework style of specifying actions: get, put, post, delete."
|
17
|
+
email: adam@heroku.com
|
18
|
+
executables:
|
19
|
+
- restclient
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- README.rdoc
|
27
|
+
- rest-client.gemspec
|
28
|
+
- lib/restclient/request.rb
|
29
|
+
- lib/restclient/response.rb
|
30
|
+
- lib/restclient/exceptions.rb
|
31
|
+
- lib/restclient/resource.rb
|
32
|
+
- spec/base.rb
|
33
|
+
- spec/request_spec.rb
|
34
|
+
- spec/response_spec.rb
|
35
|
+
- spec/exceptions_spec.rb
|
36
|
+
- spec/resource_spec.rb
|
37
|
+
- spec/restclient_spec.rb
|
38
|
+
- bin/restclient
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://rest-client.heroku.com/
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project: rest-client
|
61
|
+
rubygems_version: 1.2.0
|
62
|
+
signing_key:
|
63
|
+
specification_version: 2
|
64
|
+
summary: Simple REST client for Ruby, inspired by microframework syntax for specifying actions.
|
65
|
+
test_files: []
|
66
|
+
|