Sha256: e73b386696e33331d758579859200951951a4631affdb691a141638f0eb38088

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module FieldTest
  module Controller
    extend ActiveSupport::Concern
    include Helpers

    included do
      if respond_to?(:helper_method)
        helper_method :field_test
        helper_method :field_test_converted
        helper_method :field_test_experiments
      end
    end

    private

    def field_test_participant
      participants = []

      if respond_to?(:current_user, true)
        user = send(:current_user)
        participants << user if user
      end

      cookie_key = "field_test"

      if request.headers["Field-Test-Visitor"]
        token = request.headers["Field-Test-Visitor"]
      elsif FieldTest.cookies
        token = cookies[cookie_key]

        if participants.empty? && !token
          token = SecureRandom.uuid
          cookies[cookie_key] = {value: token, expires: 30.days.from_now}
        end
      else
        # anonymity set
        # note: hashing does not conceal input
        token = Digest::UUID.uuid_v5(FieldTest::UUID_NAMESPACE, ["visitor", FieldTest.mask_ip(request.remote_ip), request.user_agent].join("/"))

        # delete cookie if present
        cookies.delete(cookie_key) if cookies[cookie_key]
      end

      # sanitize tokens
      token = token.gsub(/[^a-z0-9\-]/i, "") if token

      if token.present?
        participants << token

        # backwards compatibility
        participants << "cookie:#{token}" if FieldTest.legacy_participants
      end

      participants
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
field_test-0.3.0 lib/field_test/controller.rb