Class: BWCLI::Configuration
- Inherits:
-
Object
- Object
- BWCLI::Configuration
- Defined in:
- lib/bwcli/configuration.rb
Overview
Configuration class to maintain config
Instance Method Summary collapse
-
#add_user(env, username, pwd) ⇒ Object
Add user to the config hash.
-
#api_endpoint ⇒ String
Returns the api endpoint for the current user.
-
#bwapi ⇒ Object
Creates and returns bwapi instance.
-
#config ⇒ Hash
Returns the config.
-
#current_user ⇒ Hash
Returns the current user set in config hash.
-
#current_user_exists? ⇒ Boolean
Check whether the current user exists in the config hash.
-
#env_exists?(env) ⇒ Boolean
Check whether a env exists in the config hash.
-
#initialize ⇒ Configuration
constructor
Initialises the configuration class.
-
#list_current_user ⇒ Object
List the current user within the config hash.
-
#list_users ⇒ Object
List all users within the config hash.
-
#oauth ⇒ Object
Authenticates current user.
-
#remove_user(username, env) ⇒ Object
Remove user.
-
#reset ⇒ Object
Resets the config to an empty hash.
-
#set_access_token(token) ⇒ Object
Sets access token for current user.
-
#set_current_user(env, username) ⇒ Object
Set current user.
-
#user_exists?(env, username) ⇒ Boolean
Check whether a user exists in the config hash.
-
#write_config ⇒ Object
Writes the config to file.
Constructor Details
#initialize ⇒ Configuration
Initialises the configuration class
11 12 13 14 15 16 17 18 |
# File 'lib/bwcli/configuration.rb', line 11 def initialize @config_file = File.join(File.("~"), ".bwcli") @config = Hashie::Mash.new(YAML.load_file @config_file) rescue Errno::ENOENT File.new(@config_file, "w") puts "\nAs you didn't have a .bwcli file, I created one for you!\n".green retry end |
Instance Method Details
#add_user(env, username, pwd) ⇒ Object
Add user to the config hash
25 26 27 28 29 30 |
# File 'lib/bwcli/configuration.rb', line 25 def add_user env, username, pwd config[env] = {} unless env_exists? env abort "The #{username} already exists!".yellow if user_exists? env, username config[env][username] = { 'access_token' => '', 'password' => encrypt_password(username, pwd) } write_config end |
#api_endpoint ⇒ String
Returns the api endpoint for the current user
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bwcli/configuration.rb', line 35 def api_endpoint case current_user.environment when 'int', 'integration' 'http://newapi.int.brandwatch.com' when 'rel', 'release', 'stage' 'http://newapi.rel.brandwatch.com' else 'http://newapi.brandwatch.com' end end |
#bwapi ⇒ Object
Creates and returns bwapi instance
49 50 51 52 53 54 55 |
# File 'lib/bwcli/configuration.rb', line 49 def bwapi abort "You have no current user set!".yellow unless current_user_exists? abort "There is no access token set for the current user".yellow if config.current_user.access_token.nil? abort "There is no environment set for the current user".yellow if config.current_user.environment.nil? return @bwapi ||= BWAPI::Client.new(:username => current_user.username, :password => decrypt_password(current_user.username, current_user.password), :api_endpoint => api_endpoint) end |
#config ⇒ Hash
Returns the config
60 61 62 |
# File 'lib/bwcli/configuration.rb', line 60 def config @config end |
#current_user ⇒ Hash
Returns the current user set in config hash
67 68 69 70 |
# File 'lib/bwcli/configuration.rb', line 67 def current_user abort "You have no current user set!".yellow unless current_user_exists? config.current_user end |
#current_user_exists? ⇒ Boolean
Check whether the current user exists in the config hash
75 76 77 78 |
# File 'lib/bwcli/configuration.rb', line 75 def current_user_exists? return false if config.current_user.nil? return true end |
#env_exists?(env) ⇒ Boolean
Check whether a env exists in the config hash
84 85 86 87 |
# File 'lib/bwcli/configuration.rb', line 84 def env_exists? env return false if config.nil? config.has_key? env end |
#list_current_user ⇒ Object
List the current user within the config hash
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/bwcli/configuration.rb', line 103 def list_current_user abort "You have no current user set!".yellow unless current_user_exists? puts "\nCurrent User" config.current_user.each do |k,v| if k == 'password' puts "#{k.yellow}: ** HIDDEN **".yellow else puts "#{k.yellow}: #{v}".yellow unless v.nil? end end end |
#list_users ⇒ Object
List all users within the config hash
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/bwcli/configuration.rb', line 90 def list_users abort "You have no users within your config file!".yellow if config.empty? puts "\nUser Configuration" config.each do |k, v| next if k == 'current_user' puts "\nEnvironment: #{k}" print_hash_values v end list_current_user if current_user_exists? end |
#oauth ⇒ Object
Authenticates current user
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/bwcli/configuration.rb', line 116 def oauth if current_user.access_token.nil? || current_user.access_token.empty? abort "Unable to login as #{current_user.username}".red unless bwapi.login set_access_token bwapi.access_token else bwapi.access_token = current_user.access_token abort "Unable to login as #{current_user.username}".red unless bwapi.login end return bwapi end |
#remove_user(username, env) ⇒ Object
Remove user
157 158 159 160 161 162 163 |
# File 'lib/bwcli/configuration.rb', line 157 def remove_user username, env abort "You have no users within your config file!".yellow if config.empty? abort "This user is currently set as the current user! Aborting!".yellow if current_user.username == username && current_user.env == env abort "The #{username} doesn't exist!".yellow unless user_exists? env, username config[env].delete username write_config end |
#reset ⇒ Object
Resets the config to an empty hash
129 130 131 132 |
# File 'lib/bwcli/configuration.rb', line 129 def reset @config = {} write_config end |
#set_access_token(token) ⇒ Object
Sets access token for current user
147 148 149 150 151 |
# File 'lib/bwcli/configuration.rb', line 147 def set_access_token token current_user.access_token = token config[current_user.environment].access_token = token write_config end |
#set_current_user(env, username) ⇒ Object
Set current user
138 139 140 141 142 |
# File 'lib/bwcli/configuration.rb', line 138 def set_current_user env, username abort "The #{username} doesn't exist! Please add!".yellow unless user_exists? env, username config.current_user = { 'username' => username, 'environment' => env, 'password' => config[env][username].password, 'access_token' => config[env][username].access_token } write_config end |
#user_exists?(env, username) ⇒ Boolean
Check whether a user exists in the config hash
170 171 172 173 |
# File 'lib/bwcli/configuration.rb', line 170 def user_exists? env, username return false if config.nil? || config.empty? return true if config[env].has_key? username end |
#write_config ⇒ Object
Writes the config to file
176 177 178 |
# File 'lib/bwcli/configuration.rb', line 176 def write_config File.open(@config_file, "w"){|f| YAML.dump(config, f)} end |