okuribito 0.0.1 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/README.md +75 -23
- data/circle.yml +4 -0
- data/lib/okuribito.rb +8 -82
- data/lib/okuribito/version.rb +1 -1
- metadata +4 -4
- data/bin/console +0 -14
- data/bin/setup +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3763c8c0fbdd9746f7e5d50595cb3c07d27e2eb5
|
4
|
+
data.tar.gz: b16c90b7bc3cee2fa02bf79983fa7b960a0c0f4c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad61526fedb01ab3b98455ff22bfa06d22c3b2ed7e09496f17df83683915c6b9ee2794a99ee4b26d63702038dd876aabf2d1ebfbe9f027b2aee589846dc6be43
|
7
|
+
data.tar.gz: ae050960f27604a7e1a91cda18a808f99db126030e734e39b1738c471fa8c63ca0123a83fc655ec14aedc7461da3a8d1f614c4e6e474377d19c3ee54a890e5be
|
data/Gemfile
ADDED
data/README.md
CHANGED
@@ -37,43 +37,95 @@ Edit `application.rb`
|
|
37
37
|
```ruby
|
38
38
|
class OkuribitoSetting < Rails::Railtie
|
39
39
|
config.after_initialize do
|
40
|
-
okuribito = Okuribito::OkuribitoPatch.new
|
41
|
-
|
42
|
-
|
43
|
-
slack: "https://hooks.slack.com/services/xxxxxxxxx/xxxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxx",
|
44
|
-
logging: "log/okuribito/method_called.log",
|
45
|
-
first_prepended: "log/okuribito/first_prepended.log"
|
46
|
-
}
|
47
|
-
)
|
40
|
+
okuribito = Okuribito::OkuribitoPatch.new do |method_name, obj_name, caller_info|
|
41
|
+
# TODO: do something as you like!
|
42
|
+
end
|
48
43
|
okuribito.apply("okuribito.yml")
|
49
44
|
end
|
50
45
|
end
|
51
46
|
```
|
52
47
|
|
53
|
-
|
54
|
-
Setting for console outout.
|
55
|
-
- `plain` is the simplest 1 line log.
|
56
|
-
- `back_trace` shows back trace in detail.
|
48
|
+
## The smallest example
|
57
49
|
|
58
|
-
|
59
|
-
|
50
|
+
```ruby
|
51
|
+
require "bundler/setup"
|
52
|
+
require "okuribito"
|
53
|
+
|
54
|
+
class TestTarget
|
55
|
+
def self.deprecated_self_method
|
56
|
+
end
|
57
|
+
|
58
|
+
def deprecated_method
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
okuribito = Okuribito::OkuribitoPatch.new do |method_name, obj_name, caller_info|
|
63
|
+
puts "#{obj_name} #{method_name} #{caller_info[0]}"
|
64
|
+
end
|
65
|
+
okuribito.apply("okuribito.yml")
|
66
|
+
|
67
|
+
TestTarget.deprecated_self_method
|
68
|
+
TestTarget.new.deprecated_method
|
69
|
+
```
|
70
|
+
|
71
|
+
Setting file:
|
60
72
|
|
61
|
-
|
62
|
-
|
73
|
+
```okuribito.yml
|
74
|
+
TestTarget:
|
75
|
+
- ".deprecated_self_method"
|
76
|
+
- "#deprecated_method"
|
63
77
|
|
64
|
-
|
65
|
-
|
78
|
+
```
|
79
|
+
|
80
|
+
Output:
|
66
81
|
|
67
|
-
|
82
|
+
```output
|
83
|
+
TestTarget deprecated_self_method example.rb:17:in `<main>'
|
84
|
+
#<TestTarget:0x007fd1e11ce368> deprecated_method example.rb:18:in `<main>'
|
85
|
+
```
|
68
86
|
|
69
|
-
|
87
|
+
## Callback examples
|
70
88
|
|
71
|
-
|
89
|
+
### Full stacktrace
|
72
90
|
|
73
|
-
|
91
|
+
```ruby
|
92
|
+
okuribito = Okuribito::OkuribitoPatch.new do |method_name, obj_name, caller_info|
|
93
|
+
puts "#############################################################"
|
94
|
+
puts "#{obj_name} #{method_name} #{caller_info[0]}"
|
95
|
+
puts "#############################################################"
|
96
|
+
puts caller_info
|
97
|
+
end
|
98
|
+
```
|
74
99
|
|
75
|
-
|
100
|
+
### Send to slack
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
okuribito = Okuribito::OkuribitoPatch.new do |method_name, obj_name, caller_info|
|
104
|
+
uri = URI.parse("https://hooks.slack.com/services/xxx...")
|
105
|
+
params = {
|
106
|
+
text: "OKURIBITO detected a method call.",
|
107
|
+
username: "OKURIBITO",
|
108
|
+
icon_emoji: ":innocent:",
|
109
|
+
attachments: [{
|
110
|
+
fields: [{
|
111
|
+
title: "#{obj_name}::#{method_name}",
|
112
|
+
value: "#{caller_info[0]}",
|
113
|
+
short: false
|
114
|
+
}]
|
115
|
+
}]
|
116
|
+
}
|
117
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
118
|
+
http.use_ssl = true
|
119
|
+
http.start do
|
120
|
+
request = Net::HTTP::Post.new(uri.path)
|
121
|
+
request.set_form_data(payload: params.to_json)
|
122
|
+
http.request(request)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
```
|
76
126
|
|
127
|
+
### Other ideas
|
128
|
+
- Send to Fluentd, TreasureData
|
77
129
|
|
78
130
|
## License
|
79
131
|
|
data/circle.yml
ADDED
data/lib/okuribito.rb
CHANGED
@@ -2,66 +2,18 @@ require "okuribito/version"
|
|
2
2
|
require "yaml"
|
3
3
|
require "active_support"
|
4
4
|
require "active_support/core_ext"
|
5
|
-
require "net/http"
|
6
|
-
require "uri"
|
7
|
-
require "json"
|
8
|
-
require "logger"
|
9
5
|
|
10
6
|
module Okuribito
|
11
7
|
class OkuribitoPatch
|
12
|
-
CLASS_METHOD_SYMBOL = "."
|
13
|
-
INSTANCE_METHOD_SYMBOL = "#"
|
8
|
+
CLASS_METHOD_SYMBOL = ".".freeze
|
9
|
+
INSTANCE_METHOD_SYMBOL = "#".freeze
|
14
10
|
PATTERN = /\A(?<symbol>[#{CLASS_METHOD_SYMBOL}#{INSTANCE_METHOD_SYMBOL}])(?<method_name>.+)\z/
|
15
11
|
|
16
|
-
def initialize(
|
17
|
-
@
|
12
|
+
def initialize(&callback)
|
13
|
+
@callback = callback
|
18
14
|
end
|
19
15
|
|
20
16
|
module PatchModule
|
21
|
-
def disp_console_by_okuribito(method_name, obj_name, caller_info, type)
|
22
|
-
case type
|
23
|
-
when "plane"
|
24
|
-
puts "#{obj_name} : #{method_name} is called."
|
25
|
-
when "back_trace"
|
26
|
-
puts "#############################################################"
|
27
|
-
puts "# #{obj_name} : #{method_name} is called."
|
28
|
-
puts "#############################################################"
|
29
|
-
puts caller_info
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def notificate_slack_by_okuribito(method_name, obj_name, caller_info, url)
|
34
|
-
uri = URI.parse(url)
|
35
|
-
params = {
|
36
|
-
text: "OKURIBITO detected a method call.",
|
37
|
-
username: "OKURIBITO",
|
38
|
-
icon_emoji: ":innocent:",
|
39
|
-
attachments: [{
|
40
|
-
fields: [{
|
41
|
-
title: "#{obj_name}::#{method_name}",
|
42
|
-
value: "#{caller_info[0]}",
|
43
|
-
short: false
|
44
|
-
}]
|
45
|
-
}]
|
46
|
-
}
|
47
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
48
|
-
http.use_ssl = true
|
49
|
-
http.start do
|
50
|
-
request = Net::HTTP::Post.new(uri.path)
|
51
|
-
request.set_form_data(payload: params.to_json)
|
52
|
-
http.request(request)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def logging_by_okuribito(symbol, method_name, obj_name, caller_info, log_path)
|
57
|
-
logger = Logger.new(log_path)
|
58
|
-
logger.formatter = proc{|severity, datetime, progname, message|
|
59
|
-
"#{datetime}, #{message}\n"
|
60
|
-
}
|
61
|
-
|
62
|
-
logger.info("#{obj_name}#{symbol}#{method_name} : #{caller_info[0]}")
|
63
|
-
end
|
64
|
-
|
65
17
|
def define_okuribito_patch(method_name)
|
66
18
|
define_method(method_name) do |*args|
|
67
19
|
yield(self.to_s, caller) if block_given?
|
@@ -74,12 +26,11 @@ module Okuribito
|
|
74
26
|
yaml = YAML.load_file(yaml_path)
|
75
27
|
yaml.each do |class_name, observe_methods|
|
76
28
|
patch_okuribito(class_name, observe_methods)
|
77
|
-
logging_prepended(class_name, observe_methods, @options[:first_prepended]) unless @options[:first_prepended].nil?
|
78
29
|
end
|
79
30
|
end
|
80
31
|
|
81
32
|
def patch_okuribito(class_name, observe_methods)
|
82
|
-
|
33
|
+
callback = @callback # スコープを乗り越えるために使用.
|
83
34
|
klass = class_name.constantize
|
84
35
|
|
85
36
|
klass.class_eval do
|
@@ -97,9 +48,7 @@ module Okuribito
|
|
97
48
|
next unless klass.instance_methods.include?(method_name)
|
98
49
|
instance_method_patch.module_eval do
|
99
50
|
define_okuribito_patch(method_name) do |obj_name, caller_info|
|
100
|
-
|
101
|
-
notificate_slack_by_okuribito(method_name, obj_name, caller_info, options[:slack]) unless options[:slack].nil?
|
102
|
-
logging_by_okuribito(symbol, method_name, obj_name, caller_info, options[:logging]) unless options[:logging].nil?
|
51
|
+
callback.call(method_name, obj_name, caller_info)
|
103
52
|
end
|
104
53
|
end
|
105
54
|
instance_method_patched += 1
|
@@ -107,9 +56,7 @@ module Okuribito
|
|
107
56
|
next unless klass.respond_to?(method_name)
|
108
57
|
class_method_patch.module_eval do
|
109
58
|
define_okuribito_patch(method_name) do |obj_name, caller_info|
|
110
|
-
|
111
|
-
notificate_slack_by_okuribito(method_name, obj_name, caller_info, options[:slack]) unless options[:slack].nil?
|
112
|
-
logging_by_okuribito(symbol, method_name, obj_name, caller_info, options[:logging]) unless options[:logging].nil?
|
59
|
+
callback.call(method_name, obj_name, caller_info)
|
113
60
|
end
|
114
61
|
end
|
115
62
|
class_method_patched += 1
|
@@ -119,26 +66,5 @@ module Okuribito
|
|
119
66
|
singleton_class.send(:prepend, class_method_patch) if class_method_patched > 0
|
120
67
|
end
|
121
68
|
end
|
122
|
-
|
123
|
-
def logging_prepended(class_name, observe_methods, log_path)
|
124
|
-
# ファイル読み出して、クラス名+メソッド名だけを配列に保持する.
|
125
|
-
methods = []
|
126
|
-
if File.exist?(log_path)
|
127
|
-
File.open(log_path) { |f| methods = f.read.split("\n") }
|
128
|
-
methods.slice!(0)
|
129
|
-
methods.map! { |m| m.split(",")[1] }
|
130
|
-
end
|
131
|
-
|
132
|
-
logger = Logger.new(log_path)
|
133
|
-
logger.formatter = proc{|severity, datetime, progname, message|
|
134
|
-
"#{datetime},#{message}\n"
|
135
|
-
}
|
136
|
-
|
137
|
-
observe_methods.each do |observe_method|
|
138
|
-
method_full_name = "#{class_name}#{observe_method}"
|
139
|
-
logger.info(method_full_name) unless methods.include?(method_full_name)
|
140
|
-
end
|
141
|
-
|
142
|
-
end
|
143
69
|
end
|
144
|
-
end
|
70
|
+
end
|
data/lib/okuribito/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: okuribito
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- muramurasan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -90,11 +90,11 @@ files:
|
|
90
90
|
- .gitignore
|
91
91
|
- .rspec
|
92
92
|
- CODE_OF_CONDUCT.md
|
93
|
+
- Gemfile
|
93
94
|
- LICENSE.txt
|
94
95
|
- README.md
|
95
96
|
- Rakefile
|
96
|
-
-
|
97
|
-
- bin/setup
|
97
|
+
- circle.yml
|
98
98
|
- lib/okuribito.rb
|
99
99
|
- lib/okuribito/version.rb
|
100
100
|
- okuribito.gemspec
|
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require "bundler/setup"
|
4
|
-
require "okuribito"
|
5
|
-
|
6
|
-
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
-
# with your gem easier. You can also use a different console, if you like.
|
8
|
-
|
9
|
-
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
# require "pry"
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start
|