rest-client 1.6.0

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.

Potentially problematic release.


This version of rest-client might be problematic. Click here for more details.

@@ -0,0 +1,269 @@
1
+ = REST Client -- simple DSL for accessing HTTP and REST resources
2
+
3
+ A simple HTTP and REST client for Ruby, inspired by the Sinatra's microframework style
4
+ of specifying actions: get, put, post, delete.
5
+
6
+ * Main page: http://github.com/archiloque/rest-client
7
+ * Mailing list: rest.client@librelist.com (send a mail to subscribe).
8
+ * IRC: #rest-client at freenode
9
+
10
+ == Usage: Raw URL
11
+
12
+ require 'rest_client'
13
+
14
+ RestClient.get 'http://example.com/resource'
15
+
16
+ RestClient.get 'http://example.com/resource', {:params => {:id => 50, 'foo' => 'bar'}}
17
+
18
+ RestClient.get 'https://user:password@example.com/private/resource', {:accept => :json}
19
+
20
+ RestClient.post 'http://example.com/resource', :param1 => 'one', :nested => { :param2 => 'two' }
21
+
22
+ RestClient.post "http://example.com/resource", { 'x' => 1 }.to_json, :content_type => :json, :accept => :json
23
+
24
+ RestClient.delete 'http://example.com/resource'
25
+
26
+ response = RestClient.get 'http://example.com/resource'
27
+ response.code
28
+ ➔ 200
29
+ response.cookies
30
+ ➔ {"Foo"=>"BAR", "QUUX"=>"QUUUUX"}
31
+ response.headers
32
+ ➔ {:content_type=>"text/html; charset=utf-8", :cache_control=>"private" ...
33
+ response.to_str
34
+ ➔ \n<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n \"http://www.w3.org/TR/html4/strict.dtd\">\n\n<html ....
35
+
36
+ RestClient.post( url,
37
+ {
38
+ :transfer => {
39
+ :path => '/foo/bar',
40
+ :owner => 'that_guy',
41
+ :group => 'those_guys'
42
+ },
43
+ :upload => {
44
+ :file => File.new(path, 'rb')
45
+ }
46
+ })
47
+
48
+ == Multipart
49
+
50
+ Yeah, that's right! This does multipart sends for you!
51
+
52
+ RestClient.post '/data', :myfile => File.new("/path/to/image.jpg", 'rb')
53
+
54
+ This does two things for you:
55
+
56
+ * Auto-detects that you have a File value sends it as multipart
57
+ * Auto-detects the mime of the file and sets it in the HEAD of the payload for each entry
58
+
59
+ If you are sending params that do not contain a File object but the payload needs to be multipart then:
60
+
61
+ RestClient.post '/data', :foo => 'bar', :multipart => true
62
+
63
+ == Usage: ActiveResource-Style
64
+
65
+ resource = RestClient::Resource.new 'http://example.com/resource'
66
+ resource.get
67
+
68
+ private_resource = RestClient::Resource.new 'https://example.com/private/resource', 'user', 'pass'
69
+ private_resource.put File.read('pic.jpg'), :content_type => 'image/jpg'
70
+
71
+ See RestClient::Resource module docs for details.
72
+
73
+ == Usage: Resource Nesting
74
+
75
+ site = RestClient::Resource.new('http://example.com')
76
+ site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
77
+
78
+ See RestClient::Resource docs for details.
79
+
80
+ == Exceptions (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)
81
+
82
+ * for results code between 200 and 207 a RestClient::Response will be returned
83
+ * for results code 301, 302 or 307 the redirection will be followed if the request is a get or a head
84
+ * for result code 303 the redirection will be followed and the request transformed into a get
85
+ * for other cases a RestClient::Exception holding the Response will be raised, a specific exception class will be thrown for know error codes
86
+
87
+ RestClient.get 'http://example.com/resource'
88
+ ➔ RestClient::ResourceNotFound: RestClient::ResourceNotFound
89
+
90
+ begin
91
+ RestClient.get 'http://example.com/resource'
92
+ rescue => e
93
+ e.response
94
+ end
95
+ ➔ 404 Resource Not Found | text/html 282 bytes
96
+
97
+ == Result handling
98
+
99
+ A block can be passed to the RestClient method, this block will then be called with the Response.
100
+ Response.return! can be called to invoke the default response's behavior.
101
+
102
+ # Don't raise exceptions but return the response
103
+ RestClient.get('http://example.com/resource'){|response, request| response }
104
+ ➔ 404 Resource Not Found | text/html 282 bytes
105
+
106
+ # Manage a specific error code
107
+ RestClient.get('http://my-rest-service.com/resource'){ |response, request, &block|
108
+ case response.code
109
+ when 200
110
+ p "It worked !"
111
+ response
112
+ when 423
113
+ raise SomeCustomExceptionIfYouWant
114
+ else
115
+ response.return!(request, &block)
116
+ end
117
+ }
118
+
119
+ # Follow redirections for all request types and not only for get and head
120
+ # RFC : "If the 301, 302 or 307 status code is received in response to a request other than GET or HEAD,
121
+ # the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user,
122
+ # since this might change the conditions under which the request was issued."
123
+ RestClient.get('http://my-rest-service.com/resource'){ |response, request, &block|
124
+ if [301, 302, 307].include? response.code
125
+ response.follow_redirection(request, &block)
126
+ else
127
+ response.return!(request, &block)
128
+ end
129
+ }
130
+
131
+ == Non-normalized URIs.
132
+
133
+ If you want to use non-normalized URIs, you can normalize them with the addressable gem (http://addressable.rubyforge.org/api/).
134
+
135
+ require 'addressable/uri'
136
+ RestClient.get(Addressable::URI.parse("http://www.詹姆斯.com/").normalize.to_str)
137
+
138
+ == Lower-level access
139
+
140
+ For cases not covered by the general API, you can use the RestClient::Resource class which provide a lower-level API, see the class' rdoc for more information.
141
+
142
+ == Shell
143
+
144
+ The restclient shell command gives an IRB session with RestClient already loaded:
145
+
146
+ $ restclient
147
+ >> RestClient.get 'http://example.com'
148
+
149
+ Specify a URL argument for get/post/put/delete on that resource:
150
+
151
+ $ restclient http://example.com
152
+ >> put '/resource', 'data'
153
+
154
+ Add a user and password for authenticated resources:
155
+
156
+ $ restclient https://example.com user pass
157
+ >> delete '/private/resource'
158
+
159
+ Create ~/.restclient for named sessions:
160
+
161
+ sinatra:
162
+ url: http://localhost:4567
163
+ rack:
164
+ url: http://localhost:9292
165
+ private_site:
166
+ url: http://example.com
167
+ username: user
168
+ password: pass
169
+
170
+ Then invoke:
171
+
172
+ $ restclient private_site
173
+
174
+ Use as a one-off, curl-style:
175
+
176
+ $ restclient get http://example.com/resource > output_body
177
+
178
+ $ restclient put http://example.com/resource < input_body
179
+
180
+ == Logging
181
+
182
+ To enable logging you can
183
+
184
+ * set RestClient.log with a ruby Logger
185
+ * or set an environment variable to avoid modifying the code (in this case you can use a file name, "stdout" or "stderr"):
186
+
187
+ $ RESTCLIENT_LOG=stdout path/to/my/program
188
+
189
+ Either produces logs like this:
190
+
191
+ RestClient.get "http://some/resource"
192
+ # => 200 OK | text/html 250 bytes
193
+ RestClient.put "http://some/resource", "payload"
194
+ # => 401 Unauthorized | application/xml 340 bytes
195
+
196
+ Note that these logs are valid Ruby, so you can paste them into the restclient
197
+ shell or a script to replay your sequence of rest calls.
198
+
199
+ == Proxy
200
+
201
+ All calls to RestClient, including Resources, will use the proxy specified by
202
+ RestClient.proxy:
203
+
204
+ RestClient.proxy = "http://proxy.example.com/"
205
+ RestClient.get "http://some/resource"
206
+ # => response from some/resource as proxied through proxy.example.com
207
+
208
+ Often the proxy url is set in an environment variable, so you can do this to
209
+ use whatever proxy the system is configured to use:
210
+
211
+ RestClient.proxy = ENV['http_proxy']
212
+
213
+ == Cookies
214
+
215
+ Request and Response objects know about HTTP cookies, and will automatically
216
+ extract and set headers for them as needed:
217
+
218
+ response = RestClient.get 'http://example.com/action_which_sets_session_id'
219
+ response.cookies
220
+ # => {"_applicatioN_session_id" => "1234"}
221
+
222
+ response2 = RestClient.post(
223
+ 'http://localhost:3000/',
224
+ {:param1 => "foo"},
225
+ {:cookies => {:session_id => "1234"}}
226
+ )
227
+ # ...response body
228
+
229
+ == SSL Client Certificates
230
+
231
+ RestClient::Resource.new(
232
+ 'https://example.com',
233
+ :ssl_client_cert => OpenSSL::X509::Certificate.new(File.read("cert.pem")),
234
+ :ssl_client_key => OpenSSL::PKey::RSA.new(File.read("key.pem"), "passphrase, if any"),
235
+ :ssl_ca_file => "ca_certificate.pem",
236
+ :verify_ssl => OpenSSL::SSL::VERIFY_PEER
237
+ ).get
238
+
239
+ Self-signed certificates can be generated with the openssl command-line tool.
240
+
241
+ == Hook
242
+
243
+ RestClient.add_before_execution_proc add a Proc to be called before each execution, it's handy if you need a direct access to the http request.
244
+
245
+ Example:
246
+
247
+ # Add oath support using the oauth gem
248
+ require 'oauth'
249
+ access_token = ...
250
+
251
+ RestClient.add_before_execution_proc do |req, params|
252
+ access_token.sign! req
253
+ end
254
+
255
+ RestClient.get 'http://example.com'
256
+
257
+ == More
258
+
259
+ Need caching, more advanced logging or any ability provided by a rack middleware ?
260
+
261
+ Have a look at rest-client-components http://github.com/crohr/rest-client-components
262
+
263
+ == Meta
264
+
265
+ Written by Adam Wiggins, major modifications by Blake Mizerany, maintained by Julien Kirch
266
+
267
+ Patches contributed by many, including Chris Anderson, Greg Borenstein, Ardekantur, Pedro Belo, Rafael Souza, Rick Olson, Aman Gupta, François Beausoleil and Nick Plante.
268
+
269
+ Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
@@ -0,0 +1,69 @@
1
+ require 'rake'
2
+
3
+ require 'jeweler'
4
+
5
+ Jeweler::Tasks.new do |s|
6
+ s.name = "rest-client"
7
+ s.description = "A simple HTTP and REST client for Ruby, inspired by the Sinatra microframework style of specifying actions: get, put, post, delete."
8
+ s.summary = "Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions."
9
+ s.author = "Adam Wiggins"
10
+ s.email = "rest.client@librelist.com"
11
+ s.homepage = "http://github.com/archiloque/rest-client"
12
+ s.rubyforge_project = "rest-client"
13
+ s.has_rdoc = true
14
+ s.files = FileList["[A-Z]*", "{bin,lib,spec}/**/*"]
15
+ s.executables = %w(restclient)
16
+ s.add_dependency("mime-types", ">= 1.16")
17
+ s.add_development_dependency("webmock", ">= 0.9.1")
18
+ s.add_development_dependency("rspec")
19
+ end
20
+
21
+ Jeweler::RubyforgeTasks.new
22
+
23
+ ############################
24
+
25
+ require 'spec/rake/spectask'
26
+
27
+ desc "Run all specs"
28
+ task :spec => ["spec:unit", "spec:integration"]
29
+
30
+ desc "Run unit specs"
31
+ Spec::Rake::SpecTask.new('spec:unit') do |t|
32
+ t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
33
+ t.spec_files = FileList['spec/*_spec.rb']
34
+ end
35
+
36
+ desc "Run integration specs"
37
+ Spec::Rake::SpecTask.new('spec:integration') do |t|
38
+ t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
39
+ t.spec_files = FileList['spec/integration/*_spec.rb']
40
+ end
41
+
42
+ desc "Print specdocs"
43
+ Spec::Rake::SpecTask.new(:doc) do |t|
44
+ t.spec_opts = ["--format", "specdoc", "--dry-run"]
45
+ t.spec_files = FileList['spec/*_spec.rb']
46
+ end
47
+
48
+ desc "Run all examples with RCov"
49
+ Spec::Rake::SpecTask.new('rcov') do |t|
50
+ t.spec_files = FileList['spec/*_spec.rb']
51
+ t.rcov = true
52
+ t.rcov_opts = ['--exclude', 'examples']
53
+ end
54
+
55
+ task :default => :spec
56
+
57
+ ############################
58
+
59
+ require 'rake/rdoctask'
60
+
61
+ Rake::RDocTask.new do |t|
62
+ t.rdoc_dir = 'rdoc'
63
+ t.title = "rest-client, fetch RESTful resources effortlessly"
64
+ t.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
65
+ t.options << '--charset' << 'utf-8'
66
+ t.rdoc_files.include('README.rdoc')
67
+ t.rdoc_files.include('lib/*.rb')
68
+ end
69
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.6.0
@@ -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?( File.expand_path(rcfile = "~/.restclientrc") )
81
+ load(rcfile)
82
+ end
83
+
84
+ ARGV.clear
85
+
86
+ IRB.start
87
+ exit!
@@ -0,0 +1,85 @@
1
+ # 1.6.0
2
+
3
+ - forgot to include rest-client.rb in the gem
4
+ - user, password and user-defined headers should survive a redirect
5
+ - added all missing status codes
6
+ - added parameter passing for get request using the :param key in header
7
+ - the warning about the logger when using a string was a bad idea
8
+ - multipart parameters names should not be escaped
9
+ - remove the cookie escaping introduced by migrating to CGI cookie parsing in 1.5.1
10
+ - add a streamed payload type (patch provided by Caleb Land)
11
+ - Exception#http_body works even when no response
12
+
13
+ # 1.5.1
14
+
15
+ - only converts headers keys which are Symbols
16
+ - use CGI for cookie parsing instead of custom code
17
+ - unescape user and password before using them (patch provided by Lars Gierth)
18
+ - expand ~ in ~/.restclientrc (patch provided by Mike Fletcher)
19
+ - ssl verification raise an exception when the ca certificate is incorrect (patch provided by Braintree)
20
+
21
+ # 1.5.0
22
+
23
+ - the response is now a String with the Response module a.k.a. the change in 1.4.0 was a mistake (Response.body is returning self for compatability)
24
+ - added AbstractResponse.to_i to improve semantic
25
+ - multipart Payloads ignores the name attribute if it's not set (patch provided by Tekin Suleyman)
26
+ - correctly takes into account user headers whose keys are strings (path provided by Cyril Rohr)
27
+ - use binary mode for payload temp file
28
+ - concatenate cookies with ';'
29
+ - fixed deeper parameter handling
30
+ - do not quote the boundary in the Content-Type header (patch provided by W. Andrew Loe III)
31
+
32
+ # 1.4.2
33
+
34
+ - fixed RestClient.add_before_execution_proc (patch provided by Nicholas Wieland)
35
+ - fixed error when an exception is raised without a response (patch provided by Caleb Land)
36
+
37
+ # 1.4.1
38
+
39
+ - fixed parameters managment when using hash
40
+
41
+ # 1.4.0
42
+
43
+ - Response is no more a String, and the mixin is replaced by an abstract_response, existing calls are redirected to response body with a warning.
44
+ - enable repeated parameters RestClient.post 'http://example.com/resource', :param1 => ['one', 'two', 'three'], => :param2 => 'foo' (patch provided by Rodrigo Panachi)
45
+ - fixed the redirect code concerning relative path and query string combination (patch provided by Kevin Read)
46
+ - redirection code moved to Response so redirection can be customized using the block syntax
47
+ - only get and head redirections are now followed by default, as stated in the specification
48
+ - added RestClient.add_before_execution_proc to hack the http request, like for oauth
49
+
50
+ The response change may be breaking in rare cases.
51
+
52
+ # 1.3.1
53
+
54
+ - added compatibility to enable responses in exception to act like Net::HTTPResponse
55
+
56
+ # 1.3.0
57
+
58
+ - a block can be used to process a request's result, this enable to handle custom error codes or paththrought (design by Cyril Rohr)
59
+ - cleaner log API, add a warning for some cases but should be compatible
60
+ - accept multiple "Set-Cookie" headers, see http://www.ietf.org/rfc/rfc2109.txt (patch provided by Cyril Rohr)
61
+ - remove "Content-Length" and "Content-Type" headers when following a redirection (patch provided by haarts)
62
+ - all http error codes have now a corresponding exception class and all of them contain the Reponse -> this means that the raised exception can be different
63
+ - changed "Content-Disposition: multipart/form-data" to "Content-Disposition: form-data" per RFC 2388 (patch provided by Kyle Crawford)
64
+
65
+ The only breaking change should be the exception classes, but as the new classes inherits from the existing ones, the breaking cases should be rare.
66
+
67
+ # 1.2.0
68
+
69
+ - formatting changed from tabs to spaces
70
+ - logged requests now include generated headers
71
+ - accept and content-type headers can now be specified using extentions: RestClient.post "http://example.com/resource", { 'x' => 1 }.to_json, :content_type => :json, :accept => :json
72
+ - should be 1.1.1 but renamed to 1.2.0 because 1.1.X versions has already been packaged on Debian
73
+
74
+ # 1.1.0
75
+
76
+ - new maintainer: Archiloque, the working repo is now at http://github.com/archiloque/rest-client
77
+ - a mailing list has been created at rest.client@librelist.com and an freenode irc channel #rest-client
78
+ - François Beausoleil' multipart code from http://github.com/francois/rest-client has been merged
79
+ - ability to use hash in hash as payload
80
+ - the mime-type code now rely on the mime-types gem http://mime-types.rubyforge.org/ instead of an internal partial list
81
+ - 204 response returns a Response instead of nil (patch provided by Elliott Draper)
82
+
83
+ All changes exept the last one should be fully compatible with the previous version.
84
+
85
+ NOTE: due to a dependency problem and to the last change, heroku users should update their heroku gem to >= 1.5.3 to be able to use this version.