check_and_notify 0.1.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: ff504b937b2e28a12d1217501bbaa6855079de84
4
+ data.tar.gz: 166a39634b1833be0d373929e52a4ddf04167d48
5
+ SHA512:
6
+ metadata.gz: 922e694598210919953a91263e757b4ae4ea70d43e4002b2429fcad6248f79b4174a6d83b5980f536b5e289c6fa4282a32f691d924551b010718b6c92842438c
7
+ data.tar.gz: 52290a8a4b1591d9c830ec0ae82cbf60eb630493b92c3c262af64fed5f027aa0675f6c4b26925a92bf7941dd220ce438980cefdd5943d9a3cb1c48eb6d3437a9
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/CHANGELOG.md ADDED
File without changes
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in check_and_notify.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 TODO: Write your name
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,74 @@
1
+ # Check And Notify
2
+
3
+ An easy to use regular checker and notifier via Slack without needing to setup cron jobs and rake tasks.
4
+ This gem uses Sidekiq and Sidekiq Cron for asynchronous execution of jobs.
5
+
6
+ In the future, other medium of notifications will be implemented like Email and SMS.
7
+
8
+ ### Installation
9
+
10
+ ```sh
11
+ $ gem install check_and_notify
12
+ ```
13
+
14
+ ### Usage
15
+
16
+ In your initializer, say for example, `check_and_notify.rb`, write a callback that will be called every ten minutes and then send a notification:
17
+
18
+ ```ruby
19
+ require 'check_and_notify'
20
+
21
+ CheckAndNotify::Callbacks.check_after_ten_minutes do
22
+ # Do anything here
23
+ message = "Hello World"
24
+
25
+ {
26
+ slack_webhook_url: "https://hooks.slack.com/services/111/222/abcd",
27
+ message: message
28
+ }
29
+ end
30
+ end
31
+
32
+ CheckAndNotify.init_cron
33
+ ```
34
+
35
+ Remember that the return of the callback must be of the form: `{slack_webhook_url: "", message: ""}`.
36
+ Also remember to call `CheckAndNotify.init_cron` to initialize Sidekiq crons.
37
+
38
+ You can add multiple callbacks if you want:
39
+
40
+ ```ruby
41
+ require 'check_and_notify'
42
+
43
+ CheckAndNotify::Callbacks.check_after_ten_minutes do
44
+ # Do anything here
45
+ message = "Hello James"
46
+
47
+ {
48
+ slack_webhook_url: "https://hooks.slack.com/services/333/444/abcd",
49
+ message: message
50
+ }
51
+ end
52
+ end
53
+
54
+ CheckAndNotify::Callbacks.check_after_ten_minutes do
55
+ # Do anything here
56
+ message = "Hello World"
57
+
58
+ {
59
+ slack_webhook_url: "https://hooks.slack.com/services/111/222/abcd",
60
+ message: message
61
+ }
62
+ end
63
+ end
64
+
65
+ CheckAndNotify.init_cron
66
+ ```
67
+
68
+ ### Todos
69
+
70
+ - Write checkers after every five minutes, fifteen minutes, thirty minutes, and so forth as long as there's a demand for it
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "check_and_notify"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'check_and_notify/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "check_and_notify"
8
+ spec.version = CheckAndNotify::VERSION
9
+ spec.authors = ["Neil Marion dela Cruz"]
10
+ spec.email = ["nmfdelacruz@gmail.com"]
11
+
12
+ spec.summary = %q{Calls a callback regularly and notifies when it returns true}
13
+ spec.description = %q{Calls a callback regularly and notifies when it returns true}
14
+ spec.homepage = "https://github.com/carabao-capital/check_and_notify"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency 'activesupport', '~> 4.2', '>= 4.2.0'
23
+ spec.add_dependency "sidekiq", "5.0.4"
24
+ spec.add_dependency "sidekiq-cron", "0.6.3"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.14"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "pry-nav", "~> 0.2.4"
30
+ end
@@ -0,0 +1,18 @@
1
+ require "active_support"
2
+ require "httparty"
3
+ require "sidekiq"
4
+ require "sidekiq-cron"
5
+
6
+ require "check_and_notify/version"
7
+ require "check_and_notify/cron"
8
+ require "check_and_notify/callbacks"
9
+ require "check_and_notify/notifiers/slack"
10
+ require "check_and_notify/workers/slack_notifier_worker"
11
+ require "check_and_notify/workers/check_every_hour_worker"
12
+ require "check_and_notify/workers/check_every_ten_minutes_worker"
13
+
14
+ module CheckAndNotify
15
+ def self.init_cron
16
+ Cron.init
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ module CheckAndNotify
2
+ module Callbacks
3
+ mattr_accessor :check_after_one_hour_callbacks
4
+ mattr_accessor :check_after_ten_minutes_callbacks
5
+ self.check_after_one_hour_callbacks = []
6
+ self.check_after_ten_minutes_callbacks = []
7
+
8
+ def self.check_after_one_hour(&block)
9
+ self.check_after_one_hour_callbacks << block
10
+ end
11
+
12
+ def self.check_after_ten_minutes(&block)
13
+ self.check_after_ten_minutes_callbacks << block
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,26 @@
1
+ module CheckAndNotify
2
+ class Cron
3
+ def self.init
4
+ init_every_hour_cron
5
+ init_every_ten_minutes_cron
6
+ end
7
+
8
+ private
9
+
10
+ def self.init_every_hour_cron
11
+ Sidekiq::Cron::Job.create({
12
+ name: 'CheckEveryHourWorker',
13
+ cron: '0 * * * *',
14
+ class: 'CheckAndNotify::Workers::CheckEveryHourWorker'
15
+ })
16
+ end
17
+
18
+ def self.init_every_ten_minutes_cron
19
+ Sidekiq::Cron::Job.create({
20
+ name: 'CheckEveryTenMinutesWorker',
21
+ cron: '*/10 * * * *',
22
+ class: 'CheckAndNotify::Workers::CheckEveryTenMinutesWorker'
23
+ })
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ module CheckAndNotify
2
+ module Notifiers
3
+ class Slack
4
+ HEADERS = {'Content-Type' => 'application/json'}
5
+
6
+ def self.notify(params)
7
+ @slack_webhook_url = params[:slack_webhook_url]
8
+ @message = params[:message]
9
+
10
+ post
11
+ end
12
+
13
+ private
14
+
15
+ def self.post
16
+ body = {text: @message}.to_json
17
+
18
+ HTTParty.post(@slack_webhook_url,
19
+ headers: HEADERS,
20
+ body: body,
21
+ )
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module CheckAndNotify
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ module CheckAndNotify
2
+ module Workers
3
+ class CheckEveryHourWorker
4
+ include Sidekiq::Worker
5
+ sidekiq_options retry: 1
6
+
7
+ def perform
8
+ callbacks_size = CheckAndNotify::Callbacks.
9
+ check_after_one_hour_callbacks.count
10
+
11
+ callbacks_size.times do |index|
12
+ SlackNotifierWorker.perform_async(
13
+ index, SlackNotifierWorker::EVERY_HOUR
14
+ )
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module CheckAndNotify
2
+ module Workers
3
+ class CheckEveryTenMinutesWorker
4
+ include Sidekiq::Worker
5
+ sidekiq_options retry: 1
6
+
7
+ def perform
8
+ callbacks_size = CheckAndNotify::Callbacks.
9
+ check_after_ten_minutes_callbacks.count
10
+
11
+ callbacks_size.times do |index|
12
+ SlackNotifierWorker.perform_async(
13
+ index, SlackNotifierWorker::EVERY_TEN_MINUTES
14
+ )
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ module CheckAndNotify
2
+ module Workers
3
+ class SlackNotifierWorker
4
+ include Sidekiq::Worker
5
+ sidekiq_options retry: 1
6
+
7
+ EVERY_HOUR = 3600
8
+ EVERY_TEN_MINUTES = 600
9
+
10
+ def perform(callback_index, callbacks_array_type)
11
+ callback = case callbacks_array_type
12
+ when EVERY_HOUR
13
+ CheckAndNotify::Callbacks.
14
+ check_after_one_hour_callbacks[callback_index]
15
+ when EVERY_TEN_MINUTES
16
+ CheckAndNotify::Callbacks.
17
+ check_after_ten_minutes_callbacks[callback_index]
18
+ end
19
+
20
+ result = callback.call
21
+ Notifiers::Slack.notify(result) if result
22
+ end
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: check_and_notify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Neil Marion dela Cruz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-14 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.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 4.2.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 4.2.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: sidekiq
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '='
38
+ - !ruby/object:Gem::Version
39
+ version: 5.0.4
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '='
45
+ - !ruby/object:Gem::Version
46
+ version: 5.0.4
47
+ - !ruby/object:Gem::Dependency
48
+ name: sidekiq-cron
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '='
52
+ - !ruby/object:Gem::Version
53
+ version: 0.6.3
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '='
59
+ - !ruby/object:Gem::Version
60
+ version: 0.6.3
61
+ - !ruby/object:Gem::Dependency
62
+ name: bundler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.14'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.14'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '10.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '10.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rspec
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '3.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: pry-nav
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 0.2.4
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 0.2.4
117
+ description: Calls a callback regularly and notifies when it returns true
118
+ email:
119
+ - nmfdelacruz@gmail.com
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - ".gitignore"
125
+ - ".rspec"
126
+ - ".travis.yml"
127
+ - CHANGELOG.md
128
+ - CODE_OF_CONDUCT.md
129
+ - Gemfile
130
+ - LICENSE.txt
131
+ - README.md
132
+ - Rakefile
133
+ - bin/console
134
+ - bin/setup
135
+ - check_and_notify.gemspec
136
+ - lib/check_and_notify.rb
137
+ - lib/check_and_notify/callbacks.rb
138
+ - lib/check_and_notify/cron.rb
139
+ - lib/check_and_notify/notifiers/slack.rb
140
+ - lib/check_and_notify/version.rb
141
+ - lib/check_and_notify/workers/check_every_hour_worker.rb
142
+ - lib/check_and_notify/workers/check_every_ten_minutes_worker.rb
143
+ - lib/check_and_notify/workers/slack_notifier_worker.rb
144
+ homepage: https://github.com/carabao-capital/check_and_notify
145
+ licenses:
146
+ - MIT
147
+ metadata: {}
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ requirements: []
163
+ rubyforge_project:
164
+ rubygems_version: 2.6.11
165
+ signing_key:
166
+ specification_version: 4
167
+ summary: Calls a callback regularly and notifies when it returns true
168
+ test_files: []