forked from socky/socky-server-ruby
/
config.rb
52 lines (42 loc) · 1.53 KB
/
config.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'yaml'
module Socky
module Server
class Config
# Each config key calls corresponding method with value as param
def initialize(config = {})
return unless config.is_a?(Hash)
# Config file should be readed first
file = config.delete(:config_file)
self.config_file(file) unless file.nil?
config.each { |key, value| self.send(key, value) if self.respond_to?(key) }
end
# Enable or disable debug
def debug(arg)
Logger.enabled = !!arg
end
# List of applications if Hash form where key is app name
# and value is app secret.
# @example valid hash
# { 'my_app_name' => 'my_secret' }
def applications(arg)
raise ArgumentError, 'expected Hash' unless arg.is_a?(Hash)
arg.each do |app_name, app_secret|
Socky::Server::Application.new(app_name.to_s, app_secret.to_s)
end
end
# Reads config file
# This should be evaluated before other methods to prevent
# overriding settings by config ones(config should have lower priority)
def config_file(path)
raise ArgumentError, 'expected String' unless path.is_a?(String)
raise ArgumentError, "config file not found: #{path}" unless File.exists?(path)
begin
config = YAML.load_file(path)
rescue Exception
raise ArgumentError, 'invalid config file'
end
Config.new(config)
end
end
end
end