Class: BsPlus::Config

Inherits:
Object show all
Defined in:
lib/bs_plus/config.rb

Constant Summary collapse

HomePath =
File.expand_path '~/.browserstack-cred'
LocalPath =
File.expand_path './.browserstack-cred'
OtherDefaults =
{ }

Class Method Summary collapse

Class Method Details

.allObject



27
28
# File 'lib/bs_plus/config.rb', line 27

def all
OtherDefaults.merge(home_config).merge(local_config)         end

.fetch(key, &blk) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bs_plus/config.rb', line 30

def fetch key, &blk
  unless block_given?
    all.fetch key
  else
    all.fetch key do
      value = yield
      write key, value
      value
    end
  end
end

.home_configObject



21
22
# File 'lib/bs_plus/config.rb', line 21

def home_config
YAML.load_file(HomePath ).deep_symbolize_keys rescue {}      end

.initObject



10
11
12
13
14
15
# File 'lib/bs_plus/config.rb', line 10

def init
  fetch(:username) {HighLine.new.ask('Please enter your browserstack username')}

  fetch(:password) {HighLine.new.ask('Please enter your password') {|h|
                                     h.echo = false  }}
end

.local_configObject



24
25
# File 'lib/bs_plus/config.rb', line 24

def local_config
YAML.load_file(LocalPath).deep_symbolize_keys rescue {}      end

.method_missing(key) ⇒ Object



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

def method_missing key
  fetch key
end

.write(key, value) ⇒ Object



42
43
44
45
# File 'lib/bs_plus/config.rb', line 42

def write key, value
  FileUtils.touch(HomePath) unless File.exists? HomePath
  write_config_file HomePath, home_config.merge({key => value})
end

.write_config_file(path, hash) ⇒ Object



47
48
49
# File 'lib/bs_plus/config.rb', line 47

def write_config_file path, hash
  File.open(HomePath, 'r+') { |f| YAML.dump hash.deep_stringify_keys, f }
end