Class: Optimizely::EventDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely/event_dispatcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil, error_handler: nil, proxy_config: nil) ⇒ EventDispatcher

Returns a new instance of EventDispatcher.



30
31
32
33
34
# File 'lib/optimizely/event_dispatcher.rb', line 30

def initialize(logger: nil, error_handler: nil, proxy_config: nil)
  @logger = logger || NoOpLogger.new
  @error_handler = error_handler || NoOpErrorHandler.new
  @proxy_config = proxy_config
end

Instance Method Details

#dispatch_event(event) ⇒ Object

Dispatch the event being represented by the Event object.

Parameters:

  • event
    • Event object



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
65
66
67
# File 'lib/optimizely/event_dispatcher.rb', line 39

def dispatch_event(event)
  response = Helpers::HttpUtils.make_request(
    event.url, event.http_verb, event.params.to_json, event.headers, Helpers::Constants::EVENT_DISPATCH_CONFIG[:REQUEST_TIMEOUT], @proxy_config
  )

  error_msg = "Event failed to dispatch with response code: #{response.code}"

  case response.code.to_i
  when 400...500
    @logger.log(Logger::ERROR, error_msg)
    @error_handler.handle_error(HTTPCallError.new("HTTP Client Error: #{response.code}"))

  when 500...600
    @logger.log(Logger::ERROR, error_msg)
    @error_handler.handle_error(HTTPCallError.new("HTTP Server Error: #{response.code}"))
  else
    @logger.log(Logger::DEBUG, "event successfully sent with response code #{response.code}")
  end
rescue Timeout::Error => e
  @logger.log(Logger::ERROR, "Request Timed out. Error: #{e}")
  @error_handler.handle_error(e)

  # Returning Timeout error to retain existing behavior.
  e
rescue StandardError => e
  @logger.log(Logger::ERROR, "Event failed to dispatch. Error: #{e}")
  @error_handler.handle_error(e)
  nil
end