httpi 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -10
- data/Gemfile.lock +6 -0
- data/README.md +138 -0
- data/Rakefile +2 -1
- data/lib/httpi.rb +97 -1
- data/lib/httpi/adapter.rb +8 -1
- data/lib/httpi/adapter/curb.rb +14 -7
- data/lib/httpi/adapter/httpclient.rb +12 -5
- data/lib/httpi/request.rb +4 -0
- data/lib/httpi/response.rb +4 -0
- data/lib/httpi/version.rb +1 -1
- data/spec/httpi/{client_spec.rb → httpi_spec.rb} +18 -2
- metadata +8 -9
- data/README.rdoc +0 -114
- data/lib/httpi/client.rb +0 -70
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
HTTPI
|
2
|
+
=====
|
3
|
+
|
4
|
+
HTTPI provides a common interface for Ruby HTTP libraries.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
|
9
|
+
The gem is available through [Rubygems](http://rubygems.org/gems/httpi) and can be installed via:
|
10
|
+
|
11
|
+
$ gem install httpi
|
12
|
+
|
13
|
+
Some examples
|
14
|
+
-------------
|
15
|
+
|
16
|
+
Let's create the most basic request object and execute a GET request:
|
17
|
+
|
18
|
+
request = HTTPI::Request.new :url => "http://example.com"
|
19
|
+
HTTPI.get request
|
20
|
+
|
21
|
+
A POST request with a request object:
|
22
|
+
|
23
|
+
request = HTTPI::Request.new
|
24
|
+
request.url = "http://post.example.com"
|
25
|
+
request.body = "send me"
|
26
|
+
|
27
|
+
HTTPI.post request
|
28
|
+
|
29
|
+
And a GET request using HTTP basic auth and the Curb adapter:
|
30
|
+
|
31
|
+
request = HTTPI::Request.new
|
32
|
+
request.url = "http://auth.example.com"
|
33
|
+
request.basic_auth "username", "password"
|
34
|
+
|
35
|
+
HTTPI.get request, :curb
|
36
|
+
|
37
|
+
HTTPI also comes with some shortcuts. This executes a GET request:
|
38
|
+
|
39
|
+
HTTPI.get "http://example.com"
|
40
|
+
|
41
|
+
And here's a POST:
|
42
|
+
|
43
|
+
HTTPI.post "http://example.com", "<some>xml</some>"
|
44
|
+
|
45
|
+
HTTPI
|
46
|
+
-------------
|
47
|
+
|
48
|
+
The `HTTPI` module uses one of the available adapters to execute HTTP requests.
|
49
|
+
It currently supports GET and POST requests:
|
50
|
+
|
51
|
+
### GET
|
52
|
+
|
53
|
+
.get(request, adapter = nil)
|
54
|
+
.get(url, adapter = nil)
|
55
|
+
|
56
|
+
### POST
|
57
|
+
|
58
|
+
.post(request, adapter = nil)
|
59
|
+
.post(url, body, adapter = nil)
|
60
|
+
|
61
|
+
### Notice
|
62
|
+
|
63
|
+
* You can specify the adapter to use per request
|
64
|
+
* And request methods always return an `HTTPI::Response`
|
65
|
+
|
66
|
+
### More control
|
67
|
+
|
68
|
+
If you need more control over the request, you can access the HTTP client instance represented
|
69
|
+
by your adapter in a block:
|
70
|
+
|
71
|
+
HTTPI.post request do |http|
|
72
|
+
http.use_ssl = true # Curb example
|
73
|
+
end
|
74
|
+
|
75
|
+
### TODO
|
76
|
+
|
77
|
+
* Add support for HEAD, PUT and DELETE requests
|
78
|
+
|
79
|
+
HTTPI::Request
|
80
|
+
--------------
|
81
|
+
|
82
|
+
The `HTTPI::Request` serves as a common denominator of options that HTTPI adapters need to support.
|
83
|
+
It represents an HTTP request and lets you customize various settings through these accessors:
|
84
|
+
|
85
|
+
#url # the URL to access
|
86
|
+
#proxy # the proxy server to use
|
87
|
+
#headers # a Hash of HTTP headers
|
88
|
+
#body # the HTTP request body
|
89
|
+
#open_timeout # the open timeout (sec)
|
90
|
+
#read_timeout # the read timeout (sec)
|
91
|
+
|
92
|
+
It also contains methods for setting up authentication:
|
93
|
+
|
94
|
+
#basic_auth(username, password) # HTTP basic auth credentials
|
95
|
+
|
96
|
+
### TODO
|
97
|
+
|
98
|
+
* Add support for HTTP digest authentication
|
99
|
+
* Add support for SSL client authentication
|
100
|
+
|
101
|
+
HTTPI::Adapter
|
102
|
+
--------------
|
103
|
+
|
104
|
+
HTTPI uses adapters to support multiple HTTP libraries.
|
105
|
+
It currently contains adapters for:
|
106
|
+
|
107
|
+
* [httpclient](http://rubygems.org/gems/httpclient) ~> 2.1.5
|
108
|
+
* [curb](http://rubygems.org/gems/curb) ~> 0.7.8
|
109
|
+
|
110
|
+
By default, HTTPI uses the `HTTPClient`. But changing the default is fairly easy:
|
111
|
+
|
112
|
+
HTTPI::Adapter.use = :curb
|
113
|
+
|
114
|
+
You can find a list of supported adapters via:
|
115
|
+
|
116
|
+
HTTPI::Adapter.adapters # returns a Hash of supported adapters
|
117
|
+
|
118
|
+
HTTPI::Response
|
119
|
+
---------------
|
120
|
+
|
121
|
+
As mentioned before, every request method return an `HTTPI::Response`.
|
122
|
+
It contains the response code, headers and body.
|
123
|
+
|
124
|
+
response = HTTPI.get request
|
125
|
+
|
126
|
+
response.code # => 200
|
127
|
+
response.headers # => { "Content-Encoding" => "gzip" }
|
128
|
+
response.body # => "<!DOCTYPE HTML PUBLIC ..."
|
129
|
+
|
130
|
+
### TODO
|
131
|
+
|
132
|
+
* Return the original `HTTPI::Request` for debugging purposes
|
133
|
+
* Return the time it took to execute the request
|
134
|
+
|
135
|
+
Participate
|
136
|
+
-----------
|
137
|
+
|
138
|
+
We appreciate any help and feedback, so please get in touch!
|
data/Rakefile
CHANGED
@@ -4,7 +4,7 @@ begin
|
|
4
4
|
require "yard"
|
5
5
|
|
6
6
|
YARD::Rake::YardocTask.new do |t|
|
7
|
-
t.files = ["README.
|
7
|
+
t.files = ["README.md", "lib/**/*.rb"]
|
8
8
|
end
|
9
9
|
rescue LoadError
|
10
10
|
desc message = %{"gem install yard" to generate documentation}
|
@@ -17,6 +17,7 @@ begin
|
|
17
17
|
MetricFu::Configuration.run do |c|
|
18
18
|
c.metrics = [:churn, :flog, :flay, :reek, :roodi, :saikuro] # :rcov seems to be broken
|
19
19
|
c.graphs = [:flog, :flay, :reek, :roodi]
|
20
|
+
c.flay = { :dirs_to_flay => ["lib"], :minimum_score => 20 }
|
20
21
|
c.rcov[:rcov_opts] << "-Ilib -Ispec"
|
21
22
|
end
|
22
23
|
rescue LoadError
|
data/lib/httpi.rb
CHANGED
@@ -1,2 +1,98 @@
|
|
1
1
|
require "httpi/version"
|
2
|
-
require "httpi/
|
2
|
+
require "httpi/request"
|
3
|
+
require "httpi/adapter"
|
4
|
+
|
5
|
+
# = HTTPI
|
6
|
+
#
|
7
|
+
# Executes HTTP requests using a predefined adapter.
|
8
|
+
module HTTPI
|
9
|
+
class << self
|
10
|
+
|
11
|
+
# Executes an HTTP GET request and returns an <tt>HTTPI::Response</tt>.
|
12
|
+
#
|
13
|
+
# ==== Example
|
14
|
+
#
|
15
|
+
# Accepts an <tt>HTTPI::Request</tt> and an optional adapter:
|
16
|
+
#
|
17
|
+
# request = HTTPI::Request.new :url => "http://example.com"
|
18
|
+
# HTTPI.get request, :httpclient
|
19
|
+
#
|
20
|
+
# ==== Shortcut
|
21
|
+
#
|
22
|
+
# You can also just pass a URL and an optional adapter if you don't
|
23
|
+
# need to configure the request:
|
24
|
+
#
|
25
|
+
# HTTPI.get "http://example.com", :curb
|
26
|
+
#
|
27
|
+
# ==== More control
|
28
|
+
#
|
29
|
+
# If you need more control over the request, you can access the HTTP
|
30
|
+
# client instance represented by your adapter in a block.
|
31
|
+
#
|
32
|
+
# HTTPI.get request do |http|
|
33
|
+
# http.follow_redirect_count = 3 # HTTPClient example
|
34
|
+
# end
|
35
|
+
def get(request, adapter = nil)
|
36
|
+
request = Request.new :url => request if request.kind_of? String
|
37
|
+
|
38
|
+
with adapter do |adapter|
|
39
|
+
yield adapter.client if block_given?
|
40
|
+
adapter.get request
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Executes an HTTP POST request and returns an <tt>HTTPI::Response</tt>.
|
45
|
+
#
|
46
|
+
# ==== Example
|
47
|
+
#
|
48
|
+
# Accepts an <tt>HTTPI::Request</tt> and an optional adapter:
|
49
|
+
#
|
50
|
+
# request = HTTPI::Request.new
|
51
|
+
# request.url = "http://example.com"
|
52
|
+
# request.body = "<some>xml</some>"
|
53
|
+
#
|
54
|
+
# HTTPI.post request, :httpclient
|
55
|
+
#
|
56
|
+
# ==== Shortcut
|
57
|
+
#
|
58
|
+
# You can also just pass a URL, a request body and an optional adapter
|
59
|
+
# if you don't need to configure the request:
|
60
|
+
#
|
61
|
+
# HTTPI.post "http://example.com", "<some>xml</some>", :curb
|
62
|
+
#
|
63
|
+
# ==== More control
|
64
|
+
#
|
65
|
+
# If you need more control over the request, you can access the HTTP
|
66
|
+
# client instance represented by your adapter in a block.
|
67
|
+
#
|
68
|
+
# HTTPI.post request do |http|
|
69
|
+
# http.use_ssl = true # Curb example
|
70
|
+
# end
|
71
|
+
def post(*args)
|
72
|
+
request, adapter = extract_post_args(args)
|
73
|
+
|
74
|
+
with adapter do |adapter|
|
75
|
+
yield adapter.client if block_given?
|
76
|
+
adapter.post request
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
# Checks whether +args+ contains of an <tt>HTTPI::Request</tt> or a URL
|
83
|
+
# and a request body plus an optional adapter and returns an Array with
|
84
|
+
# an <tt>HTTPI::Request</tt> and (if given) an adapter.
|
85
|
+
def extract_post_args(args)
|
86
|
+
return args if args[0].kind_of? Request
|
87
|
+
[Request.new(:url => args[0], :body => args[1]), args[2]]
|
88
|
+
end
|
89
|
+
|
90
|
+
# Accepts an +adapter+ (defaults to <tt>Adapter.use</tt>) and yields a
|
91
|
+
# new instance of the adapter to a given block.
|
92
|
+
def with(adapter)
|
93
|
+
adapter ||= Adapter.use
|
94
|
+
yield Adapter.find(adapter).new
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
data/lib/httpi/adapter.rb
CHANGED
@@ -2,9 +2,16 @@ require "httpi/adapter/httpclient"
|
|
2
2
|
require "httpi/adapter/curb"
|
3
3
|
|
4
4
|
module HTTPI
|
5
|
+
|
6
|
+
# = HTTPI::Adapter
|
7
|
+
#
|
8
|
+
# Manages the adapter classes. Currently supports:
|
9
|
+
#
|
10
|
+
# * httpclient
|
11
|
+
# * curb
|
5
12
|
module Adapter
|
6
13
|
|
7
|
-
# The
|
14
|
+
# The default adapter.
|
8
15
|
DEFAULT = :httpclient
|
9
16
|
|
10
17
|
# Returns the adapter to use. Defaults to <tt>HTTPI::Adapter::DEFAULT</tt>.
|
data/lib/httpi/adapter/curb.rb
CHANGED
@@ -2,12 +2,21 @@ require "httpi/response"
|
|
2
2
|
|
3
3
|
module HTTPI
|
4
4
|
module Adapter
|
5
|
+
|
6
|
+
# = HTTPI::Adapter::Curb
|
7
|
+
#
|
8
|
+
# Adapter for the Curb client.
|
9
|
+
# http://rubygems.org/gems/curb
|
5
10
|
class Curb
|
6
11
|
|
7
12
|
def initialize
|
8
13
|
require "curb"
|
9
14
|
end
|
10
15
|
|
16
|
+
def client
|
17
|
+
@client ||= Curl::Easy.new
|
18
|
+
end
|
19
|
+
|
11
20
|
def get(request)
|
12
21
|
get_request(request) { |client| client.http_get }
|
13
22
|
end
|
@@ -19,27 +28,25 @@ module HTTPI
|
|
19
28
|
private
|
20
29
|
|
21
30
|
def get_request(request)
|
22
|
-
|
31
|
+
setup_client request
|
23
32
|
yield client
|
24
33
|
respond_with client
|
25
34
|
end
|
26
35
|
|
27
36
|
def post_request(request)
|
28
|
-
request.url = request.url.
|
29
|
-
request.url.gsub!('?wsdl', '') if request.url =~ /\?wsdl$/
|
37
|
+
request.url.query = nil if request.url.query == "wsdl"
|
30
38
|
|
31
|
-
|
39
|
+
setup_client request
|
32
40
|
yield client, request.body
|
33
41
|
respond_with client
|
34
42
|
end
|
35
43
|
|
36
|
-
def
|
37
|
-
client =
|
44
|
+
def setup_client(request)
|
45
|
+
client.url = request.url.to_s
|
38
46
|
client.timeout = request.read_timeout
|
39
47
|
client.connect_timeout = request.open_timeout
|
40
48
|
client.headers = request.headers
|
41
49
|
client.verbose = false
|
42
|
-
client
|
43
50
|
end
|
44
51
|
|
45
52
|
def respond_with(client)
|
@@ -2,12 +2,21 @@ require "httpi/response"
|
|
2
2
|
|
3
3
|
module HTTPI
|
4
4
|
module Adapter
|
5
|
+
|
6
|
+
# = HTTPI::Adapter::HTTPClient
|
7
|
+
#
|
8
|
+
# Adapter for the HTTPClient client.
|
9
|
+
# http://rubygems.org/gems/httpclient
|
5
10
|
class HTTPClient
|
6
11
|
|
7
12
|
def initialize
|
8
13
|
require "httpclient"
|
9
14
|
end
|
10
15
|
|
16
|
+
def client
|
17
|
+
@client ||= ::HTTPClient.new
|
18
|
+
end
|
19
|
+
|
11
20
|
def get(request)
|
12
21
|
get_request request do |client, url, headers|
|
13
22
|
client.get url, nil, headers
|
@@ -23,21 +32,19 @@ module HTTPI
|
|
23
32
|
private
|
24
33
|
|
25
34
|
def get_request(request)
|
26
|
-
|
35
|
+
setup_client request
|
27
36
|
respond_with yield(client, request.url, request.headers)
|
28
37
|
end
|
29
38
|
|
30
39
|
def post_request(request)
|
31
|
-
|
40
|
+
setup_client request
|
32
41
|
respond_with yield(client, request.url, request.headers, request.body)
|
33
42
|
end
|
34
43
|
|
35
|
-
def
|
36
|
-
client = ::HTTPClient.new
|
44
|
+
def setup_client(request)
|
37
45
|
client.proxy = request.proxy if request.proxy
|
38
46
|
client.connect_timeout = request.open_timeout
|
39
47
|
client.receive_timeout = request.read_timeout
|
40
|
-
client
|
41
48
|
end
|
42
49
|
|
43
50
|
def respond_with(response)
|
data/lib/httpi/request.rb
CHANGED
data/lib/httpi/response.rb
CHANGED
data/lib/httpi/version.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
require "httpi"
|
3
3
|
|
4
|
-
describe HTTPI
|
5
|
-
let(:client) { HTTPI
|
4
|
+
describe HTTPI do
|
5
|
+
let(:client) { HTTPI }
|
6
6
|
let(:default_adapter) { HTTPI::Adapter.find HTTPI::Adapter.use }
|
7
7
|
let(:curb) { HTTPI::Adapter.find :curb }
|
8
8
|
|
@@ -43,6 +43,14 @@ describe HTTPI::Client do
|
|
43
43
|
end
|
44
44
|
|
45
45
|
describe ".get" do
|
46
|
+
context "(with a block)" do
|
47
|
+
it "should yield the HTTP client instance used for the request" do
|
48
|
+
client.get "http://example.com" do |http|
|
49
|
+
http.should be_an(HTTPClient)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
46
54
|
it "should raise an ArgumentError in case of an invalid adapter" do
|
47
55
|
lambda { client.get HTTPI::Request.new, :invalid }.should raise_error(ArgumentError)
|
48
56
|
end
|
@@ -91,6 +99,14 @@ describe HTTPI::Client do
|
|
91
99
|
end
|
92
100
|
|
93
101
|
describe ".post" do
|
102
|
+
context "(with a block)" do
|
103
|
+
it "should yield the HTTP client instance used for the request" do
|
104
|
+
client.get "http://example.com", :curb do |http|
|
105
|
+
http.should be_a(Curl::Easy)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
94
110
|
it "should raise an ArgumentError in case of an invalid adapter" do
|
95
111
|
lambda { client.post HTTPI::Request.new, :invalid }.should raise_error(ArgumentError)
|
96
112
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: httpi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 3
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Daniel Harrington
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-09-
|
19
|
+
date: 2010-09-24 00:00:00 +02:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -97,12 +97,12 @@ files:
|
|
97
97
|
- Gemfile
|
98
98
|
- Gemfile.lock
|
99
99
|
- Rakefile
|
100
|
-
- README.
|
100
|
+
- README.md
|
101
|
+
- .rspec
|
101
102
|
- autotest/discover.rb
|
102
103
|
- lib/httpi/adapter/curb.rb
|
103
104
|
- lib/httpi/adapter/httpclient.rb
|
104
105
|
- lib/httpi/adapter.rb
|
105
|
-
- lib/httpi/client.rb
|
106
106
|
- lib/httpi/request.rb
|
107
107
|
- lib/httpi/response.rb
|
108
108
|
- lib/httpi/version.rb
|
@@ -112,13 +112,12 @@ files:
|
|
112
112
|
- spec/httpi/adapter/curb_spec.rb
|
113
113
|
- spec/httpi/adapter/httpclient_spec.rb
|
114
114
|
- spec/httpi/adapter_spec.rb
|
115
|
-
- spec/httpi/
|
115
|
+
- spec/httpi/httpi_spec.rb
|
116
116
|
- spec/httpi/request_spec.rb
|
117
117
|
- spec/httpi/response_spec.rb
|
118
118
|
- spec/spec_helper.rb
|
119
119
|
- spec/support/fixture.rb
|
120
120
|
- spec/support/matchers.rb
|
121
|
-
- .rspec
|
122
121
|
has_rdoc: true
|
123
122
|
homepage: http://github.com/rubiii/httpi
|
124
123
|
licenses: []
|
@@ -152,6 +151,6 @@ rubyforge_project: httpi
|
|
152
151
|
rubygems_version: 1.3.7
|
153
152
|
signing_key:
|
154
153
|
specification_version: 3
|
155
|
-
summary:
|
154
|
+
summary: Interface for Ruby HTTP libraries
|
156
155
|
test_files: []
|
157
156
|
|
data/README.rdoc
DELETED
@@ -1,114 +0,0 @@
|
|
1
|
-
= HTTPI
|
2
|
-
|
3
|
-
HTTPI provides a common interface for Ruby HTTP libraries.
|
4
|
-
|
5
|
-
== Installation
|
6
|
-
|
7
|
-
The gem is available through {Rubygems}[http://rubygems.org/gems/httpi] and can be installed via:
|
8
|
-
|
9
|
-
$ gem install httpi
|
10
|
-
|
11
|
-
== Basic examples
|
12
|
-
|
13
|
-
Let's create the most basic request object and execute a GET request:
|
14
|
-
|
15
|
-
request = HTTPI::Request.new :url => "http://example.com"
|
16
|
-
HTTPI::Client.get request
|
17
|
-
|
18
|
-
And a POST request with a request object:
|
19
|
-
|
20
|
-
request = HTTPI::Request.new
|
21
|
-
request.url = "http://post.example.com"
|
22
|
-
request.body = "send me"
|
23
|
-
|
24
|
-
HTTPI::Client.post request
|
25
|
-
|
26
|
-
Or a GET request using HTTP basic auth and the Curb adapter:
|
27
|
-
|
28
|
-
request = HTTPI::Request.new
|
29
|
-
request.url = "http://auth.example.com"
|
30
|
-
request.basic_auth "username", "password"
|
31
|
-
|
32
|
-
HTTPI::Client.get request, :curb
|
33
|
-
|
34
|
-
HTTPI also comes with some shortcuts. This executes a GET request:
|
35
|
-
|
36
|
-
HTTPI::Client.get "http://example.com"
|
37
|
-
|
38
|
-
And here's a POST:
|
39
|
-
|
40
|
-
HTTPI::Client.post "http://example.com", "<some>xml</some>"
|
41
|
-
|
42
|
-
== HTTPI::Request
|
43
|
-
|
44
|
-
The HTTPI::Request serves as a common denominator of options that HTTPI adapters need to support. It represents an HTTP request and lets you customize various settings:
|
45
|
-
|
46
|
-
* [url] the URL to access
|
47
|
-
* [proxy] the proxy server to use
|
48
|
-
* [headers] a Hash of HTTP headers
|
49
|
-
* [body] the HTTP request body
|
50
|
-
* [open_timeout] the open timeout (sec)
|
51
|
-
* [read_timeout] the read timeout (sec)
|
52
|
-
|
53
|
-
It also contains methods for setting up authentication:
|
54
|
-
|
55
|
-
* [basic_auth] HTTP basic auth credentials
|
56
|
-
|
57
|
-
==== TODO:
|
58
|
-
|
59
|
-
* Add support for HTTP digest authentication
|
60
|
-
* Add support for SSL client authentication
|
61
|
-
|
62
|
-
== HTTPI::Client
|
63
|
-
|
64
|
-
The HTTPI::Client uses one of the available adapters to execute HTTP requests. It currently supports GET and POST requests:
|
65
|
-
|
66
|
-
=== GET
|
67
|
-
|
68
|
-
* get(request, adapter = nil)
|
69
|
-
* get(url, adapter = nil)
|
70
|
-
|
71
|
-
=== POST
|
72
|
-
|
73
|
-
* post(request, adapter = nil)
|
74
|
-
* post(url, body, adapter = nil)
|
75
|
-
|
76
|
-
You can specify the adapter to use per request. Request methods always returns an HTTPI::Response.
|
77
|
-
|
78
|
-
==== TODO:
|
79
|
-
|
80
|
-
* Add support for HEAD, PUT and DELETE requests
|
81
|
-
|
82
|
-
== HTTPI::Adapter
|
83
|
-
|
84
|
-
HTTPI uses adapters to support multiple HTTP libraries. It currently contains adapters for:
|
85
|
-
|
86
|
-
* {httpclient}[http://rubygems.org/gems/httpclient] ~> 2.1.5
|
87
|
-
* {curb}[http://rubygems.org/gems/curb] ~> 0.7.8
|
88
|
-
|
89
|
-
By default, HTTPI uses the HTTPClient. But changing the default is fairly easy:
|
90
|
-
|
91
|
-
HTTPI::Adapter.use = :curb
|
92
|
-
|
93
|
-
You can find a list of supported adapters via:
|
94
|
-
|
95
|
-
HTTPI::Adapter.adapters # returns a Hash of supported adapters
|
96
|
-
|
97
|
-
== HTTPI::Response
|
98
|
-
|
99
|
-
As mentioned before, every request method return an HTTPI::Response. It contains the response code, headers and body.
|
100
|
-
|
101
|
-
response = HTTPI::Client.get request
|
102
|
-
|
103
|
-
response.code # => 200
|
104
|
-
response.headers # => { "Content-Encoding" => "gzip" }
|
105
|
-
response.body # => "<!DOCTYPE HTML PUBLIC ..."
|
106
|
-
|
107
|
-
==== TODO
|
108
|
-
|
109
|
-
* Return the original HTTPI::Request for debugging purposes
|
110
|
-
* Return the time it took to execute the request
|
111
|
-
|
112
|
-
== Participate
|
113
|
-
|
114
|
-
We appreciate any help and feedback, so please get in touch!
|
data/lib/httpi/client.rb
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
require "httpi/request"
|
2
|
-
require "httpi/adapter"
|
3
|
-
|
4
|
-
module HTTPI
|
5
|
-
class Client
|
6
|
-
class << self
|
7
|
-
|
8
|
-
# Executes an HTTP GET request and returns an <tt>HTTPI::Response</tt>.
|
9
|
-
#
|
10
|
-
# ==== Example
|
11
|
-
#
|
12
|
-
# Accepts an <tt>HTTPI::Request</tt> and an optional adapter:
|
13
|
-
#
|
14
|
-
# request = HTTPI::Request.new :url => "http://example.com"
|
15
|
-
# HTTPI::Client.get request, :httpclient
|
16
|
-
#
|
17
|
-
# ==== Shortcut
|
18
|
-
#
|
19
|
-
# You can also just pass a URL and an optional adapter if you don't
|
20
|
-
# need to configure the request:
|
21
|
-
#
|
22
|
-
# HTTPI::Client.get "http://example.com", :curb
|
23
|
-
def get(request, adapter = nil)
|
24
|
-
request = Request.new :url => request if request.kind_of? String
|
25
|
-
find_adapter(adapter).get request
|
26
|
-
end
|
27
|
-
|
28
|
-
# Executes an HTTP POST request and returns an <tt>HTTPI::Response</tt>.
|
29
|
-
#
|
30
|
-
# ==== Example
|
31
|
-
#
|
32
|
-
# Accepts an <tt>HTTPI::Request</tt> and an optional adapter:
|
33
|
-
#
|
34
|
-
# request = HTTPI::Request.new
|
35
|
-
# request.url = "http://example.com"
|
36
|
-
# request.body = "<some>xml</some>"
|
37
|
-
#
|
38
|
-
# HTTPI::Client.post request, :httpclient
|
39
|
-
#
|
40
|
-
# ==== Shortcut
|
41
|
-
#
|
42
|
-
# You can also just pass a URL, a request body and an optional adapter
|
43
|
-
# if you don't need to configure the request:
|
44
|
-
#
|
45
|
-
# HTTPI::Client.post "http://example.com", "<some>xml</some>", :curb
|
46
|
-
def post(*args)
|
47
|
-
request, adapter = extract_post_args(args)
|
48
|
-
find_adapter(adapter).post request
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
# Checks whether +args+ contains of an <tt>HTTPI::Request</tt> or a URL
|
54
|
-
# and a request body plus an optional adapter and returns an Array with
|
55
|
-
# an <tt>HTTPI::Request</tt> and (if given) an adapter.
|
56
|
-
def extract_post_args(args)
|
57
|
-
return args if args.first.kind_of? Request
|
58
|
-
[Request.new(:url => args[0], :body => args[1]), args[2]]
|
59
|
-
end
|
60
|
-
|
61
|
-
# Accepts an +adapter+ (defaults to <tt>Adapter.use</tt>) and returns
|
62
|
-
# a new instance of the adapter to use.
|
63
|
-
def find_adapter(adapter)
|
64
|
-
adapter ||= Adapter.use
|
65
|
-
Adapter.find(adapter).new
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|