Sha256: e55712726b5756e81ea273d712dced73cdb64e9434e4a8558df012fccdd4dde2

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

# dependencies
require "active_support"
require "browser"
require "ipaddr"

# modules
require "field_test/calculations"
require "field_test/experiment"
require "field_test/helpers"
require "field_test/participant"
require "field_test/version"

# integrations
require "field_test/engine" if defined?(Rails)

module FieldTest
  class Error < StandardError; end
  class ExperimentNotFound < Error; end
  class UnknownParticipant < Error; end

  # same as ahoy
  UUID_NAMESPACE = "a82ae811-5011-45ab-a728-569df7499c5f"

  def self.config_path
    path = defined?(Rails) ? Rails.root : File
    path.join("config", "field_test.yml")
  end

  def self.config
    @config ||= YAML.load(ERB.new(File.read(config_path)).result)
  end

  def self.excluded_ips
    @excluded_ips ||= Array(config["exclude"] && config["exclude"]["ips"]).map { |ip| IPAddr.new(ip) }
  end

  def self.exclude_bots?
    config["exclude"] && config["exclude"]["bots"]
  end

  def self.cache
    config["cache"]
  end

  def self.cookies
    config.key?("cookies") ? config["cookies"] : true
  end

  def self.legacy_participants
    config["legacy_participants"]
  end

  def self.precision
    config["precision"] || 0
  end

  def self.events_supported?
    unless defined?(@events_supported)
      connection = FieldTest::Membership.connection
      table_name = "field_test_events"
      @events_supported =
        if connection.respond_to?(:data_source_exists?)
          connection.data_source_exists?(table_name)
        else
          connection.table_exists?(table_name)
        end
    end
    @events_supported
  end

  def self.mask_ip(ip)
    addr = IPAddr.new(ip)
    if addr.ipv4?
      # set last octet to 0
      addr.mask(24).to_s
    else
      # set last 80 bits to zeros
      addr.mask(48).to_s
    end
  end
end

ActiveSupport.on_load(:action_controller) do
  require "field_test/controller"
  include FieldTest::Controller
end

ActiveSupport.on_load(:action_mailer) do
  require "field_test/mailer"
  include FieldTest::Mailer
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
field_test-0.4.1 lib/field_test.rb
field_test-0.4.0 lib/field_test.rb
field_test-0.3.2 lib/field_test.rb