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

#error_class_for(response) ⇒ Object



69
70
71
# File 'lib/nxt_http_client/client_dsl.rb', line 69

def error_class_for(response)
  error_map[response.code.to_i] || NxtHttpClient::Error.error_class_for(response)
end

#error_mapObject



65
66
67
# File 'lib/nxt_http_client/client_dsl.rb', line 65

def error_map
  @error_map ||= dup_option_from_ancestor(:@error_map) { {} }
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

#map_error(status, error_class) ⇒ Object

Override the taxonomy’s error class for a status, e.g. ‘map_error 422, MyService::ValidationFailed`. Only takes effect when config.raise_error_taxonomy is enabled.



57
58
59
60
61
62
63
# File 'lib/nxt_http_client/client_dsl.rb', line 57

def map_error(status, error_class)
  unless error_class.is_a?(Class) && error_class <= NxtHttpClient::Error
    raise ArgumentError, "#{error_class.inspect} must be a subclass of NxtHttpClient::Error"
  end

  error_map[Integer(status)] = error_class
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