Sha256: 091d748fd8ecc478b29ce55ab029bd652560fed8b3f7f7ad19ff186443f50bde

Contents?: true

Size: 906 Bytes

Versions: 16

Compression:

Stored size: 906 Bytes

Contents

module TestTrack::Analytics
  class SafeWrapper
    attr_reader :underlying

    def initialize(underlying)
      @underlying = underlying
    end

    def error_handler=(handler)
      raise ArgumentError, "error_handler must be a lambda" unless handler.lambda?
      raise ArgumentError, "error_handler must accept 1 argument" unless handler.arity == 1
      @error_handler = handler
    end

    def track_assignment(visitor_id, assignment, params = {})
      safe_action { underlying.track_assignment(visitor_id, assignment, params) }
    end

    def alias(visitor_id, existing_id)
      safe_action { underlying.alias(visitor_id, existing_id) }
    end

    private

    def error_handler
      @error_handler || ->(e) do
        Rails.logger.error e
      end
    end

    def safe_action
      yield
      true
    rescue StandardError => e
      error_handler.call e
      false
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
test_track_rails_client-2.0.0 app/models/test_track/analytics/safe_wrapper.rb
test_track_rails_client-1.3.0 app/models/test_track/analytics/safe_wrapper.rb
test_track_rails_client-1.2.0 app/models/test_track/analytics/safe_wrapper.rb
test_track_rails_client-1.1.0 app/models/test_track/analytics/safe_wrapper.rb
test_track_rails_client-1.0.0 app/models/test_track/analytics/safe_wrapper.rb
test_track_rails_client-0.9.20 app/models/test_track/analytics/safe_wrapper.rb
test_track_rails_client-0.9.19 app/models/test_track/analytics/safe_wrapper.rb
test_track_rails_client-0.9.18 app/models/test_track/analytics/safe_wrapper.rb
test_track_rails_client-0.9.17 app/models/test_track/analytics/safe_wrapper.rb
test_track_rails_client-0.9.16 app/models/test_track/analytics/safe_wrapper.rb
test_track_rails_client-0.9.15 app/models/test_track/analytics/safe_wrapper.rb
test_track_rails_client-0.9.14 app/models/test_track/analytics/safe_wrapper.rb
test_track_rails_client-0.9.13 app/models/test_track/analytics/safe_wrapper.rb
test_track_rails_client-0.9.12 app/models/test_track/analytics/safe_wrapper.rb
test_track_rails_client-0.9.11 app/models/test_track/analytics/safe_wrapper.rb
test_track_rails_client-0.9.10 app/models/test_track/analytics/safe_wrapper.rb