services 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a7004c57ec38e0a50c485274eb889a7d86eb3604
4
- data.tar.gz: 8d2295ec5857505fffdb5bbf1a745ff5b3cdc505
3
+ metadata.gz: 5badc829e3bbaa1e7dfde3b0578580a95c590cdd
4
+ data.tar.gz: 245f341ba15aa65aebb1707fdf7301727ac45d0d
5
5
  SHA512:
6
- metadata.gz: 9c79defb87ccc2c3f4b6b52e91273248de62c5c38853912299d240a0949d05571beb51032c8c30e6bbcd169d8ef63e5b73a0d20c13dd5091c7f34769029dc941
7
- data.tar.gz: 377da1dd477a4a80a5bd9f3ab0175b5389969fa2e9d5a7f8a9e7469bc00aa77da31adb102d932b92bb8db26d24b8b1f49f9ac2e2cc3960c7482e70efff16273e
6
+ metadata.gz: 7ac343f2d578b4a90424d65dea68526d73e7383466f91c5be9160045e06791796537f80c584e5edeb30b045b4b07960f4a3e0b898853e24399cff134abdb8848
7
+ data.tar.gz: 32759e41c136880fe9aa566fca32035f9b5815a1e31c4bae45a5df556d74a172d1d8ed2223c2b450d1e779ad8010c687be6cd42fa62bde00565055894681d10e
@@ -1,3 +1,7 @@
1
+ ## 1.2.0
2
+
3
+ * Convert log time to time object when fetching logs
4
+
1
5
  ## 1.1.1
2
6
 
3
7
  * When logging to Redis, convert all values to strings first
@@ -20,18 +20,22 @@ module Services
20
20
  end
21
21
 
22
22
  def fetch
23
- @redis.lrange(@key, 0, -1).map do |json|
24
- JSON.load json
25
- end
23
+ @redis.lrange(@key, 0, -1).map(&method(:log_entry_from_json))
26
24
  end
27
25
 
28
26
  def clear
29
27
  @redis.multi do
30
28
  @redis.lrange @key, 0, -1
31
29
  @redis.del @key
32
- end.first.map do |json|
33
- JSON.load json
34
- end
30
+ end.first.map(&method(:log_entry_from_json))
31
+ end
32
+
33
+ private
34
+
35
+ def log_entry_from_json(json)
36
+ data = JSON.load(json)
37
+ data['time'] = Time.at(data['time'])
38
+ data
35
39
  end
36
40
  end
37
41
  end
@@ -1,3 +1,3 @@
1
1
  module Services
2
- VERSION = '1.1.1'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -1,69 +1,103 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Services::Logger::Redis do
4
- let(:meta) { { foo: 'bar', class: Services::Base, object: redis } }
5
- let(:message) { "One day baby we'll be old" }
6
- let(:severity) { 'critical' }
7
- let(:key) { 'custom_log_key' }
8
- let(:redis) { Redis.new }
9
- let(:logger) { described_class.new(redis, key) }
10
-
11
- def log_entries
12
- redis.lrange(key, 0, -1).map do |json|
13
- JSON.load json
4
+ let(:key) { 'custom_log_key' }
5
+ let(:redis) { Redis.new }
6
+ let(:logger) { described_class.new(redis, key) }
7
+ let(:logs) {
8
+ [
9
+ {
10
+ time: Date.new(2014, 9, 15),
11
+ message: "One day baby we'll be old",
12
+ severity: :info,
13
+ meta: {
14
+ foo: 'bar',
15
+ class: Services::Base,
16
+ object: redis
17
+ }
18
+ }, {
19
+ time: Date.new(2014, 10, 10),
20
+ message: "Oh baby, we'll be old",
21
+ severity: :warning,
22
+ meta: {}
23
+ }, {
24
+ time: Date.new(2014, 11, 17),
25
+ message: 'And think of all the stories',
26
+ severity: :critical,
27
+ meta: {
28
+ one: 2,
29
+ three: 3.14
30
+ }
31
+ }, {
32
+ time: Date.new(2014, 11, 17),
33
+ message: 'That we could have told',
34
+ severity: :debug
35
+ }
36
+ ]
37
+ }
38
+ let(:fetched_logs) {
39
+ logs.reverse.map do |log|
40
+ log[:time] = log[:time].to_time
41
+ %i(message severity).each do |k|
42
+ log[k] = log[k].try(:to_s) || ''
43
+ end
44
+ log[:meta] = if log.has_key?(:meta)
45
+ log[:meta].map { |k, v| [k.to_s, v.to_s] }.to_h
46
+ else
47
+ {}
48
+ end
49
+ log.stringify_keys
14
50
  end
15
- end
51
+ }
16
52
 
17
53
  def create_logs
18
- (2.days.ago.to_i..Time.now.to_i).step(1.hour) do |timestamp|
19
- time = Time.at(timestamp)
20
- Timecop.freeze time do
21
- logger.log time.to_s(:long), weekday: time.strftime('%a')
54
+ logs.each do |log|
55
+ Timecop.freeze log[:time] do
56
+ args = [log[:message]]
57
+ args.push log[:meta] || {}
58
+ args.push log[:severity] if log.has_key?(:severity)
59
+ logger.log *args
22
60
  end
23
61
  end
24
62
  end
25
63
 
26
- describe '#log' do
27
- it 'logs properly' do
28
- Timecop.freeze do
29
- payload = {
30
- 'time' => Time.now.to_i,
31
- 'message' => message,
32
- 'severity' => severity,
33
- 'meta' => meta.map { |k, v| [k.to_s, v.to_s] }.to_h
34
- }
35
- expect do
36
- logger.log message, meta, severity
37
- end.to change { log_entries }.from([]).to([payload])
38
- end
64
+ def logs_in_db
65
+ redis.lrange(key, 0, -1).map do |json|
66
+ data = JSON.load(json)
67
+ data['time'] = Time.at(data['time'])
68
+ data
39
69
  end
40
70
  end
41
71
 
72
+ before do
73
+ redis.del key
74
+ end
75
+
42
76
  context 'when logs are present' do
43
77
  before do
44
78
  create_logs
45
- expect(log_entries.size).to be > 0
79
+ expect(logs_in_db.size).to eq(logs.size)
46
80
  end
47
81
 
48
82
  describe '#size' do
49
83
  it 'returns the amount of logs' do
50
- expect(logger.size).to eq(log_entries.size)
84
+ expect(logger.size).to eq(logs.size)
51
85
  end
52
86
  end
53
87
 
54
88
  describe '#fetch' do
55
89
  it 'returns all logs' do
56
- expect(logger.fetch).to eq(log_entries)
90
+ expect(logger.fetch).to eq(fetched_logs)
57
91
  end
58
92
  end
59
93
 
60
94
  describe '#clear' do
61
95
  it 'returns all logs' do
62
- expect(log_entries).to eq(logger.clear)
96
+ expect(fetched_logs).to eq(logger.clear)
63
97
  end
64
98
 
65
99
  it 'clears all log entries' do
66
- expect { logger.clear }.to change { log_entries }.to([])
100
+ expect { logger.clear }.to change { logs_in_db }.to([])
67
101
  end
68
102
  end
69
103
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: services
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Meurer