Class: HttpMimic::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/http_mimic/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, url, options = {}, config = nil) ⇒ Request

Returns a new instance of Request.



9
10
11
12
13
14
# File 'lib/http_mimic/request.rb', line 9

def initialize(method, url, options = {}, config = nil)
  @method  = method
  @url     = url
  @options = options.dup
  @config  = config || HttpMimic.configuration
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/http_mimic/request.rb', line 7

def config
  @config
end

#methodObject (readonly)

Returns the value of attribute method.



7
8
9
# File 'lib/http_mimic/request.rb', line 7

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/http_mimic/request.rb', line 7

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/http_mimic/request.rb', line 7

def url
  @url
end

Instance Method Details

#performObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/http_mimic/request.rb', line 16

def perform
  builder = CommandBuilder.new(method, url, options, config)
  binary, args, stdin_data, final_url = builder.build

  full_command = [binary] + args

  log_debug("Executing HttpMimic command: #{full_command.join(' ')}")
  log_debug("Stdin data: #{stdin_data}") if stdin_data

  stdout, stderr, status = execute_open3(full_command, stdin_data)

  log_debug("Curl exit status: #{status.exitstatus}")
  log_debug("Curl stderr: #{stderr}") unless stderr.empty?

  response = ResponseParser.new(
    stdout,
    exit_status: status,
    stderr: stderr,
    command: full_command,
    request_url: final_url
  ).parse

  handle_errors(status, stderr, full_command, response)

  response
end