isteel-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.
@@ -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
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "rest-client"
3
+ s.version = "0.9.3"
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 = "Ian Steel"
7
+ s.email = "ian@bilstone.co.uk"
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/rest_client.rb lib/restclient.rb
14
+ lib/restclient/request.rb lib/restclient/response.rb
15
+ lib/restclient/exceptions.rb lib/restclient/resource.rb
16
+ spec/base.rb spec/request_spec.rb spec/response_spec.rb
17
+ spec/exceptions_spec.rb spec/resource_spec.rb spec/restclient_spec.rb
18
+ bin/restclient)
19
+ s.executables = ['restclient']
20
+ s.require_path = "lib"
21
+ end
data/spec/base.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ require File.dirname(__FILE__) + '/../lib/restclient'
@@ -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