Class: HTTPkit::Client

Inherits:
Object
  • Object
show all
Includes:
HTTPkit::Connection::Status, Support::HandlerManager::Setup
Defined in:
lib/httpkit/client.rb

Defined Under Namespace

Classes: BodyHandler, KeepAliveHandler, MandatoryHandler, TimeoutsHandler

Constant Summary

DoubleRequestError =
Class.new(Error) do
  attr_reader :client, :request

  def initialize(client, request)
    @client, @request = client, request
    super(sprintf('%s %s already has sequence number %d',
                  request.http_method, request.uri, sequence))
  end

  def sequence
    request.sequence(client)
  end
end

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from HTTPkit::Connection::Status

#close, #closed?, #error?, #network_fault?, #timeout?

Constructor Details

- (Client) initialize(config, connection)

Returns a new instance of Client



30
31
32
33
34
35
36
37
# File 'lib/httpkit/client.rb', line 30

def initialize(config, connection)
  @sequence = 0
  @config = config
  @requests = []

  setup_connection(connection)
  setup_handlers
end

Instance Attribute Details

- (Object) config (readonly)

Returns the value of attribute config



28
29
30
# File 'lib/httpkit/client.rb', line 28

def config
  @config
end

Class Method Details

+ (Object) start(config)



24
25
26
# File 'lib/httpkit/client.rb', line 24

def self.start(config)
  Connection::EventMachine.start_client(config, self)
end

Instance Method Details

- (Object) finish(request)



63
64
65
66
67
68
# File 'lib/httpkit/client.rb', line 63

def finish(request)
  @requests.delete_if do |(other)|
    request.sequence(self) == other.sequence(self)
  end
  @handlers.invoke(:finish, request)
end

- (Object) perform(request)



44
45
46
47
48
49
50
51
# File 'lib/httpkit/client.rb', line 44

def perform(request)
  served = Promise.new

  request.sequence(self, @sequence += 1)
  perform!(request, served).resume if validate(request, served)

  served
end

- (Object) receive(response)



53
54
55
56
57
58
59
60
61
# File 'lib/httpkit/client.rb', line 53

def receive(response)
  request, served = find_request

  if request
    request, response = @handlers.invoke(:receive, request, response)
    served.fulfill(response)
    response.closed { finish(request) }
  end
end

- (Object) request(*args)



39
40
41
42
# File 'lib/httpkit/client.rb', line 39

def request(*args)
  request = Request.new(*args)
  perform(request).sync
end

- (Object) teardown(reason)



70
71
72
73
74
75
76
# File 'lib/httpkit/client.rb', line 70

def teardown(reason)
  @requests.each do |_, served|
    served.reject(reason)
    next unless (response = served.value)
    response.reject_closed(reason)
  end
end