forked from suitmedia/kirk
/
client.rb
64 lines (52 loc) · 1.21 KB
/
client.rb
1
2
3
4
5
6
7
8
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
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
require 'kirk'
module Kirk
class Client
require 'kirk/client/group'
require 'kirk/client/response'
require 'kirk/client/request'
require 'kirk/client/exchange'
def self.client
@client ||= new
end
def self.stop
@client.stop if @client
@client = nil
end
def self.group(opts = {}, &blk)
client.group(opts, &blk)
end
%w(request head get post put delete).each do |method|
class_eval <<-RUBY
def self.#{method}(*args, &blk)
g = group { |g| g.#{method}(*args, &blk) }
g.responses.first
end
RUBY
end
def group(opts = {}, &blk)
Group.new(self, opts).tap do |group|
group.start(&blk)
end
end
def initialize(opts = {})
@options = opts
end
def client
@client ||= Jetty::HttpClient.new.tap do |client|
client.set_connector_type(Jetty::HttpClient::CONNECTOR_SELECT_CHANNEL)
client.set_thread_pool(thread_pool) if thread_pool
client.start
end
end
def process(request)
client.send Exchange.build(request)
end
def stop
client.stop
end
private
def thread_pool
@options[:thread_pool]
end
end
end