rest-client 1.7.0.rc1-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rest-client might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/.travis.yml +14 -0
- data/AUTHORS +81 -0
- data/Gemfile +11 -0
- data/LICENSE +21 -0
- data/README.rdoc +325 -0
- data/Rakefile +117 -0
- data/bin/restclient +93 -0
- data/history.md +166 -0
- data/lib/rest-client.rb +2 -0
- data/lib/rest_client.rb +2 -0
- data/lib/restclient.rb +164 -0
- data/lib/restclient/abstract_response.rb +106 -0
- data/lib/restclient/exceptions.rb +203 -0
- data/lib/restclient/payload.rb +240 -0
- data/lib/restclient/platform.rb +30 -0
- data/lib/restclient/raw_response.rb +34 -0
- data/lib/restclient/request.rb +582 -0
- data/lib/restclient/resource.rb +169 -0
- data/lib/restclient/response.rb +24 -0
- data/lib/restclient/version.rb +7 -0
- data/lib/restclient/windows.rb +8 -0
- data/lib/restclient/windows/root_certs.rb +105 -0
- data/rest-client.gemspec +30 -0
- data/rest-client.windows.gemspec +19 -0
- data/spec/integration/capath_digicert/244b5494.0 +19 -0
- data/spec/integration/capath_digicert/81b9768f.0 +19 -0
- data/spec/integration/capath_digicert/README +8 -0
- data/spec/integration/capath_digicert/digicert.crt +19 -0
- data/spec/integration/capath_verisign/415660c1.0 +14 -0
- data/spec/integration/capath_verisign/7651b327.0 +14 -0
- data/spec/integration/capath_verisign/README +8 -0
- data/spec/integration/capath_verisign/verisign.crt +14 -0
- data/spec/integration/certs/digicert.crt +19 -0
- data/spec/integration/certs/verisign.crt +14 -0
- data/spec/integration/integration_spec.rb +35 -0
- data/spec/integration/request_spec.rb +104 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/unit/abstract_response_spec.rb +85 -0
- data/spec/unit/exceptions_spec.rb +95 -0
- data/spec/unit/master_shake.jpg +0 -0
- data/spec/unit/payload_spec.rb +245 -0
- data/spec/unit/raw_response_spec.rb +17 -0
- data/spec/unit/request2_spec.rb +32 -0
- data/spec/unit/request_spec.rb +905 -0
- data/spec/unit/resource_spec.rb +133 -0
- data/spec/unit/response_spec.rb +166 -0
- data/spec/unit/restclient_spec.rb +79 -0
- data/spec/unit/windows/root_certs_spec.rb +22 -0
- metadata +241 -0
data/Rakefile
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# load `rake build/install/release tasks'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require_relative './lib/restclient/version'
|
4
|
+
|
5
|
+
namespace :ruby do
|
6
|
+
Bundler::GemHelper.install_tasks(:name => 'rest-client')
|
7
|
+
end
|
8
|
+
|
9
|
+
require "rspec/core/rake_task"
|
10
|
+
|
11
|
+
desc "Run all specs"
|
12
|
+
RSpec::Core::RakeTask.new('spec')
|
13
|
+
|
14
|
+
desc "Run unit specs"
|
15
|
+
RSpec::Core::RakeTask.new('spec:unit') do |t|
|
16
|
+
t.pattern = 'spec/unit/*_spec.rb'
|
17
|
+
end
|
18
|
+
|
19
|
+
desc "Run integration specs"
|
20
|
+
RSpec::Core::RakeTask.new('spec:integration') do |t|
|
21
|
+
t.pattern = 'spec/integration/*_spec.rb'
|
22
|
+
end
|
23
|
+
|
24
|
+
desc "Print specdocs"
|
25
|
+
RSpec::Core::RakeTask.new(:doc) do |t|
|
26
|
+
t.rspec_opts = ["--format", "specdoc", "--dry-run"]
|
27
|
+
t.pattern = 'spec/**/*_spec.rb'
|
28
|
+
end
|
29
|
+
|
30
|
+
desc "Run all examples with RCov"
|
31
|
+
RSpec::Core::RakeTask.new('rcov') do |t|
|
32
|
+
t.pattern = 'spec/*_spec.rb'
|
33
|
+
t.rcov = true
|
34
|
+
t.rcov_opts = ['--exclude', 'examples']
|
35
|
+
end
|
36
|
+
|
37
|
+
task :default do
|
38
|
+
sh 'rake -T'
|
39
|
+
end
|
40
|
+
|
41
|
+
def alias_task(alias_task, original)
|
42
|
+
desc "Alias for rake #{original}"
|
43
|
+
task alias_task, Rake.application[original].arg_names => original
|
44
|
+
end
|
45
|
+
alias_task(:test, :spec)
|
46
|
+
|
47
|
+
############################
|
48
|
+
|
49
|
+
WindowsPlatforms = %w{x86-mingw32 x64-mingw32 x86-mswin32}
|
50
|
+
|
51
|
+
namespace :all do
|
52
|
+
|
53
|
+
desc "Build rest-client #{RestClient::VERSION} for all platforms"
|
54
|
+
task :build => ['ruby:build'] + \
|
55
|
+
WindowsPlatforms.map {|p| "windows:#{p}:build"}
|
56
|
+
|
57
|
+
desc "Create tag v#{RestClient::VERSION} and for all platforms build and push " \
|
58
|
+
"rest-client #{RestClient::VERSION} to Rubygems"
|
59
|
+
task :release => ['build', 'ruby:release'] + \
|
60
|
+
WindowsPlatforms.map {|p| "windows:#{p}:push"}
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
namespace :windows do
|
65
|
+
spec_path = File.join(File.dirname(__FILE__), 'rest-client.windows.gemspec')
|
66
|
+
|
67
|
+
WindowsPlatforms.each do |platform|
|
68
|
+
namespace platform do
|
69
|
+
gem_filename = "rest-client-#{RestClient::VERSION}-#{platform}.gem"
|
70
|
+
base = File.dirname(__FILE__)
|
71
|
+
pkg_dir = File.join(base, 'pkg')
|
72
|
+
gem_file_path = File.join(pkg_dir, gem_filename)
|
73
|
+
|
74
|
+
desc "Build #{gem_filename} into the pkg directory"
|
75
|
+
task 'build' do
|
76
|
+
orig_platform = ENV['BUILD_PLATFORM']
|
77
|
+
begin
|
78
|
+
ENV['BUILD_PLATFORM'] = platform
|
79
|
+
|
80
|
+
sh("gem build -V #{spec_path}") do |ok, res|
|
81
|
+
if ok
|
82
|
+
FileUtils.mkdir_p(pkg_dir)
|
83
|
+
FileUtils.mv(File.join(base, gem_filename), pkg_dir)
|
84
|
+
Bundler.ui.confirm("rest-client #{RestClient::VERSION} " +
|
85
|
+
"built to pkg/#{gem_filename}")
|
86
|
+
else
|
87
|
+
abort "Command `gem build` failed: #{res}"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
ensure
|
92
|
+
ENV['BUILD_PLATFORM'] = orig_platform
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
desc "Push #{gem_filename} to Rubygems"
|
97
|
+
task 'push' do
|
98
|
+
sh("gem push #{gem_file_path}")
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
############################
|
106
|
+
|
107
|
+
require 'rdoc/task'
|
108
|
+
|
109
|
+
Rake::RDocTask.new do |t|
|
110
|
+
t.rdoc_dir = 'rdoc'
|
111
|
+
t.title = "rest-client, fetch RESTful resources effortlessly"
|
112
|
+
t.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
|
113
|
+
t.options << '--charset' << 'utf-8'
|
114
|
+
t.rdoc_files.include('README.rdoc')
|
115
|
+
t.rdoc_files.include('lib/*.rb')
|
116
|
+
end
|
117
|
+
|
data/bin/restclient
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$:.unshift File.dirname(__FILE__) + "/../lib"
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'restclient'
|
7
|
+
require 'yaml'
|
8
|
+
|
9
|
+
def usage(why = nil)
|
10
|
+
puts "failed for reason: #{why}" if why
|
11
|
+
puts "usage: restclient [get|put|post|delete] url|name [username] [password]"
|
12
|
+
puts " The verb is optional, if you leave it off you'll get an interactive shell."
|
13
|
+
puts " put and post both take the input body on stdin."
|
14
|
+
exit(1)
|
15
|
+
end
|
16
|
+
|
17
|
+
POSSIBLE_VERBS = ['get', 'put', 'post', 'delete']
|
18
|
+
|
19
|
+
if POSSIBLE_VERBS.include? ARGV.first
|
20
|
+
@verb = ARGV.shift
|
21
|
+
else
|
22
|
+
@verb = nil
|
23
|
+
end
|
24
|
+
|
25
|
+
@url = ARGV.shift || 'http://localhost:4567'
|
26
|
+
|
27
|
+
config = YAML.load(File.read(ENV['HOME'] + "/.restclient")) rescue {}
|
28
|
+
|
29
|
+
@url, @username, @password = if c = config[@url]
|
30
|
+
[c['url'], c['username'], c['password']]
|
31
|
+
else
|
32
|
+
[@url, * ARGV]
|
33
|
+
end
|
34
|
+
|
35
|
+
usage("invalid url '#{@url}") unless @url =~ /^https?/
|
36
|
+
usage("too few args") unless ARGV.size < 3
|
37
|
+
|
38
|
+
def r
|
39
|
+
@r ||= RestClient::Resource.new(@url, @username, @password)
|
40
|
+
end
|
41
|
+
|
42
|
+
r # force rc to load
|
43
|
+
|
44
|
+
if @verb
|
45
|
+
begin
|
46
|
+
if %w( put post ).include? @verb
|
47
|
+
puts r.send(@verb, STDIN.read)
|
48
|
+
else
|
49
|
+
puts r.send(@verb)
|
50
|
+
end
|
51
|
+
exit 0
|
52
|
+
rescue RestClient::Exception => e
|
53
|
+
puts e.response.body if e.respond_to?(:response) && e.response
|
54
|
+
raise
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
POSSIBLE_VERBS.each do |m|
|
59
|
+
eval <<-end_eval
|
60
|
+
def #{m}(path, *args, &b)
|
61
|
+
r[path].#{m}(*args, &b)
|
62
|
+
end
|
63
|
+
end_eval
|
64
|
+
end
|
65
|
+
|
66
|
+
def method_missing(s, * args, & b)
|
67
|
+
if POSSIBLE_VERBS.include? s
|
68
|
+
begin
|
69
|
+
r.send(s, *args, & b)
|
70
|
+
rescue RestClient::RequestFailed => e
|
71
|
+
print STDERR, e.response.body
|
72
|
+
raise e
|
73
|
+
end
|
74
|
+
else
|
75
|
+
super
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
require 'irb'
|
80
|
+
require 'irb/completion'
|
81
|
+
|
82
|
+
if File.exists? ".irbrc"
|
83
|
+
ENV['IRBRC'] = ".irbrc"
|
84
|
+
end
|
85
|
+
|
86
|
+
if File.exists?(File.expand_path(rcfile = "~/.restclientrc"))
|
87
|
+
load(rcfile)
|
88
|
+
end
|
89
|
+
|
90
|
+
ARGV.clear
|
91
|
+
|
92
|
+
IRB.start
|
93
|
+
exit!
|
data/history.md
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
# 1.7.0
|
2
|
+
|
3
|
+
- This release drops support for Ruby 1.8.7 and breaks compatibility in a few
|
4
|
+
other relatively minor ways
|
5
|
+
- Upgrade to mime-types ~> 2.0
|
6
|
+
- Don't CGI.unescape cookie values sent to the server (issue #89)
|
7
|
+
- Add support for reading credentials from netrc
|
8
|
+
- Lots of SSL changes and enhancements: (#268)
|
9
|
+
- Enable peer verification by default (setting `VERIFY_PEER` with OpenSSL)
|
10
|
+
- By default, use the system default certificate store for SSL verification,
|
11
|
+
even on Windows (this uses a separate Windows build that pulls in ffi)
|
12
|
+
- Add support for SSL `ca_path`
|
13
|
+
- Add support for SSL `cert_store`
|
14
|
+
- Add support for SSL `verify_callback` (with some caveats for jruby, OS X, #277)
|
15
|
+
- Add support for SSL ciphers, and choose secure ones by default
|
16
|
+
- Run tests under travis
|
17
|
+
- Several other bugfixes and test improvements
|
18
|
+
- Convert Errno::ETIMEDOUT to RestClient::RequestTimeout
|
19
|
+
- Handle more HTTP response codes from recent standards
|
20
|
+
- Save raw responses to binary mode tempfile (#110)
|
21
|
+
- Disable timeouts with :timeout => nil rather than :timeout => -1
|
22
|
+
- Drop all Net::HTTP monkey patches
|
23
|
+
|
24
|
+
# 1.6.8
|
25
|
+
|
26
|
+
- The 1.6.x series will be the last to support Ruby 1.8.7
|
27
|
+
- Pin mime-types to < 2.0 to maintain Ruby 1.8.7 support
|
28
|
+
- Add Gemfile, AUTHORS, add license to gemspec
|
29
|
+
- Point homepage at https://github.com/rest-client/rest-client
|
30
|
+
- Clean up and fix various tests and ruby warnings
|
31
|
+
- Backport `ssl_verify_callback` functionality from 1.7.0
|
32
|
+
|
33
|
+
# 1.6.7
|
34
|
+
|
35
|
+
- rebuild with 1.8.7 to avoid https://github.com/rubygems/rubygems/pull/57
|
36
|
+
|
37
|
+
# 1.6.6
|
38
|
+
|
39
|
+
- 1.6.5 was yanked
|
40
|
+
|
41
|
+
# 1.6.5
|
42
|
+
|
43
|
+
- RFC6265 requires single SP after ';' for separating parameters pairs in the 'Cookie:' header (patch provided by Hiroshi Nakamura)
|
44
|
+
- enable url parameters for all actions
|
45
|
+
- detect file parameters in arrays
|
46
|
+
- allow disabling the timeouts by passing -1 (patch provided by Sven Böhm)
|
47
|
+
|
48
|
+
# 1.6.4
|
49
|
+
|
50
|
+
- fix restclient script compatibility with 1.9.2
|
51
|
+
- fix unlinking temp file (patch provided by Evan Smith)
|
52
|
+
- monkeypatching ruby for http patch method (patch provided by Syl Turner)
|
53
|
+
|
54
|
+
# 1.6.3
|
55
|
+
|
56
|
+
- 1.6.2 was yanked
|
57
|
+
|
58
|
+
# 1.6.2
|
59
|
+
|
60
|
+
- add support for HEAD in resources (patch provided by tpresa)
|
61
|
+
- fix shell for 1.9.2
|
62
|
+
- workaround when some gem monkeypatch net/http (patch provided by Ian Warshak)
|
63
|
+
- DELETE requests should process parameters just like GET and HEAD
|
64
|
+
- adding :block_response parameter for manual processing
|
65
|
+
- limit number of redirections (patch provided by Chris Dinn)
|
66
|
+
- close and unlink the temp file created by playload (patch provided by Chris Green)
|
67
|
+
- make gemspec Rubygems 1.8 compatible (patch provided by David Backeus)
|
68
|
+
- added RestClient.reset_before_execution_procs (patch provided by Cloudify)
|
69
|
+
- added PATCH method (patch provided by Jeff Remer)
|
70
|
+
- hack for HTTP servers that use raw DEFLATE compression, see http://www.ruby-forum.com/topic/136825 (path provided by James Reeves)
|
71
|
+
|
72
|
+
# 1.6.1
|
73
|
+
|
74
|
+
- add response body in Exception#inspect
|
75
|
+
- add support for RestClient.options
|
76
|
+
- fix tests for 1.9.2 (patch provided by Niko Dittmann)
|
77
|
+
- block passing in Resource#[] (patch provided by Niko Dittmann)
|
78
|
+
- cookies set in a response should be kept in a redirect
|
79
|
+
- HEAD requests should process parameters just like GET (patch provided by Rob Eanes)
|
80
|
+
- exception message should never be nil (patch provided by Michael Klett)
|
81
|
+
|
82
|
+
# 1.6.0
|
83
|
+
|
84
|
+
- forgot to include rest-client.rb in the gem
|
85
|
+
- user, password and user-defined headers should survive a redirect
|
86
|
+
- added all missing status codes
|
87
|
+
- added parameter passing for get request using the :param key in header
|
88
|
+
- the warning about the logger when using a string was a bad idea
|
89
|
+
- multipart parameters names should not be escaped
|
90
|
+
- remove the cookie escaping introduced by migrating to CGI cookie parsing in 1.5.1
|
91
|
+
- add a streamed payload type (patch provided by Caleb Land)
|
92
|
+
- Exception#http_body works even when no response
|
93
|
+
|
94
|
+
# 1.5.1
|
95
|
+
|
96
|
+
- only converts headers keys which are Symbols
|
97
|
+
- use CGI for cookie parsing instead of custom code
|
98
|
+
- unescape user and password before using them (patch provided by Lars Gierth)
|
99
|
+
- expand ~ in ~/.restclientrc (patch provided by Mike Fletcher)
|
100
|
+
- ssl verification raise an exception when the ca certificate is incorrect (patch provided by Braintree)
|
101
|
+
|
102
|
+
# 1.5.0
|
103
|
+
|
104
|
+
- 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)
|
105
|
+
- added AbstractResponse.to_i to improve semantic
|
106
|
+
- multipart Payloads ignores the name attribute if it's not set (patch provided by Tekin Suleyman)
|
107
|
+
- correctly takes into account user headers whose keys are strings (path provided by Cyril Rohr)
|
108
|
+
- use binary mode for payload temp file
|
109
|
+
- concatenate cookies with ';'
|
110
|
+
- fixed deeper parameter handling
|
111
|
+
- do not quote the boundary in the Content-Type header (patch provided by W. Andrew Loe III)
|
112
|
+
|
113
|
+
# 1.4.2
|
114
|
+
|
115
|
+
- fixed RestClient.add_before_execution_proc (patch provided by Nicholas Wieland)
|
116
|
+
- fixed error when an exception is raised without a response (patch provided by Caleb Land)
|
117
|
+
|
118
|
+
# 1.4.1
|
119
|
+
|
120
|
+
- fixed parameters managment when using hash
|
121
|
+
|
122
|
+
# 1.4.0
|
123
|
+
|
124
|
+
- 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.
|
125
|
+
- enable repeated parameters RestClient.post 'http://example.com/resource', :param1 => ['one', 'two', 'three'], => :param2 => 'foo' (patch provided by Rodrigo Panachi)
|
126
|
+
- fixed the redirect code concerning relative path and query string combination (patch provided by Kevin Read)
|
127
|
+
- redirection code moved to Response so redirection can be customized using the block syntax
|
128
|
+
- only get and head redirections are now followed by default, as stated in the specification
|
129
|
+
- added RestClient.add_before_execution_proc to hack the http request, like for oauth
|
130
|
+
|
131
|
+
The response change may be breaking in rare cases.
|
132
|
+
|
133
|
+
# 1.3.1
|
134
|
+
|
135
|
+
- added compatibility to enable responses in exception to act like Net::HTTPResponse
|
136
|
+
|
137
|
+
# 1.3.0
|
138
|
+
|
139
|
+
- a block can be used to process a request's result, this enable to handle custom error codes or paththrought (design by Cyril Rohr)
|
140
|
+
- cleaner log API, add a warning for some cases but should be compatible
|
141
|
+
- accept multiple "Set-Cookie" headers, see http://www.ietf.org/rfc/rfc2109.txt (patch provided by Cyril Rohr)
|
142
|
+
- remove "Content-Length" and "Content-Type" headers when following a redirection (patch provided by haarts)
|
143
|
+
- 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
|
144
|
+
- changed "Content-Disposition: multipart/form-data" to "Content-Disposition: form-data" per RFC 2388 (patch provided by Kyle Crawford)
|
145
|
+
|
146
|
+
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.
|
147
|
+
|
148
|
+
# 1.2.0
|
149
|
+
|
150
|
+
- formatting changed from tabs to spaces
|
151
|
+
- logged requests now include generated headers
|
152
|
+
- 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
|
153
|
+
- should be 1.1.1 but renamed to 1.2.0 because 1.1.X versions has already been packaged on Debian
|
154
|
+
|
155
|
+
# 1.1.0
|
156
|
+
|
157
|
+
- new maintainer: Archiloque, the working repo is now at http://github.com/archiloque/rest-client
|
158
|
+
- a mailing list has been created at rest.client@librelist.com and an freenode irc channel #rest-client
|
159
|
+
- François Beausoleil' multipart code from http://github.com/francois/rest-client has been merged
|
160
|
+
- ability to use hash in hash as payload
|
161
|
+
- the mime-type code now rely on the mime-types gem http://mime-types.rubyforge.org/ instead of an internal partial list
|
162
|
+
- 204 response returns a Response instead of nil (patch provided by Elliott Draper)
|
163
|
+
|
164
|
+
All changes exept the last one should be fully compatible with the previous version.
|
165
|
+
|
166
|
+
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.
|
data/lib/rest-client.rb
ADDED
data/lib/rest_client.rb
ADDED
data/lib/restclient.rb
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'zlib'
|
3
|
+
require 'stringio'
|
4
|
+
require 'net/https'
|
5
|
+
|
6
|
+
require File.dirname(__FILE__) + '/restclient/version'
|
7
|
+
require File.dirname(__FILE__) + '/restclient/platform'
|
8
|
+
require File.dirname(__FILE__) + '/restclient/exceptions'
|
9
|
+
require File.dirname(__FILE__) + '/restclient/request'
|
10
|
+
require File.dirname(__FILE__) + '/restclient/abstract_response'
|
11
|
+
require File.dirname(__FILE__) + '/restclient/response'
|
12
|
+
require File.dirname(__FILE__) + '/restclient/raw_response'
|
13
|
+
require File.dirname(__FILE__) + '/restclient/resource'
|
14
|
+
require File.dirname(__FILE__) + '/restclient/payload'
|
15
|
+
require File.dirname(__FILE__) + '/restclient/windows'
|
16
|
+
|
17
|
+
# This module's static methods are the entry point for using the REST client.
|
18
|
+
#
|
19
|
+
# # GET
|
20
|
+
# xml = RestClient.get 'http://example.com/resource'
|
21
|
+
# jpg = RestClient.get 'http://example.com/resource', :accept => 'image/jpg'
|
22
|
+
#
|
23
|
+
# # authentication and SSL
|
24
|
+
# RestClient.get 'https://user:password@example.com/private/resource'
|
25
|
+
#
|
26
|
+
# # POST or PUT with a hash sends parameters as a urlencoded form body
|
27
|
+
# RestClient.post 'http://example.com/resource', :param1 => 'one'
|
28
|
+
#
|
29
|
+
# # nest hash parameters
|
30
|
+
# RestClient.post 'http://example.com/resource', :nested => { :param1 => 'one' }
|
31
|
+
#
|
32
|
+
# # POST and PUT with raw payloads
|
33
|
+
# RestClient.post 'http://example.com/resource', 'the post body', :content_type => 'text/plain'
|
34
|
+
# RestClient.post 'http://example.com/resource.xml', xml_doc
|
35
|
+
# RestClient.put 'http://example.com/resource.pdf', File.read('my.pdf'), :content_type => 'application/pdf'
|
36
|
+
#
|
37
|
+
# # DELETE
|
38
|
+
# RestClient.delete 'http://example.com/resource'
|
39
|
+
#
|
40
|
+
# # retreive the response http code and headers
|
41
|
+
# res = RestClient.get 'http://example.com/some.jpg'
|
42
|
+
# res.code # => 200
|
43
|
+
# res.headers[:content_type] # => 'image/jpg'
|
44
|
+
#
|
45
|
+
# # HEAD
|
46
|
+
# RestClient.head('http://example.com').headers
|
47
|
+
#
|
48
|
+
# To use with a proxy, just set RestClient.proxy to the proper http proxy:
|
49
|
+
#
|
50
|
+
# RestClient.proxy = "http://proxy.example.com/"
|
51
|
+
#
|
52
|
+
# Or inherit the proxy from the environment:
|
53
|
+
#
|
54
|
+
# RestClient.proxy = ENV['http_proxy']
|
55
|
+
#
|
56
|
+
# For live tests of RestClient, try using http://rest-test.heroku.com, which echoes back information about the rest call:
|
57
|
+
#
|
58
|
+
# >> RestClient.put 'http://rest-test.heroku.com/resource', :foo => 'baz'
|
59
|
+
# => "PUT http://rest-test.heroku.com/resource with a 7 byte payload, content type application/x-www-form-urlencoded {\"foo\"=>\"baz\"}"
|
60
|
+
#
|
61
|
+
module RestClient
|
62
|
+
|
63
|
+
def self.get(url, headers={}, &block)
|
64
|
+
Request.execute(:method => :get, :url => url, :headers => headers, &block)
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.post(url, payload, headers={}, &block)
|
68
|
+
Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers, &block)
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.patch(url, payload, headers={}, &block)
|
72
|
+
Request.execute(:method => :patch, :url => url, :payload => payload, :headers => headers, &block)
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.put(url, payload, headers={}, &block)
|
76
|
+
Request.execute(:method => :put, :url => url, :payload => payload, :headers => headers, &block)
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.delete(url, headers={}, &block)
|
80
|
+
Request.execute(:method => :delete, :url => url, :headers => headers, &block)
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.head(url, headers={}, &block)
|
84
|
+
Request.execute(:method => :head, :url => url, :headers => headers, &block)
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.options(url, headers={}, &block)
|
88
|
+
Request.execute(:method => :options, :url => url, :headers => headers, &block)
|
89
|
+
end
|
90
|
+
|
91
|
+
class << self
|
92
|
+
attr_accessor :proxy
|
93
|
+
end
|
94
|
+
|
95
|
+
# Setup the log for RestClient calls.
|
96
|
+
# Value should be a logger but can can be stdout, stderr, or a filename.
|
97
|
+
# You can also configure logging by the environment variable RESTCLIENT_LOG.
|
98
|
+
def self.log= log
|
99
|
+
@@log = create_log log
|
100
|
+
end
|
101
|
+
|
102
|
+
# Create a log that respond to << like a logger
|
103
|
+
# param can be 'stdout', 'stderr', a string (then we will log to that file) or a logger (then we return it)
|
104
|
+
def self.create_log param
|
105
|
+
if param
|
106
|
+
if param.is_a? String
|
107
|
+
if param == 'stdout'
|
108
|
+
stdout_logger = Class.new do
|
109
|
+
def << obj
|
110
|
+
STDOUT.puts obj
|
111
|
+
end
|
112
|
+
end
|
113
|
+
stdout_logger.new
|
114
|
+
elsif param == 'stderr'
|
115
|
+
stderr_logger = Class.new do
|
116
|
+
def << obj
|
117
|
+
STDERR.puts obj
|
118
|
+
end
|
119
|
+
end
|
120
|
+
stderr_logger.new
|
121
|
+
else
|
122
|
+
file_logger = Class.new do
|
123
|
+
attr_writer :target_file
|
124
|
+
|
125
|
+
def << obj
|
126
|
+
File.open(@target_file, 'a') { |f| f.puts obj }
|
127
|
+
end
|
128
|
+
end
|
129
|
+
logger = file_logger.new
|
130
|
+
logger.target_file = param
|
131
|
+
logger
|
132
|
+
end
|
133
|
+
else
|
134
|
+
param
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
@@env_log = create_log ENV['RESTCLIENT_LOG']
|
140
|
+
|
141
|
+
@@log = nil
|
142
|
+
|
143
|
+
def self.log # :nodoc:
|
144
|
+
@@env_log || @@log
|
145
|
+
end
|
146
|
+
|
147
|
+
@@before_execution_procs = []
|
148
|
+
|
149
|
+
# Add a Proc to be called before each request in executed.
|
150
|
+
# The proc parameters will be the http request and the request params.
|
151
|
+
def self.add_before_execution_proc &proc
|
152
|
+
@@before_execution_procs << proc
|
153
|
+
end
|
154
|
+
|
155
|
+
# Reset the procs to be called before each request is executed.
|
156
|
+
def self.reset_before_execution_procs
|
157
|
+
@@before_execution_procs = []
|
158
|
+
end
|
159
|
+
|
160
|
+
def self.before_execution_procs # :nodoc:
|
161
|
+
@@before_execution_procs
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|