Class: Rox::Core::ImpressionInvoker

Inherits:
Object
  • Object
show all
Defined in:
lib/rox/core/impression/impression_invoker.rb

Instance Method Summary collapse

Constructor Details

#initialize(internal_flags, custom_property_repository, device_properties, analytics_client, is_roxy, user_unhandled_error_invoker) ⇒ ImpressionInvoker

Returns a new instance of ImpressionInvoker.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rox/core/impression/impression_invoker.rb', line 9

def initialize(internal_flags, custom_property_repository, device_properties, analytics_client, is_roxy, user_unhandled_error_invoker)
  @internal_flags = internal_flags
  @custom_property_repository = custom_property_repository
  @device_properties = device_properties
  @analytics_client = analytics_client
  @is_roxy = is_roxy
  @user_unhandled_error_invoker = user_unhandled_error_invoker

  @impression_handlers = []
  @mutex = Mutex.new
end

Instance Method Details

#call_analytics_gateway(reporting_value, stickiness_property, context) ⇒ Object



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
# File 'lib/rox/core/impression/impression_invoker.rb', line 21

def call_analytics_gateway(reporting_value, stickiness_property, context)
  begin
    analytics_enabled = @internal_flags.enabled?('rox.internal.analytics')
    if analytics_enabled && !@is_roxy
      prop = @custom_property_repository.custom_property(stickiness_property) || @custom_property_repository.custom_property("rox.#{Rox::Core::PropertyType::DISTINCT_ID.name}")
      distinct_id = '(null_distinct_id'
      unless prop.nil?
        prop_value = prop.value(context)
        distinct_id = prop_value if prop_value.instance_of?(String)
      end

      event_time = DateTime.now.strftime('%Q').to_i
      begin
        event_time = ENV['rox.analytics.ms'].to_i if ENV['rox.analytics.ms']
      rescue StandardError
      end

      @analytics_client.track({
                                flag: reporting_value.name,
                                value: reporting_value.value,
                                distinctId: distinct_id,
                                type: 'IMPRESSION',
                                time: event_time
                              })
    end
  rescue StandardError => ex
    Logging.logger.error('Failed to send analytics', ex)
  end
end

#invoke(reporting_value, stickiness_property, context) ⇒ Object



51
52
53
54
# File 'lib/rox/core/impression/impression_invoker.rb', line 51

def invoke(reporting_value, stickiness_property, context)
  call_analytics_gateway(reporting_value, stickiness_property, context)
  raise_impression_event(ImpressionArgs.new(reporting_value, context))
end

#raise_impression_event(args) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rox/core/impression/impression_invoker.rb', line 62

def raise_impression_event(args)
  handlers = []
  @mutex.synchronize do
    handlers = @impression_handlers.clone
  end

  handlers.each do |handler|
    begin
      handler.call(args)
    rescue StandardError => e
      @user_unhandled_error_invoker.invoke(handler, ExceptionTrigger::IMPRESSION_HANDLER, e)
      Logging.logger.error('Impresssion handler exception', e)
    end
  end
end

#register_impression_handler(&block) ⇒ Object



56
57
58
59
60
# File 'lib/rox/core/impression/impression_invoker.rb', line 56

def register_impression_handler(&block)
  @mutex.synchronize do
    @impression_handlers << block
  end
end