Module: HTTPI

Defined in:
lib/httpi.rb,
lib/httpi/dime.rb,
lib/httpi/utils.rb,
lib/httpi/cookie.rb,
lib/httpi/logger.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/base.rb,
lib/httpi/adapter/curb.rb,
lib/httpi/adapter/http.rb,
lib/httpi/adapter/rack.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

mostly verbatim from: github.com/rack/rack/blob/main/lib/rack/headers.rb Because this is part of httpi’s public API, its better not to load an external library for it.

Defined Under Namespace

Modules: Adapter, Auth, ConnectionError, QueryBuilder, Utils Classes: Cookie, CookieStore, Dime, DimeRecord, Error, NotImplementedError, NotSupportedError, Request, Response, SSLError, TimeoutError

Constant Summary collapse

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

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.



175
176
177
# File 'lib/httpi.rb', line 175

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

.adapter_client_setup=(block) ⇒ Object



179
180
181
# File 'lib/httpi.rb', line 179

def adapter_client_setup=(block)
  Adapter.client_setup_block = block
end

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

Executes an HTTP DELETE request.



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

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.



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

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.



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

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.



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

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.



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

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

.query_builderObject



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

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

.query_builder=(builder) ⇒ Object



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

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)


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

def request(method, request, adapter = nil, redirects = 0)
  adapter_class = load_adapter(adapter, request)
  
  Adapter.client_setup_block.call(adapter_class.client) if Adapter.client_setup_block
  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