cronitor 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: c1325aef8ddc179aaa172908cfce6f00fe3f4fa6
4
+ data.tar.gz: ac5be4a04f28f3adf09ea31df6e19f0c38aad83e
5
+ SHA512:
6
+ metadata.gz: 7ce111c27528f60d5bee4eba5022cd53b7a9c3c0850a5912e321135a1dd8d021f44b06b93c8b5815c27dc1762e7e5c30fb3a940a308d78e987899dc54e5759e3
7
+ data.tar.gz: ed7571b3ba8bddba5fdf3c9a5c6067b1f370ce1965dcb17a8ebb2e8b35280e8524332004c4e1b8cfe6f509f26e812e31a54482c223b6b294743b2250084b1008
data/.editorconfig ADDED
@@ -0,0 +1,18 @@
1
+ ; EditorConfig is awesome: http://EditorConfig.org
2
+
3
+ root = true
4
+
5
+ ; UTF-8 charset
6
+ ; Unix-style newlines with a newline ending every file
7
+ ; 4 space indent
8
+ ; Trim trailing whitespace
9
+ [*]
10
+ indent_style = space
11
+ indent_size = 2
12
+ end_of_line = lf
13
+ charset = utf-8
14
+ trim_trailing_whitespace = true
15
+ insert_final_newline = true
16
+
17
+ [*.md]
18
+ indent_size = 4
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
+ /spec/examples.txt
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cronitor.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Eric Herot
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # Cronitor
2
+
3
+ [Cronitor](https://cronitor.io/) is a service for heartbeat-style monitoring of just about anything that can send an HTTP request.
4
+
5
+ This gem provides a simple abstraction for the creation and pinging of a Cronitor monitor. For a better understanding of the API this gem talks to, please see [How Cronitor Works](https://cronitor.io/help/how-cronitor-works).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cronitor'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cronitor
22
+
23
+ ## Usage
24
+
25
+ ### Creating a Monitor
26
+
27
+ A Cronitor monitor (hereafter referred to only as a monitor for brevity) is created if it does not already exist, and its ID returned.
28
+
29
+ ```ruby
30
+ require 'cronitor'
31
+
32
+ monitor_options = {
33
+ name: 'My Fancy Monitor',
34
+ notifications: {
35
+ emails: [],
36
+ slack: [],
37
+ pagerduty: [],
38
+ phones: [],
39
+ webhooks: []
40
+ },
41
+ rules: [
42
+ {
43
+ rule_type: 'not_run_in',
44
+ duration: 5
45
+ time_unit: 'seconds'
46
+ }
47
+ ],
48
+ note: 'A human-friendly description of this monitor'
49
+ }
50
+ my_monitor = Cronitor.new token: 'api_token', opts: monitor_options
51
+ ```
52
+
53
+ ### Pinging a Monitor
54
+
55
+ Once you’ve created a monitor, you can continue to use the existing instance of the object to ping the monitor that your task status: `run`, `complete`, or `fail`.
56
+
57
+ ```ruby
58
+ my_monitor.run
59
+ my_monitor.complete
60
+ my_monitor.fail 'A short description of the failure'
61
+ ```
62
+
63
+ ### Pinging a monitor when you have a Cronitor code
64
+
65
+ You may already have the code for a monitor, in which case, the expense of `Cronitor.new` may seem unnecessary (since it makes an HTTP request to check if a monitor exists, and you already know it does).
66
+
67
+ In that case:
68
+
69
+ ```ruby
70
+ my_monitor = Cronitor.new code: 'abcd'
71
+ ```
72
+
73
+ The aforementioned ping methods can now be used.
74
+
75
+ ## Development
76
+
77
+ 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.
78
+
79
+ 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).
80
+
81
+ ## Contributing
82
+
83
+ 1. Fork it ( https://github.com/evertrue/cronitor/fork )
84
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
85
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
86
+ 4. Push to the branch (`git push origin my-new-feature`)
87
+ 5. Create a new Pull Request
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,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'cronitor'
5
+
6
+ require 'pry'
7
+ Pry.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
data/cronitor.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'cronitor/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'cronitor'
7
+ spec.version = Cronitor::VERSION
8
+ spec.authors = ['Jeff Byrnes']
9
+ spec.email = ['thejeffbyrnes@gmail.com']
10
+
11
+ spec.summary = 'An interface for the Cronitor API'
12
+ spec.homepage = 'https://github.com/evertrue/cronitor'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
15
+ f.match(%r{^(test|spec|features)/})
16
+ end
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_dependency 'unirest', '~> 1.1'
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.10'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.3'
24
+ spec.add_development_dependency 'pry', '~> 0.10'
25
+ spec.add_development_dependency 'webmock', '~> 1.21'
26
+ spec.add_development_dependency 'sinatra', '~> 1.4'
27
+ end
data/lib/cronitor.rb ADDED
@@ -0,0 +1,69 @@
1
+ require 'cronitor/version'
2
+ require 'cronitor/error'
3
+ require 'net/http'
4
+ require 'unirest'
5
+
6
+ Unirest.default_header 'Accept', 'application/json'
7
+ Unirest.default_header 'Content-Type', 'application/json'
8
+
9
+ class Cronitor
10
+ attr_accessor :token, :opts, :code
11
+ API_URL = 'https://cronitor.io/v1'
12
+
13
+ def initialize(token: nil, opts: {}, code: nil)
14
+ @token = token
15
+ @opts = opts
16
+ @code = code
17
+
18
+ # TODO Update this to allow for tokenless usage w/ a code for an already
19
+ # created monitor
20
+ fail Cronitor::Error, 'Missing Cronitor API token' if token.nil?
21
+
22
+ create if code.nil?
23
+ end
24
+
25
+ def create
26
+ response = Unirest.post(
27
+ "#{API_URL}/monitors",
28
+ auth: { user: token },
29
+ parameters: opts.to_json
30
+ )
31
+
32
+ validate response
33
+ @code = response.body['code']
34
+ end
35
+
36
+ def validate(response)
37
+ return if response.code == 200
38
+ server_error? response
39
+
40
+ fail Cronitor::Error, error_msg(response.body)
41
+ end
42
+
43
+ private
44
+
45
+ def error_msg(body)
46
+ msg = []
47
+
48
+ body.each do |opt, value|
49
+ if value.respond_to? 'each'
50
+ value.each do |error_msg|
51
+ msg << "#{opt}: #{error_msg}"
52
+ end
53
+ else
54
+ msg << "#{opt}: #{value}"
55
+ end
56
+ end
57
+
58
+ msg.join ' '
59
+ end
60
+
61
+ def server_error?(response)
62
+ return unless [301, 302, 500, 502, 503, 504].include? response.code
63
+
64
+ fail(
65
+ Cronitor::Error,
66
+ "Something else has gone awry. HTTP status: #{response.code}"
67
+ )
68
+ end
69
+ end
@@ -0,0 +1,4 @@
1
+ class Cronitor
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ class Cronitor
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cronitor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Byrnes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: unirest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.21'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.21'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sinatra
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.4'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.4'
111
+ description:
112
+ email:
113
+ - thejeffbyrnes@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".editorconfig"
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - cronitor.gemspec
129
+ - lib/cronitor.rb
130
+ - lib/cronitor/error.rb
131
+ - lib/cronitor/version.rb
132
+ homepage: https://github.com/evertrue/cronitor
133
+ licenses: []
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.4.6
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: An interface for the Cronitor API
155
+ test_files: []