Class: Skypager::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/skypager/configuration.rb

Constant Summary collapse

DefaultSettings =
{
  github_username: '',
  github_api_token: '',

  dnsimple_api_token: '',
  dnsimple_username: '',

  dropbox_app_key: '',
  dropbox_app_secret: '',
  dropbox_app_type: 'sandbox',
  dropbox_client_token: '',
  dropbox_client_secret: '',

  aws_access_key_id: '',
  aws_secret_access_key: '',

  google_client_id: '',
  google_client_secret: '',
  google_refresh_token: '',
  google_access_token: '',

  github_access_token: '',
  github_repository: '',

  domain: "skypager.io",

  sites_directory: { }
}

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/skypager/configuration.rb', line 45

def method_missing(meth, *args, &block)
  if current.key?(meth.to_s)
    return current.fetch(meth)
  end

  super
end

Class Method Details

.method_missing(meth, *args, &block) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/skypager/configuration.rb', line 37

def self.method_missing(meth, *args, &block)
  if instance.respond_to?(meth)
    return instance.send meth, *args, &block
  end

  nil
end

Instance Method Details

#amazon_setup?Boolean

Returns:

  • (Boolean)


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

def amazon_setup?
  aws_access_key_id.to_s.length > 0 && aws_secret_access_key.to_s.length > 0
end

#applied_configObject

Applied config is configuration values passed in context usually from the cli, but also in the unit tests



166
167
168
# File 'lib/skypager/configuration.rb', line 166

def applied_config
  @applied_config ||= {}
end

#apply_config(hash = {}) ⇒ Object



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

def apply_config(hash={})
  applied_config.merge!(hash)
  current.merge(applied_config)
end

#apply_config_from_path(path) ⇒ Object



136
137
138
139
140
141
# File 'lib/skypager/configuration.rb', line 136

def apply_config_from_path(path)
  path = Pathname(path)
  parsed = JSON.parse(path.read) rescue {}
  applied_config.merge!(parsed)
  nil
end

#calculate_config(using_environment = true) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/skypager/configuration.rb', line 118

def calculate_config(using_environment = true)
  @current = defaults.merge(home_config.merge(cwd_config.merge(applied_config))).to_mash

  (defaults.keys + home_config.keys + cwd_config.keys).uniq.each do |key|
    upper = key.to_s.upcase
    if ENV[upper]
      @current[key] = ENV[upper]
    end
  end if using_environment

  @current
end

#current(using_environment = true) ⇒ Object



114
115
116
# File 'lib/skypager/configuration.rb', line 114

def current(using_environment = true)
  @current ||= calculate_config(using_environment)
end

#cwd_configObject



170
171
172
173
174
175
176
# File 'lib/skypager/configuration.rb', line 170

def cwd_config
  @cwd_config ||= begin
    (cwd_config_path.exist? rescue false) ? JSON.parse(cwd_config_path.read) : {}
  rescue
    {}
  end
end

#cwd_config_pathObject



200
201
202
# File 'lib/skypager/configuration.rb', line 200

def cwd_config_path
  @cwd_config_path || Pathname(Dir.pwd).join('skypager.json')
end

#cwd_config_path=(value) ⇒ Object



196
197
198
# File 'lib/skypager/configuration.rb', line 196

def cwd_config_path= value
  @cwd_config_path = Pathname(value)
end

#defaultsObject



110
111
112
# File 'lib/skypager/configuration.rb', line 110

def defaults
  DefaultSettings.dup
end

#dnsimple_setup?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/skypager/configuration.rb', line 63

def dnsimple_setup?
  dnsimple_api_token.to_s.length > 0 && dnsimple_username.to_s.length > 0
end

#dropbox_setup?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/skypager/configuration.rb', line 67

def dropbox_setup?
  dropbox_app_key.to_s.length > 0 && dropbox_app_secret.to_s.length > 0
end

#get(setting) ⇒ Object



93
94
95
96
# File 'lib/skypager/configuration.rb', line 93

def get(setting)
  setting = setting.to_s.downcase
  primary_config[setting]
end

#google_setup?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/skypager/configuration.rb', line 71

def google_setup?
  google_client_secret.to_s.length > 0 && google_client_id.to_s.length > 0
end

#home_configObject



178
179
180
181
182
183
184
185
186
# File 'lib/skypager/configuration.rb', line 178

def home_config
  initialize! unless home_config_path.exist?

  @home_config ||= begin
    (home_config_path.exist? rescue false) ? JSON.parse(home_config_path.read) : {}
  rescue
    {}
  end
end

#home_config_pathObject



192
193
194
# File 'lib/skypager/configuration.rb', line 192

def home_config_path
  @home_config_path || Pathname(ENV['HOME']).join(".skypager","config.json")
end

#home_config_path=(value) ⇒ Object



188
189
190
# File 'lib/skypager/configuration.rb', line 188

def home_config_path= value
  @home_config_path = Pathname(value)
end

#initialize!(fresh = false) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/skypager/configuration.rb', line 53

def initialize!(fresh=false)
  return if home_config_path.exist? && !fresh

  FileUtils.mkdir_p home_config_path.dirname

  home_config_path.open("w+") do |fh|
    fh.write(DefaultSettings.to_json)
  end
end

#primary_configObject



89
90
91
# File 'lib/skypager/configuration.rb', line 89

def primary_config
  cwd_config_path.exist? ? cwd_config : home_config
end

#save!Object



143
144
145
146
147
148
# File 'lib/skypager/configuration.rb', line 143

def save!
  save_home_config
  save_cwd_config
  @current = nil
  true
end

#save_cwd_configObject



150
151
152
153
154
155
156
# File 'lib/skypager/configuration.rb', line 150

def save_cwd_config
  return nil unless cwd_config_path.exist?

  File.open(cwd_config_path, 'w+') do |fh|
    fh.write JSON.generate(cwd_config.to_hash)
  end
end

#save_home_configObject



158
159
160
161
162
# File 'lib/skypager/configuration.rb', line 158

def save_home_config
  File.open(home_config_path, 'w+') do |fh|
    fh.write JSON.generate(home_config.to_hash)
  end
end

#set(setting, value, persist = true, options = {}) ⇒ Object



98
99
100
101
102
103
# File 'lib/skypager/configuration.rb', line 98

def set(setting, value, persist = true, options={})
  setting = setting.to_s.downcase
  primary_config[setting] = value
  save! if persist == true
  value
end

#showObject



79
80
81
82
83
84
85
86
87
# File 'lib/skypager/configuration.rb', line 79

def show
  current.each do |p|
    key, value = p

    unless key == 'sites_directory'
      puts "#{key}: #{ value.inspect }"
    end
  end
end

#unset(setting, persist = true) ⇒ Object



105
106
107
108
# File 'lib/skypager/configuration.rb', line 105

def unset(setting, persist = true)
  primary_config.delete(setting)
  save! if persist == true
end