Module: WDI::Config

Defined in:
lib/wdi/config.rb

Defined Under Namespace

Classes: ConfigFile

Constant Summary collapse

KEY_REGEX =

only lowercase words separated by periods

/^[a-z.]*$/
VALUE_AS_KEY_REGEX =

a value containing at least one word prefixed by ‘:’

/:[a-z._]+/
ALLOWED_BASH_REGEX =
/(`(echo|pwd|ls|whoami)[^\|;&]*`)/

Class Method Summary collapse

Class Method Details

.configObject



209
210
211
# File 'lib/wdi/config.rb', line 209

def self.config
  @@config ||= self.load_local_configuration
end

.create(config_uri) ⇒ Object

WRAPPER MODULE METHODS



244
245
246
# File 'lib/wdi/config.rb', line 244

def self.create(config_uri)
  config_uri.nil? ? self.load_local_configuration : self.load_configuration_from(config_uri)
end

.exists?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/wdi/config.rb', line 205

def self.exists?
  File.exists?(self.path)
end

.get(property) ⇒ Object



248
249
250
# File 'lib/wdi/config.rb', line 248

def self.get(property)
  self.config.value_at property
end

.load_configuration_from(config_uri) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/wdi/config.rb', line 217

def self.load_configuration_from(config_uri)
  begin
    uri = URI(config_uri)
    config_file = ["http", "https"].include?(uri.scheme) ? uri.open.read : IO.read(config_uri)
    @@config = WDI::Config::ConfigFile.new config_file
    self.save

  rescue Errno::ENOENT => e
    raise WDI::ConfigError, "No file at this path. Use 'http://' prefix if URI."
  rescue URI::InvalidURIError => e
    raise WDI::ConfigError, "Malformed URI. Could not find file at this path."
  rescue OpenURI::HTTPError => e
    raise WDI::ConfigError, "Provided URI can not be found. Ensure that the link is active."
  rescue JSON::ParserError => e
    raise WDI::ConfigError, "Provided file is not correctly formatted JSON."
  end
end

.load_local_configurationObject



213
214
215
# File 'lib/wdi/config.rb', line 213

def self.load_local_configuration
  @@config = WDI::Config::ConfigFile.new(IO.read(self.path))
end

.pathObject



201
202
203
# File 'lib/wdi/config.rb', line 201

def self.path
  File.expand_path("config.json", WDI::Folder::path)
end

.properties(prefix = nil) ⇒ Object



257
258
259
# File 'lib/wdi/config.rb', line 257

def self.properties(prefix=nil)
  self.config.keys_with_prefix prefix
end

.saveObject



235
236
237
238
239
# File 'lib/wdi/config.rb', line 235

def self.save
  File.open(File.expand_path("config.json", WDI::Folder::path), "w+") do |f|
    f.write self.config.to_json
  end
end

.set(property, value) ⇒ Object



252
253
254
255
# File 'lib/wdi/config.rb', line 252

def self.set(property, value)
  self.config.set_key_value property, value
  self.save
end