Module: NxtHttpClient::ClientDsl

Included in:
Client
Defined in:
lib/nxt_http_client/client_dsl.rb

Instance Method Summary collapse

Instance Method Details

#after_fire(&block) ⇒ Object



28
29
30
# File 'lib/nxt_http_client/client_dsl.rb', line 28

def after_fire(&block)
  callbacks.register(:after, block)
end

#around_fire(&block) ⇒ Object



32
33
34
# File 'lib/nxt_http_client/client_dsl.rb', line 32

def around_fire(&block)
  callbacks.register(:around, block)
end

#before_fire(&block) ⇒ Object



24
25
26
# File 'lib/nxt_http_client/client_dsl.rb', line 24

def before_fire(&block)
  callbacks.register(:before, block)
end

#callbacksObject



40
41
42
# File 'lib/nxt_http_client/client_dsl.rb', line 40

def callbacks
  @callbacks ||= dup_option_from_ancestor(:@callbacks) { FireCallbacks.new }
end

#clear_fire_callbacks(*kinds) ⇒ Object



20
21
22
# File 'lib/nxt_http_client/client_dsl.rb', line 20

def clear_fire_callbacks(*kinds)
  callbacks.clear(*kinds)
end

#configObject



36
37
38
# File 'lib/nxt_http_client/client_dsl.rb', line 36

def config
  @config ||= dup_option_from_ancestor(:@config) { Config.new }
end

#configure(opts = {}, &block) ⇒ Object



3
4
5
6
7
# File 'lib/nxt_http_client/client_dsl.rb', line 3

def configure(opts = {}, &block)
  opts.each { |k, v| config.send(k, v) }
  config.tap { |d| block.call(d) }
  config
end

#log(&block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/nxt_http_client/client_dsl.rb', line 9

def log(&block)
  @logger ||= block || dup_option_from_ancestor(:@logger)

  return unless @logger.present?
  logger = @logger

  around_fire do |client, request, response_handler, fire|
    Logger.new(logger).call(client, request, response_handler, fire)
  end
end

#response_handler(handler = Undefined.new, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/nxt_http_client/client_dsl.rb', line 44

def response_handler(handler = Undefined.new, &block)
  if undefined?(handler)
    @response_handler ||= dup_option_from_ancestor(:@response_handler) { NxtHttpClient::ResponseHandler.new }
  else
    @response_handler = handler
  end

  @response_handler.configure(&block) if block_given?
  @response_handler
end