empezar 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +25 -1
- data/empezar.gemspec +1 -0
- data/lib/empezar.rb +2 -0
- data/lib/empezar/configuration.rb +2 -2
- data/lib/empezar/echoing_formatter.rb +28 -0
- data/lib/empezar/log.rb +2 -2
- data/lib/empezar/runner.rb +5 -1
- data/lib/empezar/version.rb +1 -1
- data/sample/config/main.yaml +2 -0
- data/sample/script.rb +11 -0
- data/spec/empezar/echoing_formatter_spec.rb +84 -0
- data/spec/empezar/runner_spec.rb +37 -9
- metadata +23 -2
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -8,9 +8,33 @@ A simple Ruby library to enforce a convention for configuration, logging and exe
|
|
8
8
|
|
9
9
|
## Usage
|
10
10
|
|
11
|
+
To start, you must create a `config` directory containing at least a `main.yaml` file with at least one configuration. Currently Empezar supports the `verbosity` flag, which can be setted to `silent` and the application's log will not be echoed in command line (otherwise it will be):
|
12
|
+
|
13
|
+
```yaml
|
14
|
+
#config/main.yaml
|
15
|
+
verbosity: normal # `silent` will silence stdout
|
16
|
+
```
|
17
|
+
|
18
|
+
Then create a ruby script anywhere in the folder and run it
|
19
|
+
|
11
20
|
```ruby
|
21
|
+
# script.rb
|
12
22
|
require 'empezar'
|
13
23
|
|
14
24
|
Runner.run # Maps config/main.yaml to Configuration as SymbolMatrix
|
15
25
|
# Gets ready Log as a logger in log/main.log
|
16
|
-
|
26
|
+
|
27
|
+
Log.info "This will be logged and written in the shell"
|
28
|
+
|
29
|
+
Configuration.each do |key, value|
|
30
|
+
Log.debug "#{key}: #{value}"
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
...and then
|
35
|
+
|
36
|
+
ruby script.rb
|
37
|
+
|
38
|
+
You can find this sample in the `sample` folder in this repo.
|
39
|
+
|
40
|
+
This is really short, so here, [have a picture of a monkey](http://i.dailymail.co.uk/i/pix/2011/07/04/article-2011051-0CDC0F0900000578-739_634x894.jpg) . Enjoy
|
data/empezar.gemspec
CHANGED
data/lib/empezar.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
require 'singleton'
|
2
2
|
require 'symbolmatrix'
|
3
|
+
require 'term/ansicolor'
|
3
4
|
require 'logger'
|
4
5
|
|
5
6
|
require 'empezar/version'
|
6
7
|
require 'empezar/exceptions'
|
7
8
|
require 'empezar/configuration'
|
8
9
|
require 'empezar/runner'
|
10
|
+
require 'empezar/echoing_formatter'
|
9
11
|
require 'empezar/log'
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Empezar
|
2
|
+
class EchoingFormatter
|
3
|
+
attr_accessor :wrapee
|
4
|
+
|
5
|
+
def initialize wrapee
|
6
|
+
@wrapee = wrapee
|
7
|
+
end
|
8
|
+
|
9
|
+
def call severity, datetime, progname, message
|
10
|
+
case severity
|
11
|
+
when Logger::Severity::FATAL, "FATAL"
|
12
|
+
$stdout.puts Term::ANSIColor.bold Term::ANSIColor.red message
|
13
|
+
when Logger::Severity::ERROR, "ERROR"
|
14
|
+
$stdout.puts Term::ANSIColor.red message
|
15
|
+
when Logger::Severity::WARN, "WARN"
|
16
|
+
$stdout.puts Term::ANSIColor.yellow message
|
17
|
+
when Logger::Severity::INFO, "INFO"
|
18
|
+
$stdout.puts Term::ANSIColor.blue message
|
19
|
+
when Logger::Severity::DEBUG, "DEBUG"
|
20
|
+
$stdout.puts Term::ANSIColor.dark message
|
21
|
+
when Logger::Severity::UNKNOWN, "UNKNOWN"
|
22
|
+
$stdout.puts message
|
23
|
+
end
|
24
|
+
|
25
|
+
wrapee.call severity, datetime, progname, message
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/empezar/log.rb
CHANGED
data/lib/empezar/runner.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module Empezar
|
2
2
|
class Runner
|
3
3
|
def self.run argument = 'config/main.yaml'
|
4
|
-
self.start_logger
|
5
4
|
self.start_configuration argument
|
5
|
+
self.start_logger
|
6
6
|
end
|
7
7
|
|
8
8
|
def self.start_configuration argument
|
@@ -15,6 +15,10 @@ module Empezar
|
|
15
15
|
def self.start_logger
|
16
16
|
Dir.mkdir 'log' unless Dir.exist? 'log'
|
17
17
|
Empezar::Log.start Logger.new 'log/main.log', 'daily'
|
18
|
+
if Empezar::Configuration.instance.has_key? :verbosity and Empezar::Configuration.instance.verbosity == 'silent'
|
19
|
+
else
|
20
|
+
Empezar::Log.instance.formatter = EchoingFormatter.new Logger::Formatter.new
|
21
|
+
end
|
18
22
|
end
|
19
23
|
end
|
20
24
|
end
|
data/lib/empezar/version.rb
CHANGED
data/sample/script.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# script.rb
|
2
|
+
require 'empezar'
|
3
|
+
|
4
|
+
Runner.run # Maps config/main.yaml to Configuration as SymbolMatrix
|
5
|
+
# Gets ready Log as a logger in log/main.log
|
6
|
+
|
7
|
+
Log.info "This will be logged and written in the shell"
|
8
|
+
|
9
|
+
Configuration.each do |key, value|
|
10
|
+
Log.debug "#{key}: #{value}"
|
11
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Empezar::EchoingFormatter do
|
4
|
+
before do
|
5
|
+
@formatter_stub = stub 'formatter'
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '#initialize' do
|
9
|
+
it 'should receive a Formatter as an argument and store it as the wrapee' do
|
10
|
+
my_formatter = Empezar::EchoingFormatter.new @formatter_stub
|
11
|
+
my_formatter.wrapee.should == @formatter_stub
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#call' do
|
16
|
+
context 'fatal error' do
|
17
|
+
it 'should puts to stdout with a bold red message' do
|
18
|
+
@formatter_stub.stub :call
|
19
|
+
$stdout.should_receive(:puts).with Term::ANSIColor.bold Term::ANSIColor.red "message"
|
20
|
+
|
21
|
+
formatter = Empezar::EchoingFormatter.new @formatter_stub
|
22
|
+
formatter.call "FATAL", nil, nil, "message"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'error' do
|
27
|
+
it 'should puts to stdout with a red message' do
|
28
|
+
@formatter_stub.stub :call
|
29
|
+
$stdout.should_receive(:puts).with Term::ANSIColor.red "message"
|
30
|
+
|
31
|
+
formatter = Empezar::EchoingFormatter.new @formatter_stub
|
32
|
+
formatter.call "ERROR", nil, nil, "message"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'warn' do
|
37
|
+
it 'should puts to stdout with a yellow message' do
|
38
|
+
@formatter_stub.stub :call
|
39
|
+
$stdout.should_receive(:puts).with Term::ANSIColor.yellow "message"
|
40
|
+
|
41
|
+
formatter = Empezar::EchoingFormatter.new @formatter_stub
|
42
|
+
formatter.call "WARN", nil, nil, "message"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'info' do
|
47
|
+
it 'should puts to stdout with a blue message' do
|
48
|
+
@formatter_stub.stub :call
|
49
|
+
$stdout.should_receive(:puts).with Term::ANSIColor.blue "message"
|
50
|
+
|
51
|
+
formatter = Empezar::EchoingFormatter.new @formatter_stub
|
52
|
+
formatter.call "INFO", nil, nil, "message"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'unknown' do
|
57
|
+
it 'should puts to stdout with the message' do
|
58
|
+
@formatter_stub.stub :call
|
59
|
+
$stdout.should_receive(:puts).with "message"
|
60
|
+
|
61
|
+
formatter = Empezar::EchoingFormatter.new @formatter_stub
|
62
|
+
formatter.call "UNKNOWN", nil, nil, "message"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'debug' do
|
67
|
+
it 'should puts to stdout with a dark message' do
|
68
|
+
@formatter_stub.stub :call
|
69
|
+
$stdout.should_receive(:puts).with Term::ANSIColor.dark "message"
|
70
|
+
|
71
|
+
formatter = Empezar::EchoingFormatter.new @formatter_stub
|
72
|
+
formatter.call "DEBUG", nil, nil, "message"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should call the #call of the wrapee with all the arguments' do
|
77
|
+
arg1 = stub; arg2 = stub; arg3 = stub; arg4 = stub
|
78
|
+
@formatter_stub.should_receive(:call).with arg1, arg2, arg3, arg4
|
79
|
+
|
80
|
+
formatter = Empezar::EchoingFormatter.new @formatter_stub
|
81
|
+
formatter.call arg1, arg2, arg3, arg4
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/spec/empezar/runner_spec.rb
CHANGED
@@ -26,23 +26,51 @@ describe Empezar::Runner do
|
|
26
26
|
end
|
27
27
|
|
28
28
|
describe '.start_logger' do
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
29
|
+
before do
|
30
|
+
@demo_logger = stub 'demo logger'
|
31
|
+
Logger.should_receive(:new).with('log/main.log', 'daily').and_return @demo_logger
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'normal conditions of pressure and temperature' do
|
35
|
+
it 'should initialize the log as logger pointing to log/main.log, daily with the EchoingFormatter' do
|
36
|
+
formatter = stub 'formatter'
|
37
|
+
echoing_formatter = stub 'echoing formatter'
|
38
|
+
|
39
|
+
Dir.should_receive(:exist?).with('log').and_return true
|
40
|
+
Empezar::Configuration.instance.should_receive(:has_key?).with(:verbosity).and_return false
|
41
|
+
|
42
|
+
Logger::Formatter.should_receive(:new).and_return formatter
|
43
|
+
Empezar::EchoingFormatter.should_receive(:new).with(formatter).and_return echoing_formatter
|
44
|
+
@demo_logger.should_receive(:formatter=).with echoing_formatter
|
34
45
|
|
35
|
-
|
46
|
+
Empezar::Log.should_receive(:start).with @demo_logger
|
47
|
+
Empezar::Log.should_receive(:instance).and_return @demo_logger
|
48
|
+
|
49
|
+
Empezar::Runner.start_logger
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'the verbosity level is set to silent' do
|
54
|
+
it 'should not set the formatter as an echoing formatter' do
|
55
|
+
Empezar::EchoingFormatter.should_not_receive :new
|
56
|
+
|
57
|
+
Empezar::Configuration.instance.should_receive(:has_key?).with(:verbosity).and_return true
|
58
|
+
Empezar::Configuration.instance.should_receive(:verbosity).and_return 'silent'
|
59
|
+
|
60
|
+
Empezar::Log.should_receive(:start).with @demo_logger
|
61
|
+
|
62
|
+
Empezar::Runner.start_logger
|
63
|
+
end
|
36
64
|
end
|
37
65
|
|
38
66
|
context 'the dir log does not exist' do
|
39
67
|
it 'should create the dir log' do
|
68
|
+
Empezar::Configuration.instance.should_receive(:has_key?).with(:verbosity).and_return true
|
69
|
+
Empezar::Configuration.instance.should_receive(:verbosity).and_return 'silent'
|
40
70
|
Dir.should_receive(:exist?).with('log').and_return false
|
41
71
|
Dir.should_receive(:mkdir).with('log')
|
42
72
|
|
43
|
-
demo_logger
|
44
|
-
Logger.should_receive(:new).with('log/main.log', 'daily').and_return demo_logger
|
45
|
-
Empezar::Log.should_receive(:start).with demo_logger
|
73
|
+
Empezar::Log.should_receive(:start).with @demo_logger
|
46
74
|
|
47
75
|
Empezar::Runner.start_logger
|
48
76
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: empezar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: symbolmatrix
|
@@ -27,6 +27,22 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: term-ansicolor
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: rspec
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -59,12 +75,16 @@ files:
|
|
59
75
|
- empezar.gemspec
|
60
76
|
- lib/empezar.rb
|
61
77
|
- lib/empezar/configuration.rb
|
78
|
+
- lib/empezar/echoing_formatter.rb
|
62
79
|
- lib/empezar/exceptions.rb
|
63
80
|
- lib/empezar/log.rb
|
64
81
|
- lib/empezar/runner.rb
|
65
82
|
- lib/empezar/version.rb
|
83
|
+
- sample/config/main.yaml
|
84
|
+
- sample/script.rb
|
66
85
|
- spec/configuration_spec.rb
|
67
86
|
- spec/empezar/configuration_spec.rb
|
87
|
+
- spec/empezar/echoing_formatter_spec.rb
|
68
88
|
- spec/empezar/log_spec.rb
|
69
89
|
- spec/empezar/runner_spec.rb
|
70
90
|
- spec/log_spec.rb
|
@@ -98,6 +118,7 @@ summary: A simple Ruby library to enforce a convention for configuration, loggin
|
|
98
118
|
test_files:
|
99
119
|
- spec/configuration_spec.rb
|
100
120
|
- spec/empezar/configuration_spec.rb
|
121
|
+
- spec/empezar/echoing_formatter_spec.rb
|
101
122
|
- spec/empezar/log_spec.rb
|
102
123
|
- spec/empezar/runner_spec.rb
|
103
124
|
- spec/log_spec.rb
|