Class: FallCli::Configuration
- Inherits:
-
Object
- Object
- FallCli::Configuration
- 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
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #app_key ⇒ Object
- #app_secret ⇒ Object
- #app_token ⇒ Object
-
#create_config_file(app_key, secret_key, app_token, app_secret) ⇒ Object
Writes a config file.
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#load_config_file ⇒ Object
If we can’t load the config file, self.data is nil, which we can check for in CheckConfiguration.
-
#reload! ⇒ Object
Re-loads the config.
-
#reset! ⇒ Object
Re-runs initialize.
- #secret_key ⇒ Object
Constructor Details
#initialize ⇒ Configuration
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.("~"), FILE_NAME) @data = self.load_config_file end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
10 11 12 |
# File 'lib/fallcli/config.rb', line 10 def data @data end |
#path ⇒ Object (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_key ⇒ Object
29 30 31 |
# File 'lib/fallcli/config.rb', line 29 def app_key @data['authentication']['app_key'] end |
#app_secret ⇒ Object
41 42 43 |
# File 'lib/fallcli/config.rb', line 41 def app_secret @data['client']['app_secret'] end |
#app_token ⇒ Object
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.("~"),'.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_file ⇒ Object
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_key ⇒ Object
33 34 35 |
# File 'lib/fallcli/config.rb', line 33 def secret_key @data['authentication']['secret_key'] end |