Sha256: 17c0296d77613db68bc9fa22918c24dc42ccf68496b3f7a6f8b34c07faf4ace8

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

module Gxapi
  module ControllerMethods

    #
    # Get the variant and set it as an instance variable, handling
    # overriding by passing in the URL
    #
    # @param  identifier [String, Hash] Name for the experiment or ID hash
    # for the experiment
    # @param  ivar_name [String, Symbol] Name for the variable
    #
    # @example
    #
    #   def my_action
    #     gxapi_get_variant("Name")
    #   end
    #
    #   # OR
    #
    #   def my_action
    #     gxapi_get_variant(id: 'id_from_google')
    #   end
    #
    # @return [Celluloid::Future, Gxapi::Ostruct] Variant value
    def gxapi_get_variant(identifier, ivar_name = :variant)
      # handle override
      if params[ivar_name]
        val = Gxapi::Ostruct.new(
          value: {
            index: -1,
            experiment_id: nil,
            name: params[ivar_name]
          }
        )
      else
        val = self.gxapi_base.get_variant(identifier)
      end
      return instance_variable_set("@#{ivar_name}", val)
    end

    protected

    def gxapi_base
      @gxapi_base ||= begin
        Gxapi::Base.new(self.gxapi_token)
      end
    end

    def gxapi_token
      cookies[:gxapi] ||= SecureRandom.hex(16)
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gxapi_rails-0.1.0 lib/gxapi/controller_methods.rb
gxapi_rails-0.0.6 lib/gxapi/controller_methods.rb
gxapi_rails-0.0.5 lib/gxapi/controller_methods.rb