Class: CurlBackend

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

Instance Method Summary collapse

Constructor Details

#initialize(prepared_request, options = {}) ⇒ CurlBackend

Returns a new instance of CurlBackend.



3
4
5
6
# File 'lib/curl_backend.rb', line 3

def initialize(prepared_request, options={})
  @request = prepared_request
  @curl_options = parse_curl_options(options)
end

Instance Method Details

#executeObject



8
9
10
11
12
13
14
# File 'lib/curl_backend.rb', line 8

def execute
  header_string = @request.headers.map {|key, val| "-H '#{key}: #{val}'"}.join ' '
  data_string = @request.data ? "-d '#{@request.data}'" : ""

  curl_command = "curl -X #{@request.method.upcase} #{header_string} #{data_string} #{@curl_options}  #{@request.uri}"
  system curl_command
end

#parse_curl_options(options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/curl_backend.rb', line 16

def parse_curl_options(options)
  opts = ''

  if options.has_key? 'verbose'
    opts += '--verbose '
  end

  if options.has_key? 'head'
    opts += ' --head '
  end

  opts
end