Class: TreasureData::Config

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

Constant Summary collapse

@@path =

class variables

ENV['TREASURE_DATA_CONFIG_PATH'] || ENV['TD_CONFIG_PATH'] || File.join(ENV['HOME'], '.td', 'td.conf')
@@apikey =
nil
@@cl_apikey =

flag to indicate whether an apikey has been provided through the command-line

false
@@endpoint =
nil
@@cl_endpoint =

flag to indicate whether an endpoint has been provided through the command-line

false
@@secure =
true
@@retry_post_requests =
false

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



27
28
29
30
# File 'lib/td/config.rb', line 27

def initialize
  @path = nil
  @conf = {}   # section.key = val
end

Class Method Details

.read(path = Config.path, create = false) ⇒ Object



32
33
34
# File 'lib/td/config.rb', line 32

def self.read(path=Config.path, create=false)
  new.read(path)
end

Instance Method Details

#[](cate_key) ⇒ Object



36
37
38
# File 'lib/td/config.rb', line 36

def [](cate_key)
  @conf[cate_key]
end

#[]=(cate_key, val) ⇒ Object



40
41
42
# File 'lib/td/config.rb', line 40

def []=(cate_key, val)
  @conf[cate_key] = val
end

#delete(cate_key) ⇒ Object



44
45
46
# File 'lib/td/config.rb', line 44

def delete(cate_key)
  @conf.delete(cate_key)
end

#read(path = @path) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/td/config.rb', line 48

def read(path=@path)
  @path = path
  begin
    data = File.read(@path)
  rescue
    e = ConfigNotFoundError.new($!.to_s)
    e.set_backtrace($!.backtrace)
    raise e
  end

  section = ""

  data.each_line {|line|
    line.strip!
    case line
    when /^#/
      next
    when /\[(.+)\]/
      section = $~[1]
    when /^(\w+)\s*=\s*(.+?)\s*$/
      key = $~[1]
      val = $~[2]
      @conf["#{section}.#{key}"] = val
    else
      raise ConfigParseError, "invalid config line '#{line}' at #{@path}"
    end
  }

  self
end

#save(path = @path||Config.path) ⇒ Object



79
80
81
82
# File 'lib/td/config.rb', line 79

def save(path=@path||Config.path)
  @path = path
  write
end