Class: Callstacking::Rails::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/callstacking/rails/settings.rb

Constant Summary collapse

SETTINGS_FILE =
"#{Dir.home}/.callstacking"
PRODUCTION_URL =
"https://callstacking.com"
ENV_KEY =
'CALLSTACKING_ENABLED'
CACHE_KEY =
:callstacking_enabled

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSettings

Returns a new instance of Settings.



16
17
18
19
# File 'lib/callstacking/rails/settings.rb', line 16

def initialize
  read_settings
  @client = Callstacking::Rails::Client::Authenticate.new(url, auth_token)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/callstacking/rails/settings.rb', line 9

def client
  @client
end

#settingsObject

Returns the value of attribute settings.



8
9
10
# File 'lib/callstacking/rails/settings.rb', line 8

def settings
  @settings
end

Class Method Details

.disable!Object



44
45
46
# File 'lib/callstacking/rails/settings.rb', line 44

def self.disable!
  Thread.current[CACHE_KEY] = false
end

.enable!Object



37
38
39
# File 'lib/callstacking/rails/settings.rb', line 37

def self.enable!
  Thread.current[CACHE_KEY] = true
end

Instance Method Details

#analyze_source?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/callstacking/rails/settings.rb', line 66

def analyze_source?
  settings[:analyze_source] || false
end

#auth_tokenObject



25
26
27
# File 'lib/callstacking/rails/settings.rb', line 25

def auth_token
  ENV['CALLSTACKING_API_TOKEN'] || settings[:auth_token]
end

#auth_token?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/callstacking/rails/settings.rb', line 29

def auth_token?
  !auth_token.nil?
end

#disable!Object



48
49
50
# File 'lib/callstacking/rails/settings.rb', line 48

def disable!
  self.class.disable!
end

#disabled?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/callstacking/rails/settings.rb', line 62

def disabled?
  !enabled?
end

#enable!Object



40
41
42
# File 'lib/callstacking/rails/settings.rb', line 40

def enable!
  self.class.enable!
end

#enable_disable(enabled: true) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/callstacking/rails/settings.rb', line 90

def enable_disable(enabled: true)
  settings[:enabled] = enabled

  props = { Callstacking::Rails::Env.environment => {
    settings: settings
  } }

  write_settings(complete_settings.merge(props))
end

#enabled?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/callstacking/rails/settings.rb', line 52

def enabled?
  return ActiveRecord::Type::Boolean.new.cast(ENV[ENV_KEY]) if ENV[ENV_KEY].present?
  return Thread.current[CACHE_KEY] if Thread.current[CACHE_KEY].present?
  false
end

#excludedObject



58
59
60
# File 'lib/callstacking/rails/settings.rb', line 58

def excluded
  settings[:excluded] || []
end

#save(email, password, url) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/callstacking/rails/settings.rb', line 70

def save(email, password, url)
  props = { auth_token: '',
            url: url,
            enabled: true,
            analyze_source: false,
  }

  props = { Callstacking::Rails::Env.environment => {
    settings: props
  } }

  write_settings(complete_settings.merge(props))

  props[Callstacking::Rails::Env.environment][:settings][:auth_token] = token(email, password)

  write_settings(complete_settings.merge(props))

  read_settings
end

#urlObject



21
22
23
# File 'lib/callstacking/rails/settings.rb', line 21

def url
  settings[:url] || PRODUCTION_URL
end

#write_settings(new_settings) ⇒ Object



33
34
35
# File 'lib/callstacking/rails/settings.rb', line 33

def write_settings(new_settings)
  File.write(SETTINGS_FILE, new_settings.to_yaml)
end