Class: FallCli::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/fallcli/config.rb

Overview

This is the configuration object. It reads in configuration from a .fallcli file located in the user’s home directory

Constant Summary collapse

FILE_NAME =
'.fallcli/config'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



15
16
17
18
# File 'lib/fallcli/config.rb', line 15

def initialize
  @path = ENV["FALLCLI_CONFIG_PATH"] || File.join(File.expand_path("~"), FILE_NAME)
  @data = self.load_config_file
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



10
11
12
# File 'lib/fallcli/config.rb', line 10

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/fallcli/config.rb', line 11

def path
  @path
end

Instance Method Details

#app_keyObject



29
30
31
# File 'lib/fallcli/config.rb', line 29

def app_key
  @data['authentication']['app_key']
end

#app_secretObject



41
42
43
# File 'lib/fallcli/config.rb', line 41

def app_secret
  @data['client']['app_secret']
end

#app_tokenObject



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

def app_token
  @data['client']['app_token']
end

#create_config_file(app_key, secret_key, app_token, app_secret) ⇒ Object

Writes a config file



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fallcli/config.rb', line 56

def create_config_file(app_key, secret_key, app_token, app_secret)
  FileUtils.mkdir_p File.join(File.expand_path("~"),'.fallcli/')
  require 'yaml'
  File.open(@path, File::RDWR|File::TRUNC|File::CREAT, 0600) do |file|
    data = {
      "authentication" => { "app_key" => app_key, "secret_key" => secret_key },
      "client" => { "app_token" => app_token, "app_secret" => app_secret }
    }
    file.write data.to_yaml
  end
end

#load_config_fileObject

If we can’t load the config file, self.data is nil, which we can check for in CheckConfiguration



22
23
24
25
26
27
# File 'lib/fallcli/config.rb', line 22

def load_config_file
  require 'yaml'
  YAML.load_file(@path)
rescue Errno::ENOENT
  return
end

#reload!Object

Re-loads the config



51
52
53
# File 'lib/fallcli/config.rb', line 51

def reload!
  @data = self.load_config_file
end

#reset!Object

Re-runs initialize



46
47
48
# File 'lib/fallcli/config.rb', line 46

def reset!
  self.send(:initialize)
end

#secret_keyObject



33
34
35
# File 'lib/fallcli/config.rb', line 33

def secret_key
  @data['authentication']['secret_key']
end