Module: HTTPI

Defined in:
lib/httpi.rb,
lib/httpi/dime.rb,
lib/httpi/logger.rb,
lib/httpi/cookie.rb,
lib/httpi/version.rb,
lib/httpi/request.rb,
lib/httpi/adapter.rb,
lib/httpi/auth/ssl.rb,
lib/httpi/response.rb,
lib/httpi/auth/config.rb,
lib/httpi/adapter/rack.rb,
lib/httpi/adapter/http.rb,
lib/httpi/adapter/curb.rb,
lib/httpi/adapter/base.rb,
lib/httpi/cookie_store.rb,
lib/httpi/adapter/excon.rb,
lib/httpi/query_builder.rb,
lib/httpi/adapter/em_http.rb,
lib/httpi/adapter/net_http.rb,
lib/httpi/adapter/httpclient.rb,
lib/httpi/adapter/net_http_persistent.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("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("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("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, ConnectionError, QueryBuilder Classes: Cookie, CookieStore, Dime, DimeRecord, Error, NotImplementedError, NotSupportedError, Request, Response, SSLError, TimeoutError

Constant Summary

REQUEST_METHODS =
[:get, :post, :head, :put, :delete]
DEFAULT_LOG_LEVEL =
:debug
VERSION =
'2.4.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.log(message) ⇒ Object

Logs a given message.



32
33
34
# File 'lib/httpi/logger.rb', line 32

def log(message)
  logger.send(log_level, message) if log?
end

.log_levelObject

Returns the log level. Defaults to :debug.



27
28
29
# File 'lib/httpi/logger.rb', line 27

def log_level
  @log_level ||= DEFAULT_LOG_LEVEL
end

.loggerObject

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



19
20
21
# File 'lib/httpi/logger.rb', line 19

def logger
  @logger ||= ::Logger.new($stdout)
end

Class Method Details

.adapter=(adapter) ⇒ Object

Shortcut for setting the default adapter to use.



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

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

.delete(request, adapter = nil, &block) ⇒ Object

Executes an HTTP DELETE request.



149
150
151
152
# File 'lib/httpi.rb', line 149

def delete(request, adapter = nil, &block)
  request = Request.new(request) if request.kind_of? String
  request(:delete, request, adapter, &block)
end

.get(request, adapter = nil, &block) ⇒ Object

Executes an HTTP GET request.



125
126
127
128
# File 'lib/httpi.rb', line 125

def get(request, adapter = nil, &block)
  request = Request.new(request) if request.kind_of? String
  request(:get, request, adapter, &block)
end

.head(request, adapter = nil, &block) ⇒ Object

Executes an HTTP HEAD request.



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

def head(request, adapter = nil, &block)
  request = Request.new(request) if request.kind_of? String
  request(:head, request, adapter, &block)
end

.log?Boolean

Returns whether to log HTTP requests. Defaults to true.

Returns:

  • (Boolean)


11
12
13
# File 'lib/httpi/logger.rb', line 11

def log?
  @log != false
end

.post(*args, &block) ⇒ Object

Executes an HTTP POST request.



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

def post(*args, &block)
  request, adapter = request_and_adapter_from(args)
  request(:post, request, adapter, &block)
end

.put(*args, &block) ⇒ Object

Executes an HTTP PUT request.



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

def put(*args, &block)
  request, adapter = request_and_adapter_from(args)
  request(:put, request, adapter, &block)
end

.query_builderObject



105
106
107
# File 'lib/httpi.rb', line 105

def query_builder
  @query_builder ||= HTTPI::QueryBuilder::Flat
end

.query_builder=(builder) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/httpi.rb', line 109

def query_builder=(builder)
  if builder.is_a?(Symbol)
    builder_name = builder.to_s.capitalize
    begin
      builder = HTTPI::QueryBuilder.const_get(builder_name)
    rescue NameError
      raise ArgumentError, "Invalid builder. Available builders are: [:flat, :nested]"
    end
  end
  unless builder.respond_to?(:build)
    raise ArgumentError, "Query builder object should respond to build method"
  end
  @query_builder = builder
end

.request(method, request, adapter = nil, redirects = 0) {|adapter_class.client| ... } ⇒ Object

Executes an HTTP request for the given method.

Yields:

  • (adapter_class.client)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/httpi.rb', line 155

def request(method, request, adapter = nil, redirects = 0)
  adapter_class = load_adapter(adapter, request)

  yield adapter_class.client if block_given?
  log_request(method, request, Adapter.identify(adapter_class.class))

  response = adapter_class.request(method)

  if response && HTTPI::Response::RedirectResponseCodes.member?(response.code) && request.follow_redirect? && redirects < request.redirect_limit
    request.url = URI.join(request.url, response.headers['location'])
    log("Following redirect: '#{request.url}'.")
    return request(method, request, adapter, redirects + 1)
  end

  response
end

.reset_config!Object

Reset the default config.



37
38
39
40
41
# File 'lib/httpi/logger.rb', line 37

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