Class: HTTPI::Adapter::Curb

Inherits:
Base
  • Object
show all
Defined in:
lib/httpi/adapter/curb.rb

Overview

HTTPI::Adapter::Curb

Adapter for the Curb client. rubygems.org/gems/curb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

register

Constructor Details

#initialize(request) ⇒ Curb

Returns a new instance of Curb.


15
16
17
18
# File 'lib/httpi/adapter/curb.rb', line 15

def initialize(request)
  @request = request
  @client = Curl::Easy.new
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client


20
21
22
# File 'lib/httpi/adapter/curb.rb', line 20

def client
  @client
end

Instance Method Details

#request(method) ⇒ Object


22
23
24
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
# File 'lib/httpi/adapter/curb.rb', line 22

def request(method)
  unless REQUEST_METHODS.include? method
    raise NotSupportedError, "Curb does not support custom HTTP methods"
  end

  arguments = ["http_#{method}"]
  if [:put, :post].include? method
    arguments << (@request.body || "")
  end

  if @request.on_body
    client.on_body do |data|
      @request.on_body.call(data)
      # curb requires you to return the length of the data read from the block.
      # It allows you to abort the connection by returning a smaller value
      data.length
    end
  end

  do_request { |client| client.send(*arguments) }
rescue Curl::Err::SSLCACertificateError
  raise SSLError
rescue Curl::Err::SSLPeerCertificateError
  raise SSLError
rescue Curl::Err::ConnectionFailedError  # connection refused
  $!.extend ConnectionError
  raise
end