cs-httpi 0.9.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +5 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +66 -0
- data/Gemfile +9 -0
- data/LICENSE +20 -0
- data/README.md +229 -0
- data/Rakefile +18 -0
- data/autotest/discover.rb +1 -0
- data/cs-httpi.gemspec +26 -0
- data/lib/cs-httpi.rb +198 -0
- data/lib/cs-httpi/adapter.rb +67 -0
- data/lib/cs-httpi/adapter/curb.rb +119 -0
- data/lib/cs-httpi/adapter/httpclient.rb +98 -0
- data/lib/cs-httpi/adapter/net_http.rb +115 -0
- data/lib/cs-httpi/auth/config.rb +78 -0
- data/lib/cs-httpi/auth/ssl.rb +91 -0
- data/lib/cs-httpi/dime.rb +56 -0
- data/lib/cs-httpi/request.rb +99 -0
- data/lib/cs-httpi/response.rb +85 -0
- data/lib/cs-httpi/version.rb +5 -0
- data/nbproject/private/private.properties +2 -0
- data/nbproject/private/rake-d.txt +0 -0
- data/nbproject/project.properties +7 -0
- data/nbproject/project.xml +15 -0
- data/spec/cs-httpi/adapter/curb_spec.rb +232 -0
- data/spec/cs-httpi/adapter/httpclient_spec.rb +164 -0
- data/spec/cs-httpi/adapter/net_http_spec.rb +142 -0
- data/spec/cs-httpi/adapter_spec.rb +55 -0
- data/spec/cs-httpi/auth/config_spec.rb +117 -0
- data/spec/cs-httpi/auth/ssl_spec.rb +128 -0
- data/spec/cs-httpi/httpi_spec.rb +284 -0
- data/spec/cs-httpi/request_spec.rb +140 -0
- data/spec/cs-httpi/response_spec.rb +125 -0
- data/spec/fixtures/attachment.gif +0 -0
- data/spec/fixtures/client_cert.pem +16 -0
- data/spec/fixtures/client_key.pem +15 -0
- data/spec/fixtures/xml.gz +0 -0
- data/spec/fixtures/xml.xml +10 -0
- data/spec/fixtures/xml_dime.dime +0 -0
- data/spec/fixtures/xml_dime.xml +1 -0
- data/spec/integration/request_spec.rb +95 -0
- data/spec/integration/server.rb +39 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/support/fixture.rb +27 -0
- data/spec/support/matchers.rb +19 -0
- metadata +158 -0
data/.autotest
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
## 0.9.5 (2011-06-30)
|
2
|
+
|
3
|
+
* Improvement: Moved support for NTLM authentication into a separate gem.
|
4
|
+
Since NTLM support caused quite some problems for people who didn't even
|
5
|
+
need it, I decided to move it into httpi-ntlm until it's stable.
|
6
|
+
|
7
|
+
## 0.9.4 (2011-05-15)
|
8
|
+
|
9
|
+
* Fix: issues [34](https://github.com/rubiii/httpi/issues/34) and
|
10
|
+
[29](https://github.com/rubiii/httpi/issues/29) - replaced the dependency
|
11
|
+
on `ntlm-http` with a dependency on `pyu-ntlm-http` which comes with quite
|
12
|
+
a few bugfixes.
|
13
|
+
|
14
|
+
* Fix: Setting the default adapter did not always load the adapter's client library.
|
15
|
+
|
16
|
+
* Improvement: Added a shortcut method to set the default adapter to use.
|
17
|
+
|
18
|
+
HTTPI.adapter = :net_http
|
19
|
+
|
20
|
+
## 0.9.3 (2011-04-28)
|
21
|
+
|
22
|
+
* Fix: [issue 31](https://github.com/rubiii/httpi/issues/31) missing headers when using httpclient.
|
23
|
+
|
24
|
+
* Fix: [issue 30](https://github.com/rubiii/httpi/issues/30) fix for using SSL with Net::HTTP.
|
25
|
+
|
26
|
+
## 0.9.2 (2011-04-05)
|
27
|
+
|
28
|
+
* Fix: issues [161](https://github.com/rubiii/savon/issues/161) and [165](https://github.com/rubiii/savon/issues/165)
|
29
|
+
reported at [rubiii/savon](https://github.com/rubiii/savon).
|
30
|
+
|
31
|
+
## 0.9.1 (2011-04-04)
|
32
|
+
|
33
|
+
* Fix: [issue 25](https://github.com/rubiii/httpi/issues/22) problem with HTTPI using the Net::HTTP adapter [hakanensari].
|
34
|
+
|
35
|
+
## 0.9.0 (2011-03-08)
|
36
|
+
|
37
|
+
* Feature: improved the adapter loading process ([d4a091](https://github.com/rubiii/httpi/commit/d4a091)) [rubiii].
|
38
|
+
|
39
|
+
Instead of using HTTPClient as the default and falling back to NetHTTP, the loading process now does the following:
|
40
|
+
|
41
|
+
1. Check if either HTTPClient, Curb or NetHTTP are already defined.
|
42
|
+
If any one of those is defined, use it.
|
43
|
+
|
44
|
+
2. Try to require HTTPClient, Curb and NetHTTP at last.
|
45
|
+
If any one of those can be required, use it.
|
46
|
+
|
47
|
+
Of course you can still manually specify the adapter to use.
|
48
|
+
|
49
|
+
* Fix: [issue 22](https://github.com/rubiii/httpi/issues/22) argument error on logging adapter warning [rubiii].
|
50
|
+
|
51
|
+
* Fix: [issue 23](https://github.com/rubiii/httpi/issues/23) the HTTPI.log method now works as expected [rubiii].
|
52
|
+
|
53
|
+
## 0.8.0 (2011-03-07)
|
54
|
+
|
55
|
+
* Feature: added support for NTLM authentication ([96ceb1](https://github.com/rubiii/httpi/commit/96ceb1)) [MattHall].
|
56
|
+
|
57
|
+
You should now be able to use NTLM authentication by specifying your credentials via `HTTPI::Auth::Config#ntlm`:
|
58
|
+
|
59
|
+
request = HTTPI::Request.new
|
60
|
+
request.auth.ntlm "username", "password"
|
61
|
+
|
62
|
+
* Improvement: changed the default log level to :warn ([d01591](https://github.com/rubiii/httpi/commit/d01591))
|
63
|
+
and log at appropriate levels ([21ee1b](https://github.com/rubiii/httpi/commit/21ee1b)) [ichverstehe].
|
64
|
+
|
65
|
+
* Fix: [issue 18](https://github.com/rubiii/httpi/issues/18) don't mask exceptions in decoded_gzip_body
|
66
|
+
([f3811b](https://github.com/rubiii/httpi/commit/f3811b)) [fj].
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Daniel Harrington
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,229 @@
|
|
1
|
+
CS-HTTPI
|
2
|
+
=====
|
3
|
+
|
4
|
+
CS_HTTPI provides a common interface for Ruby HTTP libraries. This gem is a fork of httpi gem (https://github.com/rubiii/httpi)
|
5
|
+
|
6
|
+
[Bugs](http://github.com/concretesolutions/cs-httpi/issues) | [RDoc](http://rubydoc.info/gems/cs-httpi/frames)
|
7
|
+
|
8
|
+
Installation
|
9
|
+
------------
|
10
|
+
|
11
|
+
cs-httpi is available through [Rubygems](http://rubygems.org/gems/cs-httpi) and can be installed via:
|
12
|
+
|
13
|
+
```
|
14
|
+
$ gem install cs-httpi
|
15
|
+
```
|
16
|
+
|
17
|
+
|
18
|
+
Getting started
|
19
|
+
---------------
|
20
|
+
|
21
|
+
In order to provide a common interface, HTTPI provides the `HTTPI::Request` object for you to
|
22
|
+
configure your request. Here's a very simple GET request.
|
23
|
+
|
24
|
+
``` ruby
|
25
|
+
request = HTTPI::Request.new("http://example.com")
|
26
|
+
HTTPI.get request
|
27
|
+
```
|
28
|
+
|
29
|
+
To execute a POST request, you may want to specify a payload.
|
30
|
+
|
31
|
+
``` ruby
|
32
|
+
request = HTTPI::Request.new
|
33
|
+
request.url = "ht 78tp://post.example.com"
|
34
|
+
request.body = "some data"
|
35
|
+
|
36
|
+
HTTPI.post request
|
37
|
+
```
|
38
|
+
|
39
|
+
|
40
|
+
HTTPI
|
41
|
+
-----
|
42
|
+
|
43
|
+
The `HTTPI` module uses one of the available adapters to execute HTTP requests.
|
44
|
+
|
45
|
+
#### GET
|
46
|
+
|
47
|
+
``` ruby
|
48
|
+
HTTPI.get(request, adapter = nil)
|
49
|
+
HTTPI.get(url, adapter = nil)
|
50
|
+
```
|
51
|
+
|
52
|
+
#### POST
|
53
|
+
|
54
|
+
``` ruby
|
55
|
+
HTTPI.post(request, adapter = nil)
|
56
|
+
HTTPI.post(url, body, adapter = nil)
|
57
|
+
```
|
58
|
+
|
59
|
+
#### HEAD
|
60
|
+
|
61
|
+
``` ruby
|
62
|
+
HTTPI.head(request, adapter = nil)
|
63
|
+
HTTPI.head(url, adapter = nil)
|
64
|
+
```
|
65
|
+
|
66
|
+
#### PUT
|
67
|
+
|
68
|
+
``` ruby
|
69
|
+
HTTPI.put(request, adapter = nil)
|
70
|
+
HTTPI.put(url, body, adapter = nil)
|
71
|
+
```
|
72
|
+
|
73
|
+
#### DELETE
|
74
|
+
|
75
|
+
``` ruby
|
76
|
+
HTTPI.delete(request, adapter = nil)
|
77
|
+
HTTPI.delete(url, adapter = nil)
|
78
|
+
```
|
79
|
+
|
80
|
+
#### Notice
|
81
|
+
|
82
|
+
* You can specify the adapter to use per request
|
83
|
+
* And request methods always return an `HTTPI::Response`
|
84
|
+
|
85
|
+
#### More control
|
86
|
+
|
87
|
+
If you need more control over the request, you can access the HTTP client instance
|
88
|
+
represented by your adapter in a block:
|
89
|
+
|
90
|
+
``` ruby
|
91
|
+
HTTPI.post request do |http|
|
92
|
+
http.use_ssl = true # Curb example
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
|
97
|
+
HTTPI::Adapter
|
98
|
+
--------------
|
99
|
+
|
100
|
+
HTTPI uses adapters to support multiple HTTP libraries.
|
101
|
+
It currently contains adapters for:
|
102
|
+
|
103
|
+
* [httpclient](http://rubygems.org/gems/httpclient) ~> 2.1.5
|
104
|
+
* [curb](http://rubygems.org/gems/curb) ~> 0.7.8
|
105
|
+
* [net/http](http://ruby-doc.org/stdlib/libdoc/net/http/rdoc)
|
106
|
+
|
107
|
+
You can manually specify the adapter to use via:
|
108
|
+
|
109
|
+
``` ruby
|
110
|
+
HTTPI.adapter = :curb # or one of [:httpclient, :net_http]
|
111
|
+
```
|
112
|
+
|
113
|
+
If you don't specify which adapter to use, HTTPI try to load HTTPClient, then Curb and finally NetHTTP.
|
114
|
+
|
115
|
+
#### Notice
|
116
|
+
|
117
|
+
HTTPI does not force you to install any of these libraries. If you'd like to use on of the more advanced
|
118
|
+
libraries (HTTPClient or Curb), you have to make sure they're in your LOAD_PATH. HTTPI will then load the
|
119
|
+
library when executing HTTP requests.
|
120
|
+
|
121
|
+
|
122
|
+
HTTPI::Request
|
123
|
+
--------------
|
124
|
+
|
125
|
+
#### URL
|
126
|
+
|
127
|
+
``` ruby
|
128
|
+
request.url = "http://example.com"
|
129
|
+
request.url # => #<URI::HTTP:0x101c1ab18 URL:http://example.com>
|
130
|
+
```
|
131
|
+
|
132
|
+
#### Proxy
|
133
|
+
|
134
|
+
``` ruby
|
135
|
+
request.proxy = "http://example.com"
|
136
|
+
request.proxy # => #<URI::HTTP:0x101c1ab18 URL:http://example.com>
|
137
|
+
```
|
138
|
+
|
139
|
+
#### HeadersHTTPI
|
140
|
+
|
141
|
+
|
142
|
+
``` ruby
|
143
|
+
request.headers["Accept-Charset"] = "utf-8"
|
144
|
+
request.headers = { "Accept-Charset" => "utf-8" }
|
145
|
+
request.headers # => { "Accept-Charset" => "utf-8" }
|
146
|
+
```
|
147
|
+
|
148
|
+
#### Body
|
149
|
+
|
150
|
+
``` ruby
|
151
|
+
request.body = "some data"
|
152
|
+
request.body # => "some data"
|
153
|
+
```
|
154
|
+
|
155
|
+
#### Open timeout
|
156
|
+
|
157
|
+
``` ruby
|
158
|
+
request.open_timeout = 30 # sec
|
159
|
+
```
|
160
|
+
|
161
|
+
#### Read timeout
|
162
|
+
|
163
|
+
``` ruby
|
164
|
+
request.read_timeout = 30 # sec
|
165
|
+
```
|
166
|
+
|
167
|
+
|
168
|
+
HTTPI::Auth
|
169
|
+
-----------
|
170
|
+
|
171
|
+
`HTTPI::Auth` supports HTTP basic and digest authentication.
|
172
|
+
|
173
|
+
``` ruby
|
174
|
+
request.auth.basic("username", "password") # HTTP basic auth credentials
|
175
|
+
request.auth.digest("username", "password") # HTTP digest auth credentials
|
176
|
+
```
|
177
|
+
|
178
|
+
For experimental NTLM authentication, please use the [httpi-ntlm](rubygems.org/gems/httpi-ntml)
|
179
|
+
gem and provide feedback.
|
180
|
+
|
181
|
+
``` ruby
|
182
|
+
request.auth.ntlm("username", "password") # NTLM auth credentials
|
183
|
+
```
|
184
|
+
|
185
|
+
HTTPI::Auth::SSL
|
186
|
+
----------------
|
187
|
+
|
188
|
+
`HTTPI::Auth::SSL` manages SSL client authentication.
|
189
|
+
|
190
|
+
``` ruby
|
191
|
+
request.auth.ssl.cert_key_file = "client_key.pem" # the private key file to use
|
192
|
+
request.auth.ssl.cert_key_password = "C3rtP@ssw0rd" # the key file's password
|
193
|
+
request.auth.ssl.cert_file = "client_cert.pem" # the certificate file to use
|
194
|
+
request.auth.ssl.ca_cert_file = "ca_cert.pem" # the ca certificate file to use
|
195
|
+
request.auth.ssl.verify_mode = :none # or one of [:peer, :fail_if_no_peer_cert, :client_once]
|
196
|
+
```
|
197
|
+
|
198
|
+
|
199
|
+
HTTPI::Response
|
200
|
+
---------------
|
201
|
+
|
202
|
+
Every request returns an `HTTPI::Response`. It contains the response code, headers and body.
|
203
|
+
|
204
|
+
``` ruby
|
205
|
+
response = HTTPI.get request
|
206
|
+
|
207
|
+
response.code # => 200
|
208
|
+
response.headers # => { "Content-Encoding" => "gzip" }
|
209
|
+
response.body # => "<!DOCTYPE HTML PUBLIC ...>"
|
210
|
+
```
|
211
|
+
|
212
|
+
The `response.body` handles gzipped and [DIME](http://en.wikipedia.org/wiki/Direct_Internet_Message_Encapsulation) encoded responses.
|
213
|
+
|
214
|
+
#### TODO
|
215
|
+
|
216
|
+
* Return the original `HTTPI::Request` for debugging purposes
|
217
|
+
* Return the time it took to execute the request
|
218
|
+
|
219
|
+
|
220
|
+
Logging
|
221
|
+
-------
|
222
|
+
|
223
|
+
HTTPI by default logs each HTTP request to STDOUT using a log level of :debug.
|
224
|
+
|
225
|
+
``` ruby
|
226
|
+
HTTPI.log = false # disable logging
|
227
|
+
HTTPI.logger = MyLogger # change the logger
|
228
|
+
HTTPI.log_level = :info # change the log level
|
229
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new do |t|
|
7
|
+
t.pattern = "spec/cs-httpi/**/*_spec.rb"
|
8
|
+
end
|
9
|
+
|
10
|
+
desc "Run RSpec integration examples"
|
11
|
+
RSpec::Core::RakeTask.new "spec_integration" do |t|
|
12
|
+
t.pattern = "spec/integration/*_spec.rb"
|
13
|
+
end
|
14
|
+
|
15
|
+
task :default => :spec
|
16
|
+
|
17
|
+
desc "Run RSpec code and integration examples"
|
18
|
+
task :ci => [:spec, :spec_integration]
|
@@ -0,0 +1 @@
|
|
1
|
+
Autotest.add_discovery { "rspec2" }
|
data/cs-httpi.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
lib = File.expand_path("../lib/", __FILE__)
|
2
|
+
$:.unshift lib unless $:.include?(lib)
|
3
|
+
|
4
|
+
require "cs-httpi/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "cs-httpi"
|
8
|
+
s.version = HTTPI::VERSION
|
9
|
+
s.authors = ["Celestino Gomes", "Renato Elias", "Lenon Marcel", "Madson Cardoso", "Luca Bastos", "Marcelo Linhares"]
|
10
|
+
s.email = %w[tinorj@gmail.com renato.elias@gmail.com lenon.marcel@gmail.com madsonmac@gmail.com lucabastos@gmail.com marcelolinhares@gmail.com]
|
11
|
+
s.homepage = "http://github.com/concretesolutions/#{s.name}"
|
12
|
+
s.summary = "Interface for Ruby HTTP libraries"
|
13
|
+
s.description = "HTTPI provides a common interface for Ruby HTTP libraries. This gem is a fork of httpi gem (https://github.com/rubiii/httpi)"
|
14
|
+
|
15
|
+
s.rubyforge_project = s.name
|
16
|
+
|
17
|
+
s.add_dependency "rack"
|
18
|
+
|
19
|
+
s.add_development_dependency "rspec"
|
20
|
+
s.add_development_dependency "autotest"
|
21
|
+
s.add_development_dependency "mocha"
|
22
|
+
s.add_development_dependency "webmock"
|
23
|
+
|
24
|
+
s.files = `git ls-files`.split("\n")
|
25
|
+
s.require_path = "lib"
|
26
|
+
end
|
data/lib/cs-httpi.rb
ADDED
@@ -0,0 +1,198 @@
|
|
1
|
+
require "logger"
|
2
|
+
require "cs-httpi/version"
|
3
|
+
require "cs-httpi/request"
|
4
|
+
require "cs-httpi/adapter"
|
5
|
+
|
6
|
+
# = HTTPI
|
7
|
+
#
|
8
|
+
# Executes HTTP requests using a predefined adapter.
|
9
|
+
# All request methods accept an <tt>HTTPI::Request</tt> and an optional adapter.
|
10
|
+
# They may also offer shortcut methods for executing basic requests.
|
11
|
+
# Also they all return an <tt>HTTPI::Response</tt>.
|
12
|
+
#
|
13
|
+
# == GET
|
14
|
+
#
|
15
|
+
# request = HTTPI::Request.new :url => "http://example.com"
|
16
|
+
# HTTPI.get request, :httpclient
|
17
|
+
#
|
18
|
+
# === Shortcuts
|
19
|
+
#
|
20
|
+
# HTTPI.get "http://example.com", :curb
|
21
|
+
#
|
22
|
+
# == POST
|
23
|
+
#
|
24
|
+
# request = HTTPI::Request.new
|
25
|
+
# request.url = "http://example.com"
|
26
|
+
# request.body = "<some>xml</some>"
|
27
|
+
#
|
28
|
+
# HTTPI.post request, :httpclient
|
29
|
+
#
|
30
|
+
# === Shortcuts
|
31
|
+
#
|
32
|
+
# HTTPI.post "http://example.com", "<some>xml</some>", :curb
|
33
|
+
#
|
34
|
+
# == HEAD
|
35
|
+
#
|
36
|
+
# request = HTTPI::Request.new :url => "http://example.com"
|
37
|
+
# HTTPI.head request, :httpclient
|
38
|
+
#
|
39
|
+
# === Shortcuts
|
40
|
+
#
|
41
|
+
# HTTPI.head "http://example.com", :curb
|
42
|
+
#
|
43
|
+
# == PUT
|
44
|
+
#
|
45
|
+
# request = HTTPI::Request.new
|
46
|
+
# request.url = "http://example.com"
|
47
|
+
# request.body = "<some>xml</some>"
|
48
|
+
#
|
49
|
+
# HTTPI.put request, :httpclient
|
50
|
+
#
|
51
|
+
# === Shortcuts
|
52
|
+
#
|
53
|
+
# HTTPI.put "http://example.com", "<some>xml</some>", :curb
|
54
|
+
#
|
55
|
+
# == DELETE
|
56
|
+
#
|
57
|
+
# request = HTTPI::Request.new :url => "http://example.com"
|
58
|
+
# HTTPI.delete request, :httpclient
|
59
|
+
#
|
60
|
+
# === Shortcuts
|
61
|
+
#
|
62
|
+
# HTTPI.delete "http://example.com", :curb
|
63
|
+
#
|
64
|
+
# == More control
|
65
|
+
#
|
66
|
+
# If you need more control over your request, you can access the HTTP client
|
67
|
+
# instance represented by your adapter in a block.
|
68
|
+
#
|
69
|
+
# HTTPI.get request do |http|
|
70
|
+
# http.follow_redirect_count = 3 # HTTPClient example
|
71
|
+
# end
|
72
|
+
module HTTPI
|
73
|
+
|
74
|
+
REQUEST_METHODS = [:get, :post, :head, :put, :delete]
|
75
|
+
|
76
|
+
DEFAULT_LOG_LEVEL = :warn
|
77
|
+
|
78
|
+
class << self
|
79
|
+
|
80
|
+
# Executes an HTTP GET request.
|
81
|
+
def get(request, adapter = nil)
|
82
|
+
request = Request.new :url => request if request.kind_of? String
|
83
|
+
|
84
|
+
with_adapter :get, request, adapter do |adapter|
|
85
|
+
yield adapter.client if block_given?
|
86
|
+
adapter.get request
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# Executes an HTTP POST request.
|
91
|
+
def post(*args)
|
92
|
+
request, adapter = request_and_adapter_from(args)
|
93
|
+
|
94
|
+
with_adapter :post, request, adapter do |adapter|
|
95
|
+
yield adapter.client if block_given?
|
96
|
+
adapter.post request
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Executes an HTTP HEAD request.
|
101
|
+
def head(request, adapter = nil)
|
102
|
+
request = Request.new :url => request if request.kind_of? String
|
103
|
+
|
104
|
+
with_adapter :head, request, adapter do |adapter|
|
105
|
+
yield adapter.client if block_given?
|
106
|
+
adapter.head request
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
# Executes an HTTP PUT request.
|
111
|
+
def put(*args)
|
112
|
+
request, adapter = request_and_adapter_from(args)
|
113
|
+
|
114
|
+
with_adapter :put, request, adapter do |adapter|
|
115
|
+
yield adapter.client if block_given?
|
116
|
+
adapter.put request
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# Executes an HTTP DELETE request.
|
121
|
+
def delete(request, adapter = nil)
|
122
|
+
request = Request.new :url => request if request.kind_of? String
|
123
|
+
|
124
|
+
with_adapter :delete, request, adapter do |adapter|
|
125
|
+
yield adapter.client if block_given?
|
126
|
+
adapter.delete request
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
# Executes an HTTP request for the given +method+.
|
131
|
+
def request(method, request, adapter = nil)
|
132
|
+
raise ArgumentError, "Invalid request method: #{method}" unless REQUEST_METHODS.include? method
|
133
|
+
send method, request, adapter
|
134
|
+
end
|
135
|
+
|
136
|
+
# Shortcut for setting the default adapter to use.
|
137
|
+
def adapter=(adapter)
|
138
|
+
Adapter.use = adapter
|
139
|
+
end
|
140
|
+
|
141
|
+
# Sets whether to log HTTP requests.
|
142
|
+
attr_writer :log
|
143
|
+
|
144
|
+
# Returns whether to log HTTP requests. Defaults to +true+.
|
145
|
+
def log?
|
146
|
+
@log != false
|
147
|
+
end
|
148
|
+
|
149
|
+
# Sets the logger to use.
|
150
|
+
attr_writer :logger
|
151
|
+
|
152
|
+
# Returns the logger. Defaults to an instance of +Logger+ writing to STDOUT.
|
153
|
+
def logger
|
154
|
+
@logger ||= ::Logger.new STDOUT
|
155
|
+
end
|
156
|
+
|
157
|
+
# Sets the log level.
|
158
|
+
attr_writer :log_level
|
159
|
+
|
160
|
+
# Returns the log level. Defaults to :debug.
|
161
|
+
def log_level
|
162
|
+
@log_level ||= DEFAULT_LOG_LEVEL
|
163
|
+
end
|
164
|
+
|
165
|
+
# Logs given +messages+.
|
166
|
+
def log(*messages)
|
167
|
+
level = Symbol === messages.first ? messages.shift : log_level
|
168
|
+
logger.send level, messages.join(" ") if log?
|
169
|
+
end
|
170
|
+
|
171
|
+
# Reset the default config.
|
172
|
+
def reset_config!
|
173
|
+
@log = nil
|
174
|
+
@logger = nil
|
175
|
+
@log_level = nil
|
176
|
+
end
|
177
|
+
|
178
|
+
private
|
179
|
+
|
180
|
+
# Checks whether +args+ contains of an <tt>HTTPI::Request</tt> or a URL
|
181
|
+
# and a request body plus an optional adapter and returns an Array with
|
182
|
+
# an <tt>HTTPI::Request</tt> and (if given) an adapter.
|
183
|
+
def request_and_adapter_from(args)
|
184
|
+
return args if args[0].kind_of? Request
|
185
|
+
[Request.new(:url => args[0], :body => args[1]), args[2]]
|
186
|
+
end
|
187
|
+
|
188
|
+
# Expects a request +method+, a +request+ and an +adapter+ (defaults to
|
189
|
+
# <tt>Adapter.use</tt>) and yields an instance of the adapter to a given block.
|
190
|
+
def with_adapter(method, request, adapter)
|
191
|
+
adapter, adapter_class = Adapter.load adapter
|
192
|
+
|
193
|
+
log :debug, "HTTPI executes HTTP #{method.to_s.upcase} using the #{adapter} adapter"
|
194
|
+
yield adapter_class.new(request)
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
end
|