lib/field_test.rb in field_test-0.2.4 vs lib/field_test.rb in field_test-0.3.0

- old
+ new

@@ -1,35 +1,57 @@ -require "browser" +# dependencies require "active_support" +require "browser" +require "ipaddr" + +# modules require "field_test/calculations" require "field_test/experiment" -require "field_test/engine" if defined?(Rails) 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 - def self.config - # reload in dev - @config = nil if Rails.env.development? + # same as ahoy + UUID_NAMESPACE = "a82ae811-5011-45ab-a728-569df7499c5f" - @config ||= YAML.load(ERB.new(File.read("config/field_test.yml")).result) + 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.exclude_bots? - config = self.config # dev performance 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 = @@ -39,18 +61,27 @@ 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 - include FieldTest::Helpers + require "field_test/controller" + include FieldTest::Controller end -ActiveSupport.on_load(:action_view) do - include FieldTest::Helpers -end - ActiveSupport.on_load(:action_mailer) do - include FieldTest::Helpers + require "field_test/mailer" + include FieldTest::Mailer end