Module: HTTPI

Defined in:
lib/httpi.rb,
lib/httpi/adapter.rb,
lib/httpi/request.rb,
lib/httpi/version.rb,
lib/httpi/auth/ssl.rb,
lib/httpi/response.rb,
lib/httpi/auth/config.rb,
lib/httpi/adapter/curb.rb,
lib/httpi/adapter/net_http.rb,
lib/httpi/adapter/httpclient.rb

Overview

HTTPI

Executes HTTP requests using a predefined adapter. All request methods accept an HTTPI::Request and an optional adapter. They may also offer shortcut methods for executing basic requests. Also they all return an HTTPI::Response.

GET

request = HTTPI::Request.new :url => "http://example.com"
HTTPI.get request, :httpclient

Shortcuts

HTTPI.get "http://example.com", :curb

POST

request = HTTPI::Request.new
request.url = "http://example.com"
request.body = "<some>xml</some>"

HTTPI.post request, :httpclient

Shortcuts

HTTPI.post "http://example.com", "<some>xml</some>", :curb

HEAD

request = HTTPI::Request.new :url => "http://example.com"
HTTPI.head request, :httpclient

Shortcuts

HTTPI.head "http://example.com", :curb

PUT

request = HTTPI::Request.new
request.url = "http://example.com"
request.body = "<some>xml</some>"

HTTPI.put request, :httpclient

Shortcuts

HTTPI.put "http://example.com", "<some>xml</some>", :curb

DELETE

request = HTTPI::Request.new :url => "http://example.com"
HTTPI.delete request, :httpclient

Shortcuts

HTTPI.delete "http://example.com", :curb

More control

If you need more control over your request, you can access the HTTP client instance represented by your adapter in a block.

HTTPI.get request do |http|
  http.follow_redirect_count = 3  # HTTPClient example
end

Defined Under Namespace

Modules: Adapter, Auth Classes: Request, Response

Constant Summary collapse

REQUEST_METHODS =
[:get, :post, :head, :put, :delete]
VERSION =
"0.6.1"

Class Method Summary collapse

Class Method Details

.delete(request, adapter = nil) ⇒ Object

Executes an HTTP DELETE request.



118
119
120
121
122
123
124
125
# File 'lib/httpi.rb', line 118

def delete(request, adapter = nil)
  request = Request.new :url => request if request.kind_of? String
  
  with request, adapter do |adapter|
    yield adapter.client if block_given?
    adapter.delete request
  end
end

.get(request, adapter = nil) ⇒ Object

Executes an HTTP GET request.



78
79
80
81
82
83
84
85
# File 'lib/httpi.rb', line 78

def get(request, adapter = nil)
  request = Request.new :url => request if request.kind_of? String
  
  with request, adapter do |adapter|
    yield adapter.client if block_given?
    adapter.get request
  end
end

.head(request, adapter = nil) ⇒ Object

Executes an HTTP HEAD request.



98
99
100
101
102
103
104
105
# File 'lib/httpi.rb', line 98

def head(request, adapter = nil)
  request = Request.new :url => request if request.kind_of? String
  
  with request, adapter do |adapter|
    yield adapter.client if block_given?
    adapter.head request
  end
end

.post(*args) ⇒ Object

Executes an HTTP POST request.



88
89
90
91
92
93
94
95
# File 'lib/httpi.rb', line 88

def post(*args)
  request, adapter = request_and_adapter_from(args)
  
  with request, adapter do |adapter|
    yield adapter.client if block_given?
    adapter.post request
  end
end

.put(*args) ⇒ Object

Executes an HTTP PUT request.



108
109
110
111
112
113
114
115
# File 'lib/httpi.rb', line 108

def put(*args)
  request, adapter = request_and_adapter_from(args)
  
  with request, adapter do |adapter|
    yield adapter.client if block_given?
    adapter.put request
  end
end

.request(method, request, adapter = nil) ⇒ Object

Executes an HTTP request for the given method.

Raises:

  • (ArgumentError)


128
129
130
131
# File 'lib/httpi.rb', line 128

def request(method, request, adapter = nil)
  raise ArgumentError, "Invalid request method: #{method}" unless REQUEST_METHODS.include? method
  send method, request, adapter
end