Class: Spec::Client::Http::Driver::CurlDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/client/http/driver/curl_driver.rb

Instance Method Summary collapse

Constructor Details

#initializeCurlDriver

Returns a new instance of CurlDriver.



22
23
# File 'lib/spec/client/http/driver/curl_driver.rb', line 22

def initialize()
end

Instance Method Details

#execute(url, method, headers, params) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/spec/client/http/driver/curl_driver.rb', line 25

def execute(url, method, headers, params)
  # Workaround for potential bug in curl
  request_url = url
  encoded_params = url_encode_params(params)

  if method == "GET"
    request_url += "?#{encoded_params}"
  end

  response_headers = {}
  response_body = ""

  curl = Curl::Easy.new(request_url)
  curl.headers = headers

  curl.on_header do |header|
    size = header.size; header.chomp!
    name, value = header.split(/\: /)
    response_headers[name] ||= []
    response_headers[name] << value
    size
  end

  body_content = ""
  curl.on_body do |body|
    body_content += body
    body.size
  end

  case method.upcase
  when "GET"
    curl.http_get
  when "POST"
    curl.http_post(encoded_params)
  end

  response = Response.new(url)
  response.code = curl.response_code
  response.headers = response_headers
  response.body = body_content
  response
end

#url_encode_params(params) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/spec/client/http/driver/curl_driver.rb', line 13

def url_encode_params(params)
  return nil if params.empty?

  params.inject([]) do |query, (name, value)|
    # TODO: come up with a better way to deal with true and false parameters
    query << [name,value].collect {|s| s.nil? ? "" : s==false ? "0" : s==true ? "1" : CGI.escape(s.to_s)}.join('=')
  end.join("&")
end