Class: BBGAPI::Config

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

Instance Method Summary collapse

Constructor Details

#initialize(config_path) ⇒ Config

Constructor Try to load the config file and fall back to making one



12
13
14
15
16
17
18
19
# File 'lib/bbgapi/config.rb', line 12

def initialize(config_path)
  # This somewhat violates the separation of concern as
  # the Config class probably shouldn't have to deal with
  # the user, but it maintains the original code intent.
  @config_path = config_path
  @config = {}
  load || create
end

Instance Method Details

#[](key) ⇒ Object

A quick read accessor for the @config hash



58
59
60
# File 'lib/bbgapi/config.rb', line 58

def [](key)
  @config[key.to_s]
end

#[]=(key, value) ⇒ Object

A quick write accessor for the @config hash



63
64
65
# File 'lib/bbgapi/config.rb', line 63

def []=(key, value)
  @config[key.to_s] = value
end

#createObject

Make a YAML config file

  • config

    config file path



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bbgapi/config.rb', line 43

def create
  puts "Blue Box Config File Not Found, let's create one..."
  bbg_cust_id = ask("BBG Customer ID:  ") { |q| q.default = "none" }
  bbg_api_key = ask("BBG API Key:  ") { |q| q.default = "none" }

  puts "#{bbg_cust_id}:#{bbg_api_key}"

  @config = {
    'bluebox_customer_id' => bbg_cust_id,
    'bluebox_api_key' => bbg_api_key
  }
  save
end

#loadObject

Load a YAML config file

  • config_path

    config file path



23
24
25
26
27
28
29
# File 'lib/bbgapi/config.rb', line 23

def load
  begin
    @config = YAML.load_file(@config_path)
  rescue
    nil
  end
end

#saveObject

Save a YAML config file

  • config_path

    config file path



33
34
35
36
37
38
39
# File 'lib/bbgapi/config.rb', line 33

def save
  File.open(@config_path, 'w') do |out|
    out.write(@config.to_yaml)
  end
  puts "Config file written, please rerun the app"
  exit 0
end