app_monit 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: 4e1109ea294bf183a829e22b2dcb4629637deb5a
4
+ data.tar.gz: 7fe39bad8e6db970a2355518bd8cfa049c28a4df
5
+ SHA512:
6
+ metadata.gz: 611e7fb651484d36800ffcda8d43554ecd0b0a656d5c51e26eb9011ef1e5d26c8a11e8136f615a88b49015a166c4d8f40e578084107ee770e603d345b7bfce6b
7
+ data.tar.gz: cad200e2abd1d74c6d21be8f8358f88759d9b766b3bca67ae1730b6b7b7304e949d611fb4d46cc5754e9f70127c41c3bd00cdcaa6849ac41171c02c355f013c6
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ .idea
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.0
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ - 2.0.0
5
+ - 1.9.3
6
+ - jruby-19mode
7
+ - ruby-head
8
+ - jruby-head
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in app_monit.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,14 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :minitest do
5
+ # with Minitest::Unit
6
+ # watch(%r{^test/(.*)\/?test_(.*)\.rb$})
7
+ # watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
8
+ # watch(%r{^test/test_helper\.rb$}) { 'test' }
9
+
10
+ # with Minitest::Spec
11
+ watch(%r{^spec/(.*)_spec\.rb$})
12
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
13
+ watch(%r{^spec/spec_helper\.rb$}) { 'spec' }
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Appmon.it
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,31 @@
1
+ [![Build Status](https://travis-ci.org/appmonit/app_monit_gem.png?branch=master)](https://travis-ci.org/appmonit/app_monit_gem)
2
+
3
+ # AppMonit
4
+
5
+ TODO: Write a gem description
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'app_monit'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install app_monit
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( http://github.com/[my-github-username]/app_monit/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'spec'
6
+ t.pattern = 'spec/**/*_spec.rb'
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'app_monit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "app_monit"
8
+ spec.version = AppMonit::VERSION
9
+ spec.authors = ["Redmar Kerkhoff", "Benoist Claassen"]
10
+ spec.email = ["redmar@appmon.it", "benoist@appmon.it"]
11
+ spec.summary = %q{Client gem for pushing events from ruby to the appmon.it service}
12
+ spec.description = %q{Client gem for pushing events from ruby to the appmon.it service}
13
+ spec.homepage = "http://appmon.it"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(spec|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", ">= 1.5.0"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest", ">= 5.2.2"
24
+ spec.add_development_dependency "minitest-reporters"
25
+ spec.add_development_dependency "webmock", ">= 1.17.1"
26
+ spec.add_development_dependency "guard-minitest" if RUBY_PLATFORM =~ /darwin/
27
+ end
@@ -0,0 +1,19 @@
1
+ module AppMonit
2
+ class Config
3
+ class << self
4
+ attr_writer :api_key, :env, :end_point
5
+
6
+ def api_key
7
+ @api_key || raise(ApiKeyNotSetError.new("Please set your API key"))
8
+ end
9
+
10
+ def env
11
+ @env || "development"
12
+ end
13
+
14
+ def end_point
15
+ @end_point || "http://api.appmon.it"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ module AppMonit
2
+ class ApiKeyNotSetError < RuntimeError
3
+ def message
4
+ "Please set your API key, see http://appmon.it/help/set_api_key for more info."
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ require 'json'
2
+
3
+ module AppMonit
4
+ class Event
5
+ def self.create(*args)
6
+ create!(*args)
7
+ rescue Http::Error
8
+ false
9
+ end
10
+
11
+ def self.create!(name, data_hash = {})
12
+ created_at = data_hash.delete(:created_at) || Time.now.utc
13
+
14
+ message = { created_at: created_at, name: name, payload: data_hash }
15
+ client.post('/v1/events', message)
16
+ end
17
+
18
+ def self.client
19
+ Http
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,51 @@
1
+ module AppMonit
2
+ class Http
3
+ Error = Class.new(StandardError)
4
+
5
+ SUCCESS_CODES = %w(200 201).freeze
6
+
7
+ attr_accessor :client
8
+
9
+ def initialize
10
+ uri = URI.parse(AppMonit::Config.end_point)
11
+ @client = Net::HTTP.new(uri.host, uri.port)
12
+ if uri.scheme == 'https'
13
+ @client.use_ssl = true
14
+ @client.verify_mode = OpenSSL::SSL::VERIFY_PEER
15
+ end
16
+
17
+ @client.read_timeout = 1
18
+ end
19
+
20
+ def self.post(path, data_hash)
21
+ request :post, path, data_hash
22
+ end
23
+
24
+ def self.get(path)
25
+ request :get, path
26
+ end
27
+
28
+ def self.request(*args)
29
+ new.request(*args)
30
+ end
31
+
32
+ def request(method, path, body = nil)
33
+ if method == :get
34
+ request = Net::HTTP::Get.new(path)
35
+ else
36
+ request = Net::HTTP::Post.new(path)
37
+ request.body = body.to_json if body
38
+ request.content_type = 'application/json'
39
+ end
40
+
41
+ # set headers so event data ends up in the correct bucket on the other side
42
+ request.add_field('Appmonit-Api-Key', AppMonit::Config.api_key)
43
+ request.add_field('Appmonit-Env', AppMonit::Config.env)
44
+ response = client.request(request)
45
+
46
+ raise Error.new("Invalid response code: #{response.code} body: #{response.body}") unless SUCCESS_CODES.include?(response.code)
47
+
48
+ response
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,27 @@
1
+ module AppMonit
2
+ class Query
3
+ class << self
4
+ %w(count count_unique minimum maximum average sum funnel).each do |method_name|
5
+ define_method method_name do |collection_name, params|
6
+ query(method_name, collection_name, params)
7
+ end
8
+ end
9
+
10
+ def query(method_name, collection_name, params)
11
+ require 'cgi'
12
+ path = "/v1/queries/#{method_name}"
13
+
14
+ params[:event_collection] = collection_name
15
+
16
+ response = Http.get("#{path}?query=#{CGI.escape(params.to_json)}")
17
+
18
+ case response.code.to_i
19
+ when 200
20
+ JSON.parse(response.body)
21
+ else
22
+ nil
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module AppMonit
2
+ VERSION = "0.0.1"
3
+ end
data/lib/app_monit.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "app_monit/version"
2
+ require "app_monit/errors"
3
+ require "app_monit/config"
4
+ require "app_monit/http"
5
+ require "app_monit/event"
6
+ require "app_monit/query"
7
+ require "net/https"
8
+
9
+ module AppMonit
10
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe AppMonit::Event do
4
+
5
+ subject { AppMonit::Event }
6
+
7
+ before :each do
8
+ AppMonit::Config.api_key = 'MYAPIKEY'
9
+ AppMonit::Config.end_point = 'http://api.appmon.it'
10
+ AppMonit::Config.env = nil
11
+
12
+ stub_request(:post, /.*/)
13
+ end
14
+
15
+ it 'must raise an exception if API key is unset' do
16
+ AppMonit::Config.api_key = nil
17
+ assert_raises AppMonit::ApiKeyNotSetError do
18
+ subject.create({})
19
+ end
20
+ end
21
+
22
+ describe '#create' do
23
+ it 'returns a response if successful' do
24
+ subject.stub(:create!, 'response') do
25
+ assert_equal 'response', subject.create
26
+ end
27
+ end
28
+
29
+ it 'returns false if an Http::Error is raised' do
30
+ subject.stub(:create!, -> { raise AppMonit::Http::Error }) do
31
+ assert_equal false, subject.create
32
+ end
33
+ end
34
+ end
35
+
36
+ describe '#create!' do
37
+ it 'sets a created at if not given' do
38
+ @mock = MiniTest::Mock.new
39
+ @mock.expect(:post, true, ['/v1/events', { created_at: Time.at(0).utc, name: 'test', payload: {} }])
40
+
41
+ Time.stub(:now, Time.at(0)) do
42
+ subject.stub(:client, @mock) do
43
+ subject.create!('test', {})
44
+ @mock.verify
45
+ end
46
+ end
47
+ end
48
+
49
+ it 'sets calls post on http with the correct params' do
50
+ time = Time.now
51
+ @mock = MiniTest::Mock.new
52
+ @mock.expect(:post, true, ['/v1/events', { created_at: time.utc, name: 'test', payload: {test: 'test'} }])
53
+
54
+ subject.stub(:client, @mock) do
55
+ subject.create!('test', {created_at: time, test: 'test'})
56
+ @mock.verify
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe AppMonit::Http do
4
+ subject { AppMonit::Http.new }
5
+
6
+ before do
7
+ AppMonit::Config.api_key = 'MYAPIKEY'
8
+ AppMonit::Config.end_point = 'http://api.appmon.it'
9
+ AppMonit::Config.env = nil
10
+
11
+ stub_request(:post, /.*/)
12
+ end
13
+
14
+ it 'POSTs to the configured end_point' do
15
+ AppMonit::Config.end_point = 'http://xyz.appmon.it'
16
+ subject.request(:post, '/', {})
17
+ assert_requested :post, /^\Axyz.appmon.it/
18
+ end
19
+
20
+ it 'sets the HTTP header to application/json' do
21
+ subject.request(:post, '/', {})
22
+ assert_requested :post, /.*/, :headers => { 'Content-Type' => 'application/json' }
23
+ end
24
+
25
+ it 'sets the HTTP header Appmonit-Env to the configured environment' do
26
+ AppMonit::Config.env = 'staging'
27
+ subject.request(:post, '/', {})
28
+ assert_requested :post, /.*/, :headers => { 'Appmonit-Env' => 'staging' }
29
+ end
30
+
31
+ it 'sets the HTTP header Appmonit-Env by default to \'development\' environment' do
32
+ AppMonit::Config.env = nil # make sure it's unset so it defaults to normal
33
+ subject.request(:post, '/', {})
34
+ assert_requested :post, /.*/, :headers => { 'Appmonit-Env' => 'development' }
35
+ end
36
+
37
+ it 'sets the HTTP header Appmonit-Api-Key to the configured API Key' do
38
+ AppMonit::Config.api_key = "FUBAR123"
39
+ subject.request(:post, '/', {})
40
+ assert_requested :post, /.*/, :headers => { 'Appmonit-Api-Key' => 'FUBAR123' }
41
+ end
42
+
43
+ it 'sets the read timeout to 1 second' do
44
+ assert_equal 1, subject.client.read_timeout
45
+ end
46
+
47
+ describe 'when using ssl' do
48
+ before(:each) { AppMonit::Config.end_point = 'https://xyz.appmon.it' }
49
+
50
+ it 'sets the use_ssl flag to true' do
51
+ assert_equal true, subject.client.use_ssl?
52
+ end
53
+
54
+ it 'sets the verify mode to PEER' do
55
+ assert_equal OpenSSL::SSL::VERIFY_PEER, subject.client.verify_mode
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe AppMonit::Query do
4
+ subject { AppMonit::Query }
5
+
6
+ before do
7
+ AppMonit::Config.api_key = 'MYAPIKEY'
8
+ AppMonit::Config.end_point = 'http://api.appmon.it'
9
+ AppMonit::Config.env = nil
10
+ end
11
+
12
+ %w(count count_unique minimum maximum average sum funnel).each do |method_name|
13
+ describe method_name do
14
+ it 'gets the results with the given params' do
15
+ stub_request(:get, /api.appmon.it\/v1\/queries\/#{method_name}/).to_return(code: '200', body: {result: '0'}.to_json)
16
+
17
+ params = { valid: 'params' }
18
+ subject.send(method_name, 'collection_name', params)
19
+
20
+ params[:event_collection] = 'collection_name'
21
+
22
+ assert_requested(:get, /api.appmon.it\/v1\/queries\/#{method_name}\?query=#{params.to_json}/)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,8 @@
1
+ require 'app_monit'
2
+ require 'minitest/autorun'
3
+ require 'minitest/unit'
4
+ #require 'minitest/pride'
5
+ require 'minitest/reporters'
6
+ Minitest::Reporters.use! Minitest::Reporters::DefaultReporter.new
7
+
8
+ require 'webmock/minitest'
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: app_monit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Redmar Kerkhoff
8
+ - Benoist Claassen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-03-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 1.5.0
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 1.5.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: minitest
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 5.2.2
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 5.2.2
56
+ - !ruby/object:Gem::Dependency
57
+ name: minitest-reporters
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: webmock
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 1.17.1
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 1.17.1
84
+ - !ruby/object:Gem::Dependency
85
+ name: guard-minitest
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description: Client gem for pushing events from ruby to the appmon.it service
99
+ email:
100
+ - redmar@appmon.it
101
+ - benoist@appmon.it
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".ruby-version"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - Guardfile
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - appmonit_gem.gemspec
115
+ - lib/app_monit.rb
116
+ - lib/app_monit/config.rb
117
+ - lib/app_monit/errors.rb
118
+ - lib/app_monit/event.rb
119
+ - lib/app_monit/http.rb
120
+ - lib/app_monit/query.rb
121
+ - lib/app_monit/version.rb
122
+ - spec/app_monit/event_spec.rb
123
+ - spec/app_monit/http_spec.rb
124
+ - spec/app_monit/query_spec.rb
125
+ - spec/spec_helper.rb
126
+ homepage: http://appmon.it
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.2.1
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Client gem for pushing events from ruby to the appmon.it service
150
+ test_files:
151
+ - spec/app_monit/event_spec.rb
152
+ - spec/app_monit/http_spec.rb
153
+ - spec/app_monit/query_spec.rb
154
+ - spec/spec_helper.rb