Module: RogIQ::ConfigStore
- Defined in:
- lib/rogiq/config_store.rb
Constant Summary collapse
- DIR =
File.join(Dir.home, ".rogiq").freeze
- PATH =
File.join(DIR, "config.yml").freeze
Class Method Summary collapse
- .account_id ⇒ Object
- .api_base_url ⇒ Object
- .clear! ⇒ Object
- .load ⇒ Object
- .merge!(attrs) ⇒ Object
- .remote_available? ⇒ Boolean
- .save(hash) ⇒ Object
- .stringify_keys(hash) ⇒ Object
- .token ⇒ Object
Class Method Details
.account_id ⇒ Object
49 50 51 52 53 |
# File 'lib/rogiq/config_store.rb', line 49 def account_id v = load["account_id"] s = v.to_s.strip s.empty? ? nil : s end |
.api_base_url ⇒ Object
39 40 41 42 |
# File 'lib/rogiq/config_store.rb', line 39 def api_base_url v = load["api_base_url"].to_s.strip v.empty? ? nil : v end |
.clear! ⇒ Object
30 31 32 |
# File 'lib/rogiq/config_store.rb', line 30 def clear! FileUtils.rm_f(PATH) end |
.load ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/rogiq/config_store.rb', line 13 def load return {} unless File.file?(PATH) YAML.safe_load(File.read(PATH), permitted_classes: [ Symbol, Time ]) || {} rescue Psych::SyntaxError {} end |
.merge!(attrs) ⇒ Object
26 27 28 |
# File 'lib/rogiq/config_store.rb', line 26 def merge!(attrs) save(load.merge(stringify_keys(attrs))) end |
.remote_available? ⇒ Boolean
34 35 36 37 |
# File 'lib/rogiq/config_store.rb', line 34 def remote_available? h = load !h["token"].to_s.strip.empty? && !h["api_base_url"].to_s.strip.empty? end |
.save(hash) ⇒ Object
21 22 23 24 |
# File 'lib/rogiq/config_store.rb', line 21 def save(hash) FileUtils.mkdir_p(DIR) File.write(PATH, YAML.dump(stringify_keys(hash))) end |
.stringify_keys(hash) ⇒ Object
55 56 57 |
# File 'lib/rogiq/config_store.rb', line 55 def stringify_keys(hash) hash.to_h.transform_keys(&:to_s) end |
.token ⇒ Object
44 45 46 47 |
# File 'lib/rogiq/config_store.rb', line 44 def token v = load["token"].to_s.strip v.empty? ? nil : v end |