Module: HTTPI

Defined in:
lib/httpi.rb,
lib/httpi/dime.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: Dime, DimeRecord, Request, Response

Constant Summary collapse

REQUEST_METHODS =
[:get, :post, :head, :put, :delete]
DEFAULT_LOG_LEVEL =
:warn
VERSION =
"0.9.6"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.log(*messages) ⇒ Object

Logs given messages.



166
167
168
169
# File 'lib/httpi.rb', line 166

def log(*messages)
  level = Symbol === messages.first ? messages.shift : log_level
  logger.send level, messages.join(" ") if log?
end

.log_levelObject

Returns the log level. Defaults to :debug.



161
162
163
# File 'lib/httpi.rb', line 161

def log_level
  @log_level ||= DEFAULT_LOG_LEVEL
end

.loggerObject

Returns the logger. Defaults to an instance of Logger writing to STDOUT.



153
154
155
# File 'lib/httpi.rb', line 153

def logger
  @logger ||= ::Logger.new STDOUT
end

Class Method Details

.adapter=(adapter) ⇒ Object

Shortcut for setting the default adapter to use.



137
138
139
# File 'lib/httpi.rb', line 137

def adapter=(adapter)
  Adapter.use = adapter
end

.delete(request, adapter = nil) ⇒ Object

Executes an HTTP DELETE request.



121
122
123
124
125
126
127
128
# File 'lib/httpi.rb', line 121

def delete(request, adapter = nil)
  request = Request.new :url => request if request.kind_of? String

  with_adapter :delete, 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.



81
82
83
84
85
86
87
88
# File 'lib/httpi.rb', line 81

def get(request, adapter = nil)
  request = Request.new :url => request if request.kind_of? String

  with_adapter :get, 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.



101
102
103
104
105
106
107
108
# File 'lib/httpi.rb', line 101

def head(request, adapter = nil)
  request = Request.new :url => request if request.kind_of? String

  with_adapter :head, request, adapter do |adapter|
    yield adapter.client if block_given?
    adapter.head request
  end
end

.log?Boolean

Returns whether to log HTTP requests. Defaults to true.

Returns:

  • (Boolean)


145
146
147
# File 'lib/httpi.rb', line 145

def log?
  @log != false
end

.post(*args) ⇒ Object

Executes an HTTP POST request.



91
92
93
94
95
96
97
98
# File 'lib/httpi.rb', line 91

def post(*args)
  request, adapter = request_and_adapter_from(args)

  with_adapter :post, request, adapter do |adapter|
    yield adapter.client if block_given?
    adapter.post request
  end
end

.put(*args) ⇒ Object

Executes an HTTP PUT request.



111
112
113
114
115
116
117
118
# File 'lib/httpi.rb', line 111

def put(*args)
  request, adapter = request_and_adapter_from(args)

  with_adapter :put, 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)


131
132
133
134
# File 'lib/httpi.rb', line 131

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

.reset_config!Object

Reset the default config.



172
173
174
175
176
# File 'lib/httpi.rb', line 172

def reset_config!
  @log = nil
  @logger = nil
  @log_level = nil
end