Class: BWCLI::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/bwcli/configuration.rb

Overview

Configuration class to maintain config

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

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.expand_path("~"), ".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

Parameters:

  • env (String)

    the environment

  • username (String)

    the username of the user

  • pwd (String)

    the password of the user



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_endpointString

Returns the api endpoint for the current user

Returns:

  • (String)

    the api endpoint



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

#bwapiObject

Creates and returns bwapi instance

Returns:

  • (Object)

    bwapi the 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

#configHash

Returns the config

Returns:

  • (Hash)

    config the current config



60
61
62
# File 'lib/bwcli/configuration.rb', line 60

def config
  @config
end

#current_userHash

Returns the current user set in config hash

Returns:

  • (Hash)

    returns the current user



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

Returns:

  • (Boolean)


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

Parameters:

  • env (String)

    the environment

Returns:

  • (Boolean)


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_userObject

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_usersObject

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

#oauthObject

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.
    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.
  end

  return bwapi
end

#remove_user(username, env) ⇒ Object

Remove user

Parameters:

  • username (String)

    the username of the user

  • env (String)

    the environment



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

#resetObject

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

Parameters:

  • token (String)

    the access token to store in config hash



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

Parameters:

  • env (String)

    the environment

  • username (String)

    the username of the 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

Parameters:

  • env (String)

    the environment

  • username (String)

    the username of the user

Returns:

  • (Boolean)


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_configObject

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