Class: Spectre::Http::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/spectre/http.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, logger) ⇒ Client

Returns a new instance of Client.



218
219
220
221
222
223
# File 'lib/spectre/http.rb', line 218

def initialize config, logger
  @config = config['http']
  @logger = logger
  @debug = config['debug'] || false
  @openapi_cache = {}
end

Instance Method Details

#http(name, secure: false) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/spectre/http.rb', line 229

def http(name, secure: false, &)
  req = Marshal.load(Marshal.dump(DEFAULT_HTTP_CONFIG))

  if @config.key? name
    deep_merge(req, Marshal.load(Marshal.dump(@config[name])))

    unless req['base_url']
      raise SpectreHttpError, "No `base_url' set for HTTP client '#{name}'. " \
                              'Check your HTTP config in your environment.'
    end
  else
    req['base_url'] = name
  end

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

  SpectreHttpRequest.new(req).instance_eval(&) if block_given?

  invoke(req)
end

#https(name) ⇒ Object



225
226
227
# File 'lib/spectre/http.rb', line 225

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

#requestObject



250
251
252
253
254
255
256
# File 'lib/spectre/http.rb', line 250

def request
  req = Thread.current.thread_variable_get(:request)

  raise 'No request has been invoked yet' unless req

  req
end

#responseObject



258
259
260
261
262
263
264
# File 'lib/spectre/http.rb', line 258

def response
  res = Thread.current.thread_variable_get(:response)

  raise 'No response has been received yet' unless res

  res
end