services 0.4.0 → 1.0.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: d822d6579be3909fe382b950cd93d69303fa06a0
4
- data.tar.gz: 528251f7a5177f619d09402b32aaaf2a84bb9b2c
3
+ metadata.gz: 06b81795775cc40d634101b8736c934f9bb64b75
4
+ data.tar.gz: 5a80029a0b5da7660e4ecad40e6868f5ea444919
5
5
  SHA512:
6
- metadata.gz: d6a2fc3cefd633a743bb5e86e8ffc2163e2a42c7f4b1406a912b7108f2603ca9f770644f90d6e9b9aa409087b19cf7f4064943ea045bdd425a2f504c458fc331
7
- data.tar.gz: 23805aad75f6806727fb430eef2bdec3b943ed7c3750ae5e479c97268a76884af1b001d90dc2ebb362cee5d6a1e96c556070856e6f90e8553ba9d4e63d97f87e
6
+ metadata.gz: e5148f72b9f1bd8ee35592bb106e0e25222c284d0eed74a94ec41dfd2daffe3fc2f90223f701e2dc2b24b42f33f27dbd38c0653adf1b703602738cccef1642bc
7
+ data.tar.gz: 03b295a1abdda5f1377c720ffe4d292a360dd108dcd2ec0fc347477a44304382775f38b183c384b6e0069f668975ebd59cca3ce4a5d03d4fa15e8a475ae89a1a
data/README.md CHANGED
@@ -49,7 +49,7 @@ The SQL `Services::BaseFinder` (discussed further down) generates is optimized f
49
49
 
50
50
  The following service takes one or more users or user IDs as an argument.
51
51
 
52
- ```
52
+ ```ruby
53
53
  module Services
54
54
  module Users
55
55
  class Delete < Services::Base
@@ -72,7 +72,7 @@ It's good practice to always return the objects a service has been operating on
72
72
 
73
73
  Another example, this time using `Services::BaseFinder`:
74
74
 
75
- ```
75
+ ```ruby
76
76
  module Services
77
77
  module Users
78
78
  class Find < Services::BaseFinder
data/lib/services.rb CHANGED
@@ -7,13 +7,14 @@ module Services
7
7
 
8
8
  with_configuration do
9
9
  has :host, classes: String
10
- has :log_dir, classes: [String, Pathname]
10
+ has :logger
11
11
  has :redis
12
12
  end
13
13
  end
14
14
 
15
15
  require_relative 'services/version'
16
- require_relative 'services/logger'
16
+ require_relative 'services/logger/file'
17
+ require_relative 'services/logger/redis'
17
18
  begin
18
19
  require_relative 'services/asyncable'
19
20
  rescue Services::BackgroundProcessorNotFound
@@ -1,17 +1,15 @@
1
1
  require 'active_support/tagged_logging'
2
2
 
3
3
  module Services
4
- class Logger
5
- def initialize
6
- unless Services.configuration.log_dir.nil?
7
- log_file = File.join(Services.configuration.log_dir, 'services.log')
4
+ module Logger
5
+ class File
6
+ def initialize(log_dir)
7
+ log_file = ::File.join(log_dir, 'services.log')
8
8
  @logger = ActiveSupport::TaggedLogging.new(::Logger.new(log_file))
9
9
  @logger.clear_tags!
10
10
  end
11
- end
12
11
 
13
- def log(tags, message, severity = :info)
14
- unless @logger.nil?
12
+ def log(message, tags = [], severity = :info)
15
13
  @logger.tagged Time.now, severity.upcase, *tags do
16
14
  @logger.send severity, message
17
15
  end
@@ -0,0 +1,28 @@
1
+ module Services
2
+ module Logger
3
+ class Redis
4
+ def initialize(redis, key = 'logs')
5
+ @redis, @key = redis, key
6
+ end
7
+
8
+ def log(message, tags = [], severity = :info)
9
+ value = {
10
+ time: Time.now.to_i,
11
+ message: message,
12
+ severity: severity,
13
+ tags: tags
14
+ }
15
+ @redis.lpush @key, value.to_json
16
+ end
17
+
18
+ def clear
19
+ @redis.multi do
20
+ @redis.lrange @key, 0, -1
21
+ @redis.del @key
22
+ end.first.map do |json|
23
+ JSON.load json
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -2,12 +2,14 @@ module Services
2
2
  class Base
3
3
  module CallLogger
4
4
  def call(*args)
5
+ return super if Services.configuration.logger.nil?
6
+
5
7
  log "START with args #{args}"
6
8
  log "CALLED BY #{caller || '(not found)'}"
7
9
  start = Time.now
8
10
  begin
9
11
  result = super
10
- rescue StandardError => e
12
+ rescue => e
11
13
  log_exception e
12
14
  raise e
13
15
  ensure
@@ -19,8 +21,7 @@ module Services
19
21
  private
20
22
 
21
23
  def log(message, severity = :info)
22
- @logger ||= Logger.new
23
- @logger.log [self.class, @id], message, severity
24
+ Services.configuration.logger.log message, [self.class, @id], severity
24
25
  end
25
26
 
26
27
  def log_exception(e, cause = false)
@@ -5,9 +5,5 @@ module Services
5
5
  initializer 'services.load_base_finder' do
6
6
  require 'services/base_finder'
7
7
  end
8
-
9
- config.after_initialize do
10
- Services.configuration.log_dir = Rails.root.join('log')
11
- end
12
8
  end
13
9
  end
@@ -1,3 +1,3 @@
1
1
  module Services
2
- VERSION = '0.4.0'
2
+ VERSION = '1.0.0'
3
3
  end
data/services.gemspec CHANGED
@@ -6,27 +6,28 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
6
  require 'services/version'
7
7
 
8
8
  Gem::Specification.new do |gem|
9
- gem.name = 'services'
10
- gem.version = Services::VERSION
11
- gem.platform = Gem::Platform::RUBY
12
- gem.author = 'Manuel Meurer'
13
- gem.email = 'manuel@krautcomputing.com'
14
- gem.summary = 'A nifty service layer for your Rails app'
15
- gem.description = 'A nifty service layer for your Rails app'
16
- gem.homepage = 'http://krautcomputing.github.io/services'
17
- gem.license = 'MIT'
9
+ gem.name = 'services'
10
+ gem.version = Services::VERSION
11
+ gem.platform = Gem::Platform::RUBY
12
+ gem.author = 'Manuel Meurer'
13
+ gem.email = 'manuel@krautcomputing.com'
14
+ gem.summary = 'A nifty service layer for your Rails app'
15
+ gem.description = 'A nifty service layer for your Rails app'
16
+ gem.homepage = 'http://krautcomputing.github.io/services'
17
+ gem.license = 'MIT'
18
+ gem.required_ruby_version = '~> 2.0'
19
+ gem.files = `git ls-files`.split($/)
20
+ gem.executables = gem.files.grep(%r(^bin/)).map { |f| File.basename(f) }
21
+ gem.test_files = gem.files.grep(%r(^(test|spec|features)/))
22
+ gem.require_paths = ['lib']
18
23
 
19
- gem.files = `git ls-files`.split($/)
20
- gem.executables = gem.files.grep(%r(^bin/)).map { |f| File.basename(f) }
21
- gem.test_files = gem.files.grep(%r(^(test|spec|features)/))
22
- gem.require_paths = ['lib']
23
-
24
- gem.add_development_dependency 'rake', '>= 0.9.0'
24
+ gem.add_development_dependency 'rake', '>= 0.9.0'
25
25
  gem.add_development_dependency 'guard-rspec', '~> 4.2'
26
- gem.add_development_dependency 'rspec', '~> 3.0'
27
- gem.add_development_dependency 'sidekiq', '~> 3.0'
28
- gem.add_development_dependency 'redis', '~> 3.0'
29
- gem.add_development_dependency 'tries', '~> 0.3'
30
- gem.add_runtime_dependency 'rails', '>= 3.0.0'
31
- gem.add_runtime_dependency 'gem_config', '~> 0.3'
26
+ gem.add_development_dependency 'rspec', '~> 3.0'
27
+ gem.add_development_dependency 'sidekiq', '~> 3.0'
28
+ gem.add_development_dependency 'redis', '~> 3.0'
29
+ gem.add_development_dependency 'tries', '~> 0.3'
30
+ gem.add_development_dependency 'timecop', '~> 0.7'
31
+ gem.add_runtime_dependency 'rails', '>= 3.0.0'
32
+ gem.add_runtime_dependency 'gem_config', '~> 0.3'
32
33
  end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe Services::Logger::Redis do
4
+ let(:tags) { %w(foo bar baz) }
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
14
+ end
15
+ end
16
+
17
+ describe '#log' do
18
+ it 'logs properly' do
19
+ Timecop.freeze do
20
+ payload = {
21
+ 'time' => Time.now.to_i,
22
+ 'message' => message,
23
+ 'severity' => severity,
24
+ 'tags' => tags
25
+ }
26
+ expect do
27
+ logger.log message, tags, severity
28
+ end.to change { log_entries }.from([]).to([payload])
29
+ end
30
+ end
31
+ end
32
+
33
+ describe '#clear' do
34
+ before do
35
+ (2.days.ago.to_i..Time.now.to_i).step(1.hour) do |timestamp|
36
+ time = Time.at(timestamp)
37
+ Timecop.freeze time do
38
+ tags = [time.strftime('%a')]
39
+ logger.log tags, time.to_s(:long)
40
+ end
41
+ end
42
+ expect(log_entries.size).to be > 0
43
+ end
44
+
45
+ it 'returns all log entries' do
46
+ expect(log_entries).to eq(logger.clear)
47
+ end
48
+
49
+ it 'clears all log entries' do
50
+ expect do
51
+ logger.clear
52
+ end.to change { log_entries }.to([])
53
+ end
54
+ end
55
+ end
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,8 @@ require 'rspec'
2
2
  require 'tries'
3
3
  require 'redis'
4
4
  require 'sidekiq'
5
+ require 'timecop'
6
+ require 'active_support/core_ext'
5
7
 
6
8
  require_relative '../lib/services'
7
9
 
@@ -26,8 +28,8 @@ sidekiq_pidfile = support_dir.join('sidekiq.pid')
26
28
  sidekiq_timeout = 20
27
29
 
28
30
  Services.configure do |config|
29
- config.redis = Redis.new
30
- config.log_dir = log_dir
31
+ config.redis = Redis.new
32
+ config.logger = Services::Logger::File.new(log_dir)
31
33
  end
32
34
 
33
35
  Sidekiq.configure_client do |config|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: services
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Meurer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-12 00:00:00.000000000 Z
11
+ date: 2014-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: timecop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.7'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.7'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rails
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -139,7 +153,8 @@ files:
139
153
  - lib/services/asyncable.rb
140
154
  - lib/services/base.rb
141
155
  - lib/services/base_finder.rb
142
- - lib/services/logger.rb
156
+ - lib/services/logger/file.rb
157
+ - lib/services/logger/redis.rb
143
158
  - lib/services/modules/call_logger.rb
144
159
  - lib/services/modules/exception_wrapper.rb
145
160
  - lib/services/modules/uniqueness_checker.rb
@@ -147,6 +162,7 @@ files:
147
162
  - lib/services/version.rb
148
163
  - services.gemspec
149
164
  - spec/services/base_spec.rb
165
+ - spec/services/logger/redis_spec.rb
150
166
  - spec/services/modules/call_logger_spec.rb
151
167
  - spec/services/modules/exception_wrapper_spec.rb
152
168
  - spec/services/modules/uniqueness_checker_spec.rb
@@ -167,9 +183,9 @@ require_paths:
167
183
  - lib
168
184
  required_ruby_version: !ruby/object:Gem::Requirement
169
185
  requirements:
170
- - - ">="
186
+ - - "~>"
171
187
  - !ruby/object:Gem::Version
172
- version: '0'
188
+ version: '2.0'
173
189
  required_rubygems_version: !ruby/object:Gem::Requirement
174
190
  requirements:
175
191
  - - ">="
@@ -183,6 +199,7 @@ specification_version: 4
183
199
  summary: A nifty service layer for your Rails app
184
200
  test_files:
185
201
  - spec/services/base_spec.rb
202
+ - spec/services/logger/redis_spec.rb
186
203
  - spec/services/modules/call_logger_spec.rb
187
204
  - spec/services/modules/exception_wrapper_spec.rb
188
205
  - spec/services/modules/uniqueness_checker_spec.rb