Module: Gxapi

Defined in:
lib/gxapi.rb,
lib/gxapi/base.rb,
lib/gxapi/engine.rb,
lib/gxapi/ostruct.rb,
lib/gxapi/version.rb,
lib/gxapi/google_analytics.rb,
lib/gxapi/controller_methods.rb,
lib/generators/gxapi/install_generator.rb

Defined Under Namespace

Modules: ControllerMethods Classes: Base, Engine, GoogleAnalytics, InstallGenerator, Ostruct

Constant Summary collapse

VERSION =
'0.0.4'

Class Method Summary collapse

Class Method Details

.cacheActiveSupport::Cache::Store

get our cache instance

Returns:

  • (ActiveSupport::Cache::Store)


24
25
26
27
28
29
30
31
# File 'lib/gxapi.rb', line 24

def self.cache
  # if we have an overridden cache, return it
  return @overridden_cache if defined?(@overridden_cache)
  # use Rails.cache if it is defined
  return ::Rails.cache if defined?(::Rails) && ::Rails.cache
  # last resort, just use our own cache choice
  @cache ||= ActiveSupport::Cache::MemoryStore.new
end

.cache=(cache) ⇒ ActiveSupport::Cache::Store

setter for cache

Returns:

  • (ActiveSupport::Cache::Store)

    The new cache object



37
38
39
# File 'lib/gxapi.rb', line 37

def self.cache=(cache)
  @overridden_cache = cache
end

.cache_namespaceString?

namespace for our cache keys

Returns:

  • (String, nil)


45
46
47
# File 'lib/gxapi.rb', line 45

def self.cache_namespace
  @cache_namespace
end

.cache_namespace=(val) ⇒ String

setter for cache_namespace

Returns:

  • (String)

    New value for cache_namespace



53
54
55
# File 'lib/gxapi.rb', line 53

def self.cache_namespace=(val)
  @cache_namespace = val
end

.configGxapi::Ostruct

Gxapi config - this is loaded based on the config_path

Returns:



62
63
64
65
66
67
68
69
# File 'lib/gxapi.rb', line 62

def self.config
  @config ||= begin
    # parse our yml file after running it through ERB
    contents = File.read(self.config_path)
    yml = ERB.new(contents).result(binding)
    Gxapi::Ostruct.new(YAML.load(yml)[Gxapi.env])
  end
end

.config_pathString

get the config path for our config YAML file

Returns:

  • (String)

    defaults to #Rails.root/config/gxapi.yml



75
76
77
# File 'lib/gxapi.rb', line 75

def self.config_path
  @config_path ||= File.join(Rails.root, "config/gxapi.yml")
end

.config_path=(val) ⇒ String

setter for config path

Returns:



83
84
85
# File 'lib/gxapi.rb', line 83

def self.config_path=(val)
  @config_path = val
end

.envString

our environment - defaults to Rails.env or test

Returns:

  • (String)


91
92
93
# File 'lib/gxapi.rb', line 91

def self.env
  @env ||= defined?(::Rails) ? ::Rails.env : "test"
end

.env=(val) ⇒ String

Set the value of env

Returns:

  • (String)

    environment



99
100
101
# File 'lib/gxapi.rb', line 99

def self.env=(val)
  @env = val
end

.loggerLogger, Log4r::Logger

instance of logger for Gxapi

Returns:

  • (Logger, Log4r::Logger)


107
108
109
110
111
112
113
114
115
# File 'lib/gxapi.rb', line 107

def self.logger
  @logger ||= begin
    if defined?(::Rails) && ::Rails.logger
      ::Rails.logger
    else
      Logger.new(STDOUT)
    end
  end
end

.logger=(logger) ⇒ Logger, Log4r

Setter for the Logger

Parameters:

  • logger (Logger, Log4r)

Returns:

  • (Logger, Log4r)

    Logger instance



123
124
125
# File 'lib/gxapi.rb', line 123

def self.logger=(logger)
  @logger = logger
end

.reload_experimentsBoolean

Reload all data from experiments

Returns:

  • (Boolean)

    Always true



131
132
133
134
# File 'lib/gxapi.rb', line 131

def self.reload_experiments
  Base.new("").reload_experiments
  true
end

.rootString

root directory for gxapi

Returns:

  • (String)


141
142
143
# File 'lib/gxapi.rb', line 141

def self.root
  File.expand_path("../../", __FILE__)
end

.with_error_handling(&block) ⇒ Value, false

Wrap with error handling logs errors and returns false if an error occurs

Returns:

  • (Value, false)


151
152
153
154
155
156
157
158
159
# File 'lib/gxapi.rb', line 151

def self.with_error_handling(&block)
  begin
    yield
  rescue => e
    self.logger.error(e.message)
    self.logger.error(e.backtrace)
    false
  end
end