okuribito 0.0.1

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: 554a0e1bcd2a985c3f08ec493efad06bf0b58b95
4
+ data.tar.gz: 3a419fbe576efd5143466f5b1aecf084378c2a68
5
+ SHA512:
6
+ metadata.gz: 46165e2cceca9ad323f05fb647fe40c4c717362462ce4dbd1832cd4f7da8cc529a284f193c5ac35908bd32b76a7e566b1682dca2542f78a7e55b1584572d7f2d
7
+ data.tar.gz: f62c2bf61de45c6cc502d24d06b1fe8fbad45628200889c9f43a6ea55f5bb449ab275933dc9073cc64b8d1cf48462bbd35219eea8a076ba66cb9eaa30d61ed8e
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at yasuhiro.matsumura@pixta.co.jp. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 yasuhiro_matsumura
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # Okuribito
2
+
3
+ ![okuribito](okuribito_logo.png)
4
+
5
+ Okuribito is a gem to judge whether methods should be sent to the heaven :innocent:.
6
+ Okuribito was named after a japanese movie.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'okuribito'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install okuribito
23
+
24
+ ## Usage
25
+
26
+ Add `config/okuribito.yml` and edit it.
27
+
28
+ ```yml
29
+ User:
30
+ - '#feed'
31
+ Micropost:
32
+ - '.from_users_followed_by'
33
+ ```
34
+
35
+ Edit `application.rb`
36
+
37
+ ```ruby
38
+ class OkuribitoSetting < Rails::Railtie
39
+ config.after_initialize do
40
+ okuribito = Okuribito::OkuribitoPatch.new(
41
+ {
42
+ console: "back_trace",
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
+ )
48
+ okuribito.apply("okuribito.yml")
49
+ end
50
+ end
51
+ ```
52
+
53
+ ### console
54
+ Setting for console outout.
55
+ - `plain` is the simplest 1 line log.
56
+ - `back_trace` shows back trace in detail.
57
+
58
+ ### slack
59
+ Setting for slack notification.
60
+
61
+ ### logging
62
+ Setting for logging.
63
+
64
+ ### first_prepended
65
+ Setting for logging to save when you started to monitor.
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
+
71
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/okuribito. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
76
+
77
+
78
+ ## License
79
+
80
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
81
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
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
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module Okuribito
2
+ VERSION = "0.0.1"
3
+ end
data/lib/okuribito.rb ADDED
@@ -0,0 +1,144 @@
1
+ require "okuribito/version"
2
+ require "yaml"
3
+ require "active_support"
4
+ require "active_support/core_ext"
5
+ require "net/http"
6
+ require "uri"
7
+ require "json"
8
+ require "logger"
9
+
10
+ module Okuribito
11
+ class OkuribitoPatch
12
+ CLASS_METHOD_SYMBOL = "."
13
+ INSTANCE_METHOD_SYMBOL = "#"
14
+ PATTERN = /\A(?<symbol>[#{CLASS_METHOD_SYMBOL}#{INSTANCE_METHOD_SYMBOL}])(?<method_name>.+)\z/
15
+
16
+ def initialize(options = {})
17
+ @options = options
18
+ end
19
+
20
+ 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
+ def define_okuribito_patch(method_name)
66
+ define_method(method_name) do |*args|
67
+ yield(self.to_s, caller) if block_given?
68
+ super(*args)
69
+ end
70
+ end
71
+ end
72
+
73
+ def apply(yaml_path)
74
+ yaml = YAML.load_file(yaml_path)
75
+ yaml.each do |class_name, observe_methods|
76
+ patch_okuribito(class_name, observe_methods)
77
+ logging_prepended(class_name, observe_methods, @options[:first_prepended]) unless @options[:first_prepended].nil?
78
+ end
79
+ end
80
+
81
+ def patch_okuribito(class_name, observe_methods)
82
+ options = @options # スコープを乗り越えるために使用.
83
+ klass = class_name.constantize
84
+
85
+ klass.class_eval do
86
+ instance_method_patch = Module.new.extend(PatchModule)
87
+ class_method_patch = Module.new.extend(PatchModule)
88
+ instance_method_patched = 0
89
+ class_method_patched = 0
90
+
91
+ observe_methods.each do |observe_method|
92
+ next unless md = PATTERN.match(observe_method)
93
+ symbol, method_name = md[:symbol], md[:method_name].to_sym
94
+
95
+ case symbol
96
+ when INSTANCE_METHOD_SYMBOL
97
+ next unless klass.instance_methods.include?(method_name)
98
+ instance_method_patch.module_eval do
99
+ define_okuribito_patch(method_name) do |obj_name, caller_info|
100
+ disp_console_by_okuribito(method_name, obj_name, caller_info, options[:console]) unless options[:console].nil?
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?
103
+ end
104
+ end
105
+ instance_method_patched += 1
106
+ when CLASS_METHOD_SYMBOL
107
+ next unless klass.respond_to?(method_name)
108
+ class_method_patch.module_eval do
109
+ define_okuribito_patch(method_name) do |obj_name, caller_info|
110
+ disp_console_by_okuribito(method_name, obj_name, caller_info, options[:console]) unless options[:console].nil?
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?
113
+ end
114
+ end
115
+ class_method_patched += 1
116
+ end
117
+ end
118
+ prepend instance_method_patch if instance_method_patched > 0
119
+ singleton_class.send(:prepend, class_method_patch) if class_method_patched > 0
120
+ end
121
+ 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
+ end
144
+ end
data/okuribito.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'okuribito/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "okuribito"
8
+ spec.version = Okuribito::VERSION
9
+ spec.authors = ["muramurasan"]
10
+ spec.email = ["ym.works1985@gmail.com"]
11
+
12
+ spec.summary = "Monitoring system of the method call"
13
+ spec.description = "Okuribito monitors the method call by the yaml."
14
+ spec.homepage = "https://github.com/muramurasan/okuribito"
15
+ spec.license = "MIT"
16
+
17
+ spec.required_ruby_version = '>= 2.0.0'
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ if spec.respond_to?(:metadata)
22
+ # spec.metadata['allowed_push_host'] = "http://mygemserver.com"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
25
+ end
26
+
27
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency "activesupport", "~> 4.0"
33
+ spec.add_dependency "json", "~> 1.0"
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.12"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.0"
38
+ end
Binary file
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: okuribito
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - muramurasan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Okuribito monitors the method call by the yaml.
84
+ email:
85
+ - ym.works1985@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - CODE_OF_CONDUCT.md
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - lib/okuribito.rb
99
+ - lib/okuribito/version.rb
100
+ - okuribito.gemspec
101
+ - okuribito_logo.png
102
+ homepage: https://github.com/muramurasan/okuribito
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: 2.0.0
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.0.14.1
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Monitoring system of the method call
126
+ test_files: []