sevenwire-rest-client 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +151 -0
- data/Rakefile +48 -0
- data/VERSION.yml +4 -0
- data/bin/restclient +82 -0
- data/lib/rest_client.rb +2 -0
- data/lib/restclient/exceptions.rb +34 -0
- data/lib/restclient/mixin/response.rb +43 -0
- data/lib/restclient/raw_response.rb +30 -0
- data/lib/restclient/request.rb +232 -0
- data/lib/restclient/resource.rb +146 -0
- data/lib/restclient/response.rb +20 -0
- data/lib/restclient.rb +93 -0
- data/spec/base.rb +4 -0
- data/spec/exceptions_spec.rb +12 -0
- data/spec/mixin/response_spec.rb +46 -0
- data/spec/raw_response_spec.rb +17 -0
- data/spec/request_spec.rb +442 -0
- data/spec/resource_spec.rb +75 -0
- data/spec/response_spec.rb +16 -0
- data/spec/restclient_spec.rb +53 -0
- metadata +79 -0
@@ -0,0 +1,146 @@
|
|
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
|
+
# With an open timeout (seconds):
|
20
|
+
#
|
21
|
+
# RestClient::Resource.new('http://behindfirewall', :open_timeout => 10)
|
22
|
+
#
|
23
|
+
# You can also use resources to share common headers. For headers keys,
|
24
|
+
# symbols are converted to strings. Example:
|
25
|
+
#
|
26
|
+
# resource = RestClient::Resource.new('http://some/resource', :headers => { :client_version => 1 })
|
27
|
+
#
|
28
|
+
# This header will be transported as X-Client-Version (notice the X prefix,
|
29
|
+
# capitalization and hyphens)
|
30
|
+
#
|
31
|
+
# Use the [] syntax to allocate subresources:
|
32
|
+
#
|
33
|
+
# site = RestClient::Resource.new('http://example.com', :user => 'adam', :password => 'mypasswd')
|
34
|
+
# site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
|
35
|
+
#
|
36
|
+
class Resource
|
37
|
+
attr_reader :url, :options
|
38
|
+
|
39
|
+
def initialize(url, options={}, backwards_compatibility=nil)
|
40
|
+
@url = url
|
41
|
+
if options.class == Hash
|
42
|
+
@options = options
|
43
|
+
else # compatibility with previous versions
|
44
|
+
@options = { :user => options, :password => backwards_compatibility }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def get(additional_headers={})
|
49
|
+
Request.execute(options.merge(
|
50
|
+
:method => :get,
|
51
|
+
:url => url,
|
52
|
+
:headers => headers.merge(additional_headers)
|
53
|
+
))
|
54
|
+
end
|
55
|
+
|
56
|
+
def post(payload, additional_headers={})
|
57
|
+
Request.execute(options.merge(
|
58
|
+
:method => :post,
|
59
|
+
:url => url,
|
60
|
+
:payload => payload,
|
61
|
+
:headers => headers.merge(additional_headers)
|
62
|
+
))
|
63
|
+
end
|
64
|
+
|
65
|
+
def put(payload, additional_headers={})
|
66
|
+
Request.execute(options.merge(
|
67
|
+
:method => :put,
|
68
|
+
:url => url,
|
69
|
+
:payload => payload,
|
70
|
+
:headers => headers.merge(additional_headers)
|
71
|
+
))
|
72
|
+
end
|
73
|
+
|
74
|
+
def delete(additional_headers={})
|
75
|
+
Request.execute(options.merge(
|
76
|
+
:method => :delete,
|
77
|
+
:url => url,
|
78
|
+
:headers => headers.merge(additional_headers)
|
79
|
+
))
|
80
|
+
end
|
81
|
+
|
82
|
+
def to_s
|
83
|
+
url
|
84
|
+
end
|
85
|
+
|
86
|
+
def user
|
87
|
+
options[:user]
|
88
|
+
end
|
89
|
+
|
90
|
+
def password
|
91
|
+
options[:password]
|
92
|
+
end
|
93
|
+
|
94
|
+
def headers
|
95
|
+
options[:headers] || {}
|
96
|
+
end
|
97
|
+
|
98
|
+
def timeout
|
99
|
+
options[:timeout]
|
100
|
+
end
|
101
|
+
|
102
|
+
def open_timeout
|
103
|
+
options[:open_timeout]
|
104
|
+
end
|
105
|
+
|
106
|
+
# Construct a subresource, preserving authentication.
|
107
|
+
#
|
108
|
+
# Example:
|
109
|
+
#
|
110
|
+
# site = RestClient::Resource.new('http://example.com', 'adam', 'mypasswd')
|
111
|
+
# site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
|
112
|
+
#
|
113
|
+
# This is especially useful if you wish to define your site in one place and
|
114
|
+
# call it in multiple locations:
|
115
|
+
#
|
116
|
+
# def orders
|
117
|
+
# RestClient::Resource.new('http://example.com/orders', 'admin', 'mypasswd')
|
118
|
+
# end
|
119
|
+
#
|
120
|
+
# orders.get # GET http://example.com/orders
|
121
|
+
# orders['1'].get # GET http://example.com/orders/1
|
122
|
+
# orders['1/items'].delete # DELETE http://example.com/orders/1/items
|
123
|
+
#
|
124
|
+
# Nest resources as far as you want:
|
125
|
+
#
|
126
|
+
# site = RestClient::Resource.new('http://example.com')
|
127
|
+
# posts = site['posts']
|
128
|
+
# first_post = posts['1']
|
129
|
+
# comments = first_post['comments']
|
130
|
+
# comments.post 'Hello', :content_type => 'text/plain'
|
131
|
+
#
|
132
|
+
def [](suburl)
|
133
|
+
self.class.new(concat_urls(url, suburl), options)
|
134
|
+
end
|
135
|
+
|
136
|
+
def concat_urls(url, suburl) # :nodoc:
|
137
|
+
url = url.to_s
|
138
|
+
suburl = suburl.to_s
|
139
|
+
if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/'
|
140
|
+
url + suburl
|
141
|
+
else
|
142
|
+
"#{url}/#{suburl}"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/mixin/response'
|
2
|
+
|
3
|
+
module RestClient
|
4
|
+
# The response from RestClient looks like a string, but is actually one of
|
5
|
+
# these. 99% of the time you're making a rest call all you care about is
|
6
|
+
# the body, but on the occassion you want to fetch the headers you can:
|
7
|
+
#
|
8
|
+
# RestClient.get('http://example.com').headers[:content_type]
|
9
|
+
#
|
10
|
+
class Response < String
|
11
|
+
|
12
|
+
include RestClient::Mixin::Response
|
13
|
+
|
14
|
+
def initialize(string, net_http_res)
|
15
|
+
@net_http_res = net_http_res
|
16
|
+
super(string || "")
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
data/lib/restclient.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/https'
|
3
|
+
require 'zlib'
|
4
|
+
require 'stringio'
|
5
|
+
|
6
|
+
require File.dirname(__FILE__) + '/restclient/request'
|
7
|
+
require File.dirname(__FILE__) + '/restclient/mixin/response'
|
8
|
+
require File.dirname(__FILE__) + '/restclient/response'
|
9
|
+
require File.dirname(__FILE__) + '/restclient/raw_response'
|
10
|
+
require File.dirname(__FILE__) + '/restclient/resource'
|
11
|
+
require File.dirname(__FILE__) + '/restclient/exceptions'
|
12
|
+
|
13
|
+
# This module's static methods are the entry point for using the REST client.
|
14
|
+
#
|
15
|
+
# # GET
|
16
|
+
# xml = RestClient.get 'http://example.com/resource'
|
17
|
+
# jpg = RestClient.get 'http://example.com/resource', :accept => 'image/jpg'
|
18
|
+
#
|
19
|
+
# # authentication and SSL
|
20
|
+
# RestClient.get 'https://user:password@example.com/private/resource'
|
21
|
+
#
|
22
|
+
# # POST or PUT with a hash sends parameters as a urlencoded form body
|
23
|
+
# RestClient.post 'http://example.com/resource', :param1 => 'one'
|
24
|
+
#
|
25
|
+
# # nest hash parameters
|
26
|
+
# RestClient.post 'http://example.com/resource', :nested => { :param1 => 'one' }
|
27
|
+
#
|
28
|
+
# # POST and PUT with raw payloads
|
29
|
+
# RestClient.post 'http://example.com/resource', 'the post body', :content_type => 'text/plain'
|
30
|
+
# RestClient.post 'http://example.com/resource.xml', xml_doc
|
31
|
+
# RestClient.put 'http://example.com/resource.pdf', File.read('my.pdf'), :content_type => 'application/pdf'
|
32
|
+
#
|
33
|
+
# # DELETE
|
34
|
+
# RestClient.delete 'http://example.com/resource'
|
35
|
+
#
|
36
|
+
# # retreive the response http code and headers
|
37
|
+
# res = RestClient.get 'http://example.com/some.jpg'
|
38
|
+
# res.code # => 200
|
39
|
+
# res.headers[:content_type] # => 'image/jpg'
|
40
|
+
#
|
41
|
+
# # HEAD
|
42
|
+
# RestClient.head('http://example.com').headers
|
43
|
+
#
|
44
|
+
# To use with a proxy, just set RestClient.proxy to the proper http proxy:
|
45
|
+
#
|
46
|
+
# RestClient.proxy = "http://proxy.example.com/"
|
47
|
+
#
|
48
|
+
# Or inherit the proxy from the environment:
|
49
|
+
#
|
50
|
+
# RestClient.proxy = ENV['http_proxy']
|
51
|
+
#
|
52
|
+
# For live tests of RestClient, try using http://rest-test.heroku.com, which echoes back information about the rest call:
|
53
|
+
#
|
54
|
+
# >> RestClient.put 'http://rest-test.heroku.com/resource', :foo => 'baz'
|
55
|
+
# => "PUT http://rest-test.heroku.com/resource with a 7 byte payload, content type application/x-www-form-urlencoded {\"foo\"=>\"baz\"}"
|
56
|
+
#
|
57
|
+
module RestClient
|
58
|
+
def self.get(url, headers={})
|
59
|
+
Request.execute(:method => :get, :url => url, :headers => headers)
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.post(url, payload, headers={})
|
63
|
+
Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers)
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.put(url, payload, headers={})
|
67
|
+
Request.execute(:method => :put, :url => url, :payload => payload, :headers => headers)
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.delete(url, headers={})
|
71
|
+
Request.execute(:method => :delete, :url => url, :headers => headers)
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.head(url, headers={})
|
75
|
+
Request.execute(:method => :head, :url => url, :headers => headers)
|
76
|
+
end
|
77
|
+
|
78
|
+
class << self
|
79
|
+
attr_accessor :proxy
|
80
|
+
end
|
81
|
+
|
82
|
+
# Print log of RestClient calls. Value can be stdout, stderr, or a filename.
|
83
|
+
# You can also configure logging by the environment variable RESTCLIENT_LOG.
|
84
|
+
def self.log=(log)
|
85
|
+
@@log = log
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.log # :nodoc:
|
89
|
+
return ENV['RESTCLIENT_LOG'] if ENV['RESTCLIENT_LOG']
|
90
|
+
return @@log if defined? @@log
|
91
|
+
nil
|
92
|
+
end
|
93
|
+
end
|
data/spec/base.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/base'
|
2
|
+
|
3
|
+
describe RestClient::Exception do
|
4
|
+
it "sets the exception message to ErrorMessage" do
|
5
|
+
RestClient::ServerBrokeConnection.new.message.should == 'Server broke connection'
|
6
|
+
end
|
7
|
+
|
8
|
+
it "contains exceptions in RestClient" do
|
9
|
+
RestClient::ServerBrokeConnection.new.should be_a_kind_of(RestClient::Exception)
|
10
|
+
RestClient::RequestTimeout.new.should be_a_kind_of(RestClient::Exception)
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../base'
|
2
|
+
|
3
|
+
class MockResponse
|
4
|
+
include RestClient::Mixin::Response
|
5
|
+
|
6
|
+
def initialize(body, res)
|
7
|
+
@net_http_res = res
|
8
|
+
@body = @body
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe RestClient::Mixin::Response do
|
13
|
+
before do
|
14
|
+
@net_http_res = mock('net http response')
|
15
|
+
@response = MockResponse.new('abc', @net_http_res)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "fetches the numeric response code" do
|
19
|
+
@net_http_res.should_receive(:code).and_return('200')
|
20
|
+
@response.code.should == 200
|
21
|
+
end
|
22
|
+
|
23
|
+
it "beautifies the headers by turning the keys to symbols" do
|
24
|
+
h = RestClient::Response.beautify_headers('content-type' => [ 'x' ])
|
25
|
+
h.keys.first.should == :content_type
|
26
|
+
end
|
27
|
+
|
28
|
+
it "beautifies the headers by turning the values to strings instead of one-element arrays" do
|
29
|
+
h = RestClient::Response.beautify_headers('x' => [ 'text/html' ] )
|
30
|
+
h.values.first.should == 'text/html'
|
31
|
+
end
|
32
|
+
|
33
|
+
it "fetches the headers" do
|
34
|
+
@net_http_res.should_receive(:to_hash).and_return('content-type' => [ 'text/html' ])
|
35
|
+
@response.headers.should == { :content_type => 'text/html' }
|
36
|
+
end
|
37
|
+
|
38
|
+
it "extracts cookies from response headers" do
|
39
|
+
@net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
|
40
|
+
@response.cookies.should == { 'session_id' => '1' }
|
41
|
+
end
|
42
|
+
|
43
|
+
it "can access the net http result directly" do
|
44
|
+
@response.net_http_res.should == @net_http_res
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/base'
|
2
|
+
|
3
|
+
describe RestClient::RawResponse do
|
4
|
+
before do
|
5
|
+
@tf = mock("Tempfile", :read => "the answer is 42", :open => true)
|
6
|
+
@net_http_res = mock('net http response')
|
7
|
+
@response = RestClient::RawResponse.new(@tf, @net_http_res)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "behaves like string" do
|
11
|
+
@response.to_s.should == 'the answer is 42'
|
12
|
+
end
|
13
|
+
|
14
|
+
it "exposes a Tempfile" do
|
15
|
+
@response.file.should == @tf
|
16
|
+
end
|
17
|
+
end
|