Module: Vignette

Defined in:
lib/vignette.rb,
lib/vignette/version.rb

Defined Under Namespace

Modules: Errors

Constant Summary collapse

VERSION =
"0.0.12"

Class Method Summary collapse

Class Method Details

.clear_requestObject



59
60
61
# File 'lib/vignette.rb', line 59

def self.clear_request
  Vignette.request = Vignette.session = Vignette.cookies = nil # clear items
end

.get_store(session = Vignette.session, cookies = Vignette.cookies) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/vignette.rb', line 79

def self.get_store(session=Vignette.session, cookies=Vignette.cookies)
  case Vignette.store
  when :cookies
    raise Errors::ConfigError, "Missing cookies configuration in Vignette.  Must access Vignette in controller within around_filter." if cookies.nil?
    Rails.logger.debug [ 'Vignette::vignette', 'Cookies Sampling', cookies ] if Vignette.logging
    cookies.signed
  when :session
    raise Errors::ConfigError, "Missing session configuration in Vignette.  Must access Vignette in controller within around_filter." if session.nil?
    Rails.logger.debug [ 'Vignette::vignette', 'Session Sampling', session ] if Vignette.logging
    session
  else
    Rails.logger.debug [ 'Vignette::vignette', 'Random Sampling' ] if Vignette.logging
    {} # This is an empty storage
  end
end

.init(opts = {}) ⇒ Object

Member Functions



33
34
35
36
37
38
39
40
# File 'lib/vignette.rb', line 33

def self.init(opts={})
  opts = {
    store: nil,
    logging: nil
  }.with_indifferent_access.merge(opts)

  Vignette.store = opts[:store]
end

.request_config(request, session, cookies) ⇒ Object

Settings for configuations



43
44
45
46
47
# File 'lib/vignette.rb', line 43

def self.request_config(request, session, cookies)
  Vignette.request = request
  Vignette.session = session
  Vignette.cookies = cookies
end

.tests(session = Vignette.session, cookies = Vignette.cookies) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vignette.rb', line 63

def self.tests(session=Vignette.session, cookies=Vignette.cookies)
  store = get_store(session, cookies)
  store && store[:v].present? ? JSON(store[:v]) : {}
  if store && store[:v].present?
    v = JSON(store[:v])

    name_values = v.values.map { |v| [ v['n'], [ v['t'], v['v'] ] ] }.group_by { |el| el[0] }

    arr = name_values.map { |k,v| [ k.to_sym, v.sort { |a,b| b[1][0] <=> a[1][0] }.first[1][1] ] }

    Hash[arr]
  else
    {}
  end
end

.with_settings(request, session, cookies) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/vignette.rb', line 49

def self.with_settings(request, session, cookies)
  begin
    Vignette.request_config(request, session, cookies)

    yield
  ensure
    Vignette.clear_request
  end
end