Browse code

Config parser

Cinan Rakosnik authored on 15/12/2012 at 20:59:32
Showing 5 changed files
... ...
@@ -1,15 +1,26 @@
1 1
 require_relative "rubysermon/version"
2 2
 require_relative "rubysermon/mod_loader"
3 3
 require_relative "rubysermon/mod_template"
4
+require_relative "rubysermon/configurator"
4 5
 
5 6
 module Rubysermon
6 7
 	class App
7 8
 
9
+		@config_path = "#{Dir.pwd}/config.json"
10
+		@config
11
+		@loader
12
+
8 13
 		def initialize
9 14
 
10 15
 		end
11 16
 
12 17
 		private
18
+
19
+		def config
20
+			configurator = Configurator.new(@config_path)
21
+			@config = configurator.get_settings()
22
+		end
23
+
13 24
 		def load_mod(mod_name)
14 25
 			begin
15 26
 				@loader = ModLoader.load mod_name
16 27
new file mode 100644
... ...
@@ -0,0 +1,5 @@
0
+{
1
+  "modules" : [
2
+    "cpu"
3
+  ]
4
+}
0 5
\ No newline at end of file
1 6
new file mode 100644
... ...
@@ -0,0 +1,17 @@
0
+require 'json'
1
+
2
+module Rubysermon
3
+	class Configurator
4
+
5
+		@parsed_settings
6
+
7
+		def initialize(config_path)
8
+			raw_json = File.read(config_path)
9
+			@parsed_settings = JSON.parse(raw_json, symbolize_names: true)
10
+		end
11
+
12
+		def get_settings
13
+			@parsed_settings
14
+		end
15
+	end
16
+end
0 17
\ No newline at end of file
1 18
new file mode 100644
... ...
@@ -0,0 +1,12 @@
0
+{
1
+  "modules" : [
2
+    "cpu",
3
+    "ram"
4
+  ],
5
+
6
+  "notify" : [
7
+    "mail"
8
+  ],
9
+
10
+  "refresh": 30
11
+}
0 12
\ No newline at end of file
1 13
new file mode 100644
... ...
@@ -0,0 +1,16 @@
0
+require_relative '../../test_helper.rb'
1
+
2
+class Configurator_test < MiniTest::Unit::TestCase
3
+
4
+	def test_load_config
5
+		config_file = "../../fixtures/config.json"
6
+		configurator = Rubysermon::Configurator.new(config_file)
7
+
8
+		loaded_settings = configurator.get_settings()
9
+		expected_settings = {modules: ["cpu", "ram"],
10
+							 notify: ["mail"],
11
+							 refresh: 30}
12
+
13
+		assert_equal(expected_settings, loaded_settings)
14
+	end
15
+end
0 16
\ No newline at end of file