cafmal 0.21.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 85bf93918888b671597451ccd0e07c06f0f2aee6
4
+ data.tar.gz: abbb22706f9e46df530be4d75d99a17cc5f8b4d3
5
+ SHA512:
6
+ metadata.gz: 63b8f647b9a58f7f31c75d6ae59918f739aaabc280fd37de3f736e73daee069bc7d1fce9fd7f97cb19321e42db65b27a29b0cdcba4a041d189f730c194bc7cf2
7
+ data.tar.gz: 9427d711729984c6dcd04af845012bb4b9bf6c77b50e42270e9679ec26b802b3de3d3e7c46965a19acf7132e92346545b6c5c45f0a14c0b65188b576104a800a
data/.gitignore ADDED
@@ -0,0 +1,54 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+ Gemfile.lock
13
+
14
+ # Used by dotenv library to load environment variables.
15
+ # .env
16
+
17
+ ## Specific to RubyMotion:
18
+ .dat*
19
+ .repl_history
20
+ build/
21
+ *.bridgesupport
22
+ build-iPhoneOS/
23
+ build-iPhoneSimulator/
24
+
25
+ ## Specific to RubyMotion (use of CocoaPods):
26
+ #
27
+ # We recommend against adding the Pods directory to your .gitignore. However
28
+ # you should judge for yourself, the pros and cons are mentioned at:
29
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
30
+ #
31
+ # vendor/Pods/
32
+
33
+ ## Documentation cache and generated files:
34
+ /.yardoc/
35
+ /_yardoc/
36
+ /doc/
37
+ /rdoc/
38
+
39
+ ## Environment normalization:
40
+ /.bundle/
41
+ /vendor/bundle
42
+ /lib/bundler/man/
43
+
44
+ # for a library or gem, you might want to ignore these files since the code is
45
+ # intended to run in multiple environments; otherwise, check them in:
46
+ # Gemfile.lock
47
+ # .ruby-version
48
+ # .ruby-gemset
49
+
50
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
51
+ .rvmrc
52
+
53
+ # rbenv
54
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cafmal.gemspec
4
+ gemspec
5
+
6
+ gem 'rubocop'
7
+ gem 'rake'
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Nils Bartels
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Cafmal
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cafmal`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'cafmal'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install cafmal
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/brtz/cafmal-lib-ruby.
36
+
37
+
38
+ ## License
39
+
40
+ 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,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 "cafmal"
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
data/cafmal.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cafmal/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cafmal"
8
+ spec.version = Cafmal::VERSION
9
+ spec.authors = ["Nils Bartels"]
10
+ spec.email = ["gem-cafmal@schrohm.de"]
11
+
12
+ spec.summary = %q{Create alerts from metrics and logs}
13
+ spec.description = %q{The cafmal library in ruby}
14
+ spec.homepage = "https://rubygems.org/gems/cafmal"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+
28
+ spec.add_dependency "httparty", "~> 0.14.0"
29
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ module Cafmal
4
+ class Alert < Resource
5
+
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ module Cafmal
4
+ class Alerter < Resource
5
+
6
+ end
7
+ end
@@ -0,0 +1,81 @@
1
+ # encoding: utf-8
2
+
3
+ module Cafmal
4
+ class Auth
5
+ @token = nil
6
+ @decoded_token = nil
7
+ @cafmal_api_url = nil
8
+
9
+ attr_reader :token
10
+ attr_reader :decoded_token
11
+ attr_reader :cafmal_api_url
12
+
13
+ def initialize(api_url)
14
+ @cafmal_api_url = api_url
15
+ end
16
+
17
+ def expired?(force = false)
18
+ is_expired = false
19
+ if @token.nil?
20
+ is_expired = true
21
+ else
22
+ is_expired = (Time.at(@decoded_token['payload']['exp']).utc.to_datetime < Time.now().utc.to_datetime)
23
+ # force is checking against auth from the api itself instead of relying on JWT exp
24
+ if force
25
+ request_user = JSON.parse(Cafmal::User.new(@cafmal_api_url, @token).show(@decoded_token['payload']['sub']))
26
+ is_expired = request_user.nil?
27
+ end
28
+ end
29
+ return is_expired
30
+ end
31
+
32
+ def login(email = 'admin@example.com', password = 'cafmal')
33
+ credentials = {auth: {email: email, password: password}}.to_json
34
+ request_auth = Cafmal::Request::Post.new(@cafmal_api_url + '/user_token', credentials, {"Content-Type" => "application/json"})
35
+ if request_auth.code < 300
36
+ @token = JSON.parse(request_auth.response.body)["jwt"]
37
+ @decoded_token = {}
38
+ @decoded_token['header'] = JSON.parse(Base64.decode64(@token.split('.')[0]))
39
+ @decoded_token['payload'] = JSON.parse(Base64.decode64(@token.split('.')[1]))
40
+
41
+ team_id = JSON.parse(Cafmal::User.new(@cafmal_api_url, @token).show(@decoded_token['payload']['sub']))["team_id"]
42
+
43
+ event = Cafmal::Event.new(@cafmal_api_url, @token)
44
+ event.create({name: 'user.login', message: "#{email} has logged in.", kind: 'login', severity: 'info', team_id: team_id})
45
+
46
+ #@TODO silence all alerts for your team_id, set silenced_at now + 1h
47
+
48
+ true
49
+ end
50
+ end
51
+
52
+ # we supply the token here, so web does not have to cache the auth obj
53
+ def logout(token)
54
+ unless token.nil?
55
+ headers = {"Content-Type" => "application/json", "Authorization" => "Bearer #{token}"}
56
+
57
+ decoded_token = {}
58
+ decoded_token['header'] = JSON.parse(Base64.decode64(token.split('.')[0]))
59
+ decoded_token['payload'] = JSON.parse(Base64.decode64(token.split('.')[1]))
60
+
61
+ user = JSON.parse(Cafmal::User.new(@cafmal_api_url, token).show(decoded_token['payload']['sub']))
62
+ team_id = user["team_id"]
63
+ email = user["email"]
64
+
65
+ #@TODO if you are the last logged in user, unsilence your team alerts
66
+
67
+ # kind has to be login, as it's a label of events
68
+ event_id = JSON.parse(Cafmal::Event.new(@cafmal_api_url, token).create({name: 'user.logout', message: "#{email} has logged out.", kind: 'login', severity: 'info', team_id: team_id}))
69
+
70
+ if event_id.nil?
71
+ false
72
+ else
73
+ @token = nil
74
+ @decoded_token = nil
75
+ true
76
+ end
77
+ end
78
+
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ module Cafmal
4
+ class Check < Resource
5
+
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ module Cafmal
4
+ class Datasource < Resource
5
+
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+ module Cafmal
4
+ class Event < Resource
5
+
6
+ def list(age = nil, duration = 3600)
7
+ @query = age.nil? ? "" : "?query[age]=#{age}&query[duration]=#{duration}"
8
+
9
+ super
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ module Cafmal
4
+ module Request
5
+ @response = nil
6
+ @code = nil
7
+
8
+ class Post
9
+ attr_reader :response
10
+ attr_reader :code
11
+
12
+ def initialize(url, body, headers)
13
+ @response = HTTParty.post(url, body: body, headers: headers)
14
+ @code = @response.code
15
+ end
16
+ end
17
+
18
+ class Get
19
+ attr_reader :response
20
+ attr_reader :code
21
+
22
+ def initialize(url, headers)
23
+ @response = HTTParty.get(url, headers: headers)
24
+ @code = @response.code
25
+ end
26
+ end
27
+
28
+ class Put
29
+ attr_reader :response
30
+ attr_reader :code
31
+
32
+ def initialize(url, body, headers)
33
+ @response = HTTParty.put(url, body: body, headers: headers)
34
+ @code = @response.code
35
+ end
36
+ end
37
+
38
+ class Delete
39
+ attr_reader :response
40
+ attr_reader :code
41
+
42
+ def initialize(url, body, headers)
43
+ @response = HTTParty.delete(url, body: body, headers: headers)
44
+ @code = @response.code
45
+ end
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,77 @@
1
+ # encoding: utf-8
2
+
3
+ module Cafmal
4
+ class Resource
5
+ @token = nil
6
+ @decoded_token = nil
7
+ @cafmal_api_url = nil
8
+ @query = nil
9
+ @headers = nil
10
+ @resourcename = nil
11
+
12
+ attr_reader :token
13
+ attr_reader :decoded_token
14
+ attr_reader :cafmal_api_url
15
+ attr_reader :query
16
+ attr_writer :query
17
+
18
+ def initialize(cafmal_api_url, token)
19
+ @cafmal_api_url = cafmal_api_url
20
+ @token = token
21
+ @decoded_token = {}
22
+ @decoded_token['header'] = JSON.parse(Base64.decode64(@token.split('.')[0]))
23
+ @decoded_token['payload'] = JSON.parse(Base64.decode64(@token.split('.')[1]))
24
+ @query = ""
25
+ @headers = {"Content-Type" => "application/json", "Authorization" => "Bearer #{@token}"}
26
+ @resourcename = (demodulize(self.class.name) + "s").downcase
27
+ end
28
+
29
+ def create(params)
30
+ request_create_resource = Cafmal::Request::Post.new(@cafmal_api_url + "/#{@resourcename}", params.to_json, @headers)
31
+
32
+ return request_create_resource.response.body
33
+ end
34
+
35
+ def new
36
+ request_new_resource = Cafmal::Request::Get.new(@cafmal_api_url + "/#{@resourcename}/new", @headers)
37
+
38
+ return request_new_resource.response.body
39
+ end
40
+
41
+ def list(*options)
42
+ request_list_resource = Cafmal::Request::Get.new(@cafmal_api_url + "/#{@resourcename}" + @query, @headers)
43
+
44
+ return request_list_resource.response.body
45
+ end
46
+
47
+ def show(id)
48
+ request_show_resource = Cafmal::Request::Get.new(@cafmal_api_url + "/#{@resourcename}/" + id.to_s, @headers)
49
+
50
+ return request_show_resource.response.body
51
+ end
52
+
53
+ def update(params)
54
+ request_update_resource = Cafmal::Request::Put.new(@cafmal_api_url + "/#{@resourcename}/#{params['id']}", params.to_json, @headers)
55
+
56
+ return request_update_resource.response.body
57
+ end
58
+
59
+ def destroy(params)
60
+ request_destroy_resource = Cafmal::Request::Delete.new(@cafmal_api_url + "/#{@resourcename}/#{params['id']}", params.to_json, @headers)
61
+
62
+ return request_destroy_resource.response.body
63
+ end
64
+
65
+ # helpers below
66
+
67
+ def demodulize(path)
68
+ path = path.to_s
69
+ if i = path.rindex('::')
70
+ path[(i+2)..-1]
71
+ else
72
+ path
73
+ end
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ module Cafmal
4
+ class Team < Resource
5
+
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ module Cafmal
4
+ class User < Resource
5
+
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ module Cafmal
4
+ VERSION = "0.21.0"
5
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ module Cafmal
4
+ class Worker < Resource
5
+
6
+ end
7
+ end
data/lib/cafmal.rb ADDED
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ require 'httparty'
4
+ require 'cafmal/version'
5
+
6
+ module Cafmal
7
+ autoload 'Alert', 'cafmal/alert'
8
+ autoload 'Alerter', 'cafmal/alerter'
9
+ autoload 'Auth', 'cafmal/auth'
10
+ autoload 'Check', 'cafmal/check'
11
+ autoload 'Datasource', 'cafmal/datasource'
12
+ autoload 'Event', 'cafmal/event'
13
+ autoload 'Team', 'cafmal/team'
14
+ autoload 'User', 'cafmal/user'
15
+ autoload 'Worker', 'cafmal/worker'
16
+ end
17
+
18
+ require 'cafmal/resource'
19
+ require 'cafmal/request'
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cafmal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.21.0
5
+ platform: ruby
6
+ authors:
7
+ - Nils Bartels
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.14.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.14.0
69
+ description: The cafmal library in ruby
70
+ email:
71
+ - gem-cafmal@schrohm.de
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - cafmal.gemspec
84
+ - lib/cafmal.rb
85
+ - lib/cafmal/alert.rb
86
+ - lib/cafmal/alerter.rb
87
+ - lib/cafmal/auth.rb
88
+ - lib/cafmal/check.rb
89
+ - lib/cafmal/datasource.rb
90
+ - lib/cafmal/event.rb
91
+ - lib/cafmal/request.rb
92
+ - lib/cafmal/resource.rb
93
+ - lib/cafmal/team.rb
94
+ - lib/cafmal/user.rb
95
+ - lib/cafmal/version.rb
96
+ - lib/cafmal/worker.rb
97
+ homepage: https://rubygems.org/gems/cafmal
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.5.2
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Create alerts from metrics and logs
121
+ test_files: []