Class: GiantClient::CurbAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/giant_client/curb_adapter.rb

Constant Summary collapse

CRLF =
/\r\n/
HEADER_SPLIT =
/:\s*/

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#delete, #encode_query, #get, #head, #normalize_header, #normalize_header_hash, #post, #prepend_question_mark, #put, #stringify_query, #url_from_opts

Instance Method Details

#normalize_response(response) ⇒ Object



36
37
38
39
40
41
# File 'lib/giant_client/curb_adapter.rb', line 36

def normalize_response(response)
  status_code = response.response_code
  headers = parse_out_headers(response.header_str)
  body = response.body_str
  Response.new(status_code, headers, body)
end

#parse_out_headers(header_string) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/giant_client/curb_adapter.rb', line 43

def parse_out_headers(header_string)
  headers = {}
  pairs = header_string.split(CRLF)
  pairs.shift
  pairs.each do |pair|
    header, value = *pair.split(HEADER_SPLIT, 2)
    header = normalize_header(header)
    headers[header] = value
  end
  headers
end

#request(method, opts) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/giant_client/curb_adapter.rb', line 9

def request(method, opts)

  if BODYLESS_METHODS.include?(method)
    raise GiantClient::Error::NotImplemented unless opts[:body] == ''
  end

  url = url_from_opts(opts)

  if method == :post
    post_body = opts[:body]
  elsif method == :put
    put_data = opts[:body]
  end

  begin
    response = Curl.http( method.upcase, url, post_body, put_data ) do |curl|
      curl.headers = opts[:headers]
      curl.timeout = opts[:timeout]
      curl.connect_timeout = opts[:timeout]
    end
  rescue Curl::Err::TimeoutError
    raise GiantClient::Error::Timeout, "the request timed out (timeout: #{opts[:timeout]}"
  end

  normalize_response(response)
end