bystander 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ab91cc6152250993e6eddd5e7aa68d7ebf0928d8
4
+ data.tar.gz: 10d52f786b8856f329a4c03119095127096ab673
5
+ SHA512:
6
+ metadata.gz: 796472da0f45288fa328c7ad406cdb815613e0f54a61d92ddfebf9a8b387d976740c323284f48186f2ec098f63a81e4425a0952ac152c3e218bc2b95489f036d
7
+ data.tar.gz: 83453d0e50711c9ded9b60048253949c390b66b4f8da979a11716e905352dca0668d0b063b7a550fdf339d2eda0f4f9368a8ffd15cca603870967cc67d330dad
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rake'
4
+ gem 'minitest'
5
+ gem 'mocha'
6
+
7
+ gem 'slack-notifier'
data/Gemfile.lock ADDED
@@ -0,0 +1,18 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ metaclass (0.0.4)
5
+ minitest (5.3.2)
6
+ mocha (1.1.0)
7
+ metaclass (~> 0.0.1)
8
+ rake (10.3.2)
9
+ slack-notifier (0.6.0)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ minitest
16
+ mocha
17
+ rake
18
+ slack-notifier
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # Bystander
2
+
3
+ A Ruby Gem that makes it easy to log your application's Critical Paths™ without
4
+ having to touch the behaviour of your code.
5
+
6
+ ![Bystander in action, in a Slack channel](/screenshot.png?raw=true)
7
+
8
+ ## Getting started
9
+
10
+ ### How does it work?
11
+
12
+ With some basic configuration, Bystander hooks in to your Ruby classes
13
+ and creates a log entry every time one of the methods you specify is
14
+ called, and when the method returns.
15
+
16
+ ### Where does it log to?
17
+
18
+ Anywhere! Well, if you want it to. Currently it defaults to logging to a
19
+ [Slack](https://slack.com) channel, but is designed to work with any
20
+ transport method (warning: you're gonna have to write some code).
21
+
22
+ ### Usage
23
+
24
+ #### Automatic Logging
25
+
26
+ At the top of the class containing the methods you want to log, include
27
+ the `Bystander` module and tell it what methods you're interested in.
28
+
29
+ ```ruby
30
+ class MaintenanceHandler
31
+ include Bystander
32
+ notify [:under_maintenance]
33
+
34
+ def under_maintenance
35
+ #...
36
+ end
37
+ end
38
+ ```
39
+
40
+ Whenever this method is called, Bystander will log the call and the
41
+ return:
42
+
43
+ ```
44
+ Calling: MaintenanceHandler::under_maintenance([])
45
+ Finished: MaintenanceHandler::under_maintenance([])
46
+ ```
47
+
48
+ #### Manual Logging
49
+
50
+ Bystander has a basic capability to log custom messages, inline in your
51
+ code, like a normal logger.
52
+
53
+ ```ruby
54
+ Bystander.log("I've made a huge mistake")
55
+ ```
56
+
57
+ ### Configuration
58
+
59
+ #### Slack
60
+
61
+ Before you use Bystander, ensure you run the following configuration
62
+ block (e.g., in a Rails initializer):
63
+
64
+ ```ruby
65
+ Bystander::Transports::Slack.configure do |slack|
66
+ slack.domain 'wcmc'
67
+ slack.username 'Bystander'
68
+ slack.auth_token ENV['SLACK_AUTH_TOKEN']
69
+ slack.channel '#bystander'
70
+
71
+ # Prepend all messages with this string. Useful for env info, etc.
72
+ slack.prepend "#{ENV['RAILS_ENV']} - (##{Process.pid}):"
73
+ end
74
+ ```
75
+
76
+ ## Using Bystander in Tests
77
+
78
+ Most of the time you won't want Bystander running during tests. To
79
+ disable Bystander, preface your test code (for example, in your
80
+ `test_helper.rb`) with:
81
+
82
+ ```
83
+ Bystander.enable_testing!
84
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ Rake::TestTask.new(:test) do |test|
4
+ test.libs << 'test'
5
+ test.pattern = 'test/**/*_test.rb'
6
+ end
7
+
8
+ task :default => :test
data/bystander.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../lib/bystander/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Adam Mulligan", "Andrea Rossi"]
5
+ gem.email = ["adam.mulligan@unep-wcmc.org", "andrea.rossi@unep-wcmc.org"]
6
+ gem.description = gem.summary = "Log your application flow without any ugly loggers"
7
+
8
+ gem.files = `git ls-files`.split("\n")
9
+ gem.test_files = `git ls-files -- test/*`.split("\n")
10
+ gem.name = "bystander"
11
+ gem.require_paths = ["lib"]
12
+ gem.version = Bystander::VERSION
13
+
14
+ gem.add_development_dependency 'rake'
15
+ gem.add_development_dependency 'minitest'
16
+ gem.add_development_dependency 'mocha'
17
+
18
+ gem.add_dependency 'slack-notifier', '~> 0.6.0'
19
+ end
20
+
data/lib/bystander.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'bystander/transports/slack'
2
+ require 'bystander/class_methods'
3
+
4
+ module Bystander
5
+ def self.included base
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ def self.log message
10
+ transport.notify message
11
+ end
12
+
13
+ def self.transport
14
+ @transport ||= Bystander::Transports::Slack
15
+ end
16
+
17
+ def self.transport= transport
18
+ @transport = transport
19
+ end
20
+ end
@@ -0,0 +1,53 @@
1
+ require 'bystander/notifier'
2
+ require 'bystander/util/testing'
3
+
4
+ module Bystander
5
+ module ClassMethods
6
+ @@methods_to_notify = []
7
+ @@redefined_methods = []
8
+
9
+ def notify methods
10
+ @@methods_to_notify = methods
11
+ end
12
+
13
+ def method_added method
14
+ if notify?(method) && !redefined?(method)
15
+ @@redefined_methods << method
16
+ wrap_instance_method instance_method(method)
17
+ end
18
+ end
19
+
20
+ def singleton_method_added method
21
+ if notify?(method) && !redefined?(method)
22
+ @@redefined_methods << method
23
+ wrap_class_method method(method)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def notify? method
30
+ @@methods_to_notify.include? method
31
+ end
32
+
33
+ def redefined? method
34
+ @@redefined_methods.include? method
35
+ end
36
+
37
+ def wrap_instance_method method
38
+ define_method(method.name) do |*args, &block|
39
+ Bystander::Notifier.wrap("#{self.class.to_s}##{method.name}(#{args.join(', ')})") do
40
+ method.bind(self).call(*args, &block)
41
+ end
42
+ end
43
+ end
44
+
45
+ def wrap_class_method method
46
+ define_singleton_method(method.name) do |*args, &block|
47
+ Bystander::Notifier.wrap("#{self.to_s}::#{method.name}(#{args.join(', ')})") do
48
+ method.call(*args, &block)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,13 @@
1
+ module Bystander
2
+ module Notifier
3
+
4
+ def self.wrap identifier
5
+ Bystander.transport.notify "Calling: #{identifier}"
6
+ return_value = yield
7
+ Bystander.transport.notify "Finished: #{identifier}"
8
+
9
+ return_value
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module Bystander
2
+ module Transports
3
+ module DevNull
4
+ def self.notify message
5
+ # `echo #{message} > /dev/null`
6
+ end
7
+
8
+ def self.configure
9
+ yield self
10
+ end
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,32 @@
1
+ require 'slack-notifier'
2
+
3
+ module Bystander
4
+ module Transports
5
+ module Slack
6
+ SETTINGS = [:domain, :username, :auth_token, :channel, :prepend]
7
+
8
+ SETTINGS.each do |setting|
9
+ define_singleton_method(setting) do |*args|
10
+ return instance_variable_get(:"@#{setting}") if args.length == 0
11
+ instance_variable_set(:"@#{setting}", args[0])
12
+ end
13
+ end
14
+
15
+ def self.slack
16
+ @slack ||= ::Slack::Notifier.new(domain, auth_token).tap { |slack|
17
+ slack.channel = channel
18
+ slack.username = username
19
+ }
20
+ end
21
+
22
+ def self.notify message
23
+ slack.ping "_#{prepend}_ `#{message}`"
24
+ end
25
+
26
+ def self.configure
27
+ yield self
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,7 @@
1
+ require 'bystander/transports/dev_null'
2
+
3
+ module Bystander
4
+ def self.enable_testing!
5
+ transport = Bystander::Transports::DevNull
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Bystander
2
+ VERSION = "1.0.0"
3
+ end
data/screenshot.png ADDED
Binary file
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ class TestObject < Object; end;
4
+
5
+ class BystanderTest < MiniTest::Test
6
+ def test_notify_notifies_before_and_after_method
7
+ TestObject.class_eval do
8
+ include Bystander
9
+ notify [:manbonify]
10
+ define_method(:manbonify) {|an_arg| "fake return value" }
11
+ end
12
+
13
+ object = TestObject.new
14
+
15
+ Bystander.transport.expects(:notify).with("Calling: TestObject#manbonify(yo)")
16
+ Bystander.transport.expects(:notify).with("Finished: TestObject#manbonify(yo)")
17
+
18
+ return_value = object.manbonify "yo"
19
+ assert_equal "fake return value", return_value
20
+ end
21
+
22
+ def test_notify_notifies_before_and_after_class_method
23
+ TestObject.class_eval do
24
+ include Bystander
25
+ notify [:classest_of_methods]
26
+ define_singleton_method(:classest_of_methods){}
27
+ end
28
+
29
+ Bystander.transport.expects(:notify).with("Calling: TestObject::classest_of_methods()")
30
+ Bystander.transport.expects(:notify).with("Finished: TestObject::classest_of_methods()")
31
+ TestObject.classest_of_methods
32
+ end
33
+
34
+ def test_log_notifies_the_transport_with_given_message
35
+ message = 'I just logged something'
36
+ Bystander.transport.expects(:notify).with(message)
37
+
38
+ Bystander.log message
39
+ end
40
+ end
@@ -0,0 +1,7 @@
1
+ require 'bystander'
2
+
3
+ require 'minitest/autorun'
4
+ require 'mocha/mini_test'
5
+
6
+
7
+ Mocha::Configuration.prevent(:stubbing_non_existent_method)
@@ -0,0 +1,13 @@
1
+ require 'test_helper'
2
+
3
+ class TransportsSlackTest < Minitest::Test
4
+ def test_notify_pings_slack
5
+ message = 'a message'
6
+
7
+ slack_mock = mock()
8
+ slack_mock.expects(:ping).with("__ `#{message}`")
9
+ Bystander::Transports::Slack.stubs(:slack).returns(slack_mock)
10
+
11
+ Bystander::Transports::Slack.notify message
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bystander
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Mulligan
8
+ - Andrea Rossi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-10-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: minitest
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: mocha
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: slack-notifier
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 0.6.0
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 0.6.0
70
+ description: Log your application flow without any ugly loggers
71
+ email:
72
+ - adam.mulligan@unep-wcmc.org
73
+ - andrea.rossi@unep-wcmc.org
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".ruby-version"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - README.md
82
+ - Rakefile
83
+ - bystander.gemspec
84
+ - lib/bystander.rb
85
+ - lib/bystander/class_methods.rb
86
+ - lib/bystander/notifier.rb
87
+ - lib/bystander/transports/dev_null.rb
88
+ - lib/bystander/transports/slack.rb
89
+ - lib/bystander/util/testing.rb
90
+ - lib/bystander/version.rb
91
+ - screenshot.png
92
+ - test/bystander_test.rb
93
+ - test/test_helper.rb
94
+ - test/transports/slack_test.rb
95
+ homepage:
96
+ licenses: []
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.2.2
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Log your application flow without any ugly loggers
118
+ test_files:
119
+ - test/bystander_test.rb
120
+ - test/test_helper.rb
121
+ - test/transports/slack_test.rb