Module: Spectre::Http

Defined in:
lib/spectre/http.rb,
lib/spectre/http/keystone.rb,
lib/spectre/http/basic_auth.rb

Defined Under Namespace

Modules: BasicAuth, Keystone Classes: HttpError, SpectreHttpHeader, SpectreHttpRequest, SpectreHttpResponse

Constant Summary collapse

DEFAULT_HTTP_CONFIG =
{
  'method' => 'GET',
  'path' => '',
  'host' => nil,
  'port' => 80,
  'scheme' => 'http',
  'use_ssl' => false,
  'cert' => nil,
  'headers' => nil,
  'query' => nil,
  'content_type' => nil,
  'timeout' => 180,
  'retries' => 0,
}
@@modules =
[]
@@http_cfg =
{}
@@response =
nil
@@request =
nil
@@secure_keys =
[]

Class Method Summary collapse

Class Method Details

.http(name, secure: false, &block) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/spectre/http.rb', line 181

def http name, secure: false, &block
  req = DEFAULT_HTTP_CONFIG.clone

  if @@http_cfg.key? name
    req.deep_merge! @@http_cfg[name].deep_clone
    raise HttpError.new("No `base_url' set for HTTP client '#{name}'. Check your HTTP config in your environment.") unless req['base_url']
  else
    req['base_url'] = name
  end

  req['use_ssl'] = secure unless secure.nil?

  SpectreHttpRequest.new(req)._evaluate(&block) if block_given?

  invoke(req)
end

.https(name, &block) ⇒ Object



177
178
179
# File 'lib/spectre/http.rb', line 177

def https name, &block
  http(name, secure: true, &block)
end

.register(mod) ⇒ Object



210
211
212
213
214
# File 'lib/spectre/http.rb', line 210

def register mod
  raise 'Module must not be nil' unless mod

  @@modules << mod
end

.requestObject



198
199
200
201
202
# File 'lib/spectre/http.rb', line 198

def request
  raise 'No request has been invoked yet' unless @@request

  @@request
end

.responseObject



204
205
206
207
208
# File 'lib/spectre/http.rb', line 204

def response
  raise 'There is no response. No request has been invoked yet.' unless @@response

  @@response
end