pingify 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ TODO
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pingify.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 tor
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.
@@ -0,0 +1,29 @@
1
+ # Pingify
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pingify'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pingify
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pingify'
@@ -0,0 +1,38 @@
1
+ require "pingify/version"
2
+ require 'pingify/result'
3
+ require 'rest-client'
4
+
5
+ module Pingify
6
+ class Runnable
7
+
8
+ attr_accessor :uri
9
+
10
+ def initialize(options={ :timeout => 2.0, :open_timeout => 2.0 }) @uri = options.delete :uri
11
+ @times = options.delete(:times) || 4
12
+ @http_opts = { :timeout => options.delete(:timeout), :open_timeout => options.delete(:open_timeout) }
13
+ end
14
+
15
+ def run
16
+ res = ping!
17
+ ping_headers = Hash[res.headers.select{ |k,_| k.match(/^x_ping_/) }.map{ |k,v| [k.to_s.sub('x_ping_', '').to_sym, v] }]
18
+
19
+ runtimes = @times - 1
20
+ start = Time.now
21
+ runtimes.times { ping! }
22
+ total_time = Time.now - start
23
+
24
+ avg = total_time / runtimes
25
+
26
+ data = { :average => avg, :body => res.body }.merge(ping_headers)
27
+ return Result.new(data)
28
+ rescue
29
+ return ErrorResult.new({}, [$!])
30
+ end
31
+
32
+ private
33
+
34
+ def ping!
35
+ RestClient.get(uri, @http_opts)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,29 @@
1
+ module Pinger
2
+ class Result
3
+ attr_reader :errors
4
+ attr_reader :data
5
+
6
+ def initialize(data, errors = [])
7
+ @data = data
8
+ @errors = errors
9
+ end
10
+
11
+ def success?
12
+ errors.none?
13
+ end
14
+
15
+ def average
16
+ data[:average]
17
+ end
18
+
19
+ def test
20
+ yield(data)
21
+ end
22
+ end
23
+
24
+ class ErrorResult < Result
25
+ def test(&block)
26
+ false
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Pingify
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pingify/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pingify"
8
+ spec.version = Pingify::VERSION
9
+ spec.authors = ["tor"]
10
+ spec.email = ["torkale@gmail.com"]
11
+ spec.description = %q{}
12
+ spec.summary = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ spec.add_development_dependency "webmock"
25
+ spec.add_dependency "rest-client"
26
+ end
@@ -0,0 +1,19 @@
1
+ require 'minitest/autorun'
2
+ require 'pingify'
3
+
4
+ describe "google.com" do
5
+ it 'does not fail' do
6
+ res = Pingify::Runnable.new({uri: 'http://google.com'}).run
7
+ res.success?.must_equal true
8
+ p res.data
9
+ res.average.must_be_close_to 0.3, 1
10
+ end
11
+ end
12
+
13
+ describe "example.com" do
14
+ it "fails" do
15
+ res = Pingify::Runnable.new({uri: 'http://localhost:10000', :timeout => 0.001}).run
16
+ res.success?.wont_equal true
17
+ end
18
+
19
+ end
@@ -0,0 +1,21 @@
1
+ require 'minitest/autorun'
2
+ require 'pingify/result'
3
+
4
+ describe Pingify::Result do
5
+ let(:result){
6
+ Pingify::Result.new(average: 1.0)
7
+ }
8
+ it 'passes on predicate' do
9
+ test = result.test do |d|
10
+ d[:average] < 2
11
+ end
12
+ assert test
13
+ end
14
+
15
+ it "fails on predicate" do
16
+ test = result.test do |d|
17
+ d[:average] > 2
18
+ end
19
+ refute test
20
+ end
21
+ end
@@ -0,0 +1,54 @@
1
+ require 'minitest/autorun'
2
+ require 'pingify'
3
+ require 'webmock/minitest'
4
+
5
+ describe Pingify do
6
+ let(:subject){
7
+ Pingify::Runnable.new({:uri => 'http://uri/ping'})
8
+ }
9
+
10
+ describe 'general ping' do
11
+ it 'returns runnable object' do
12
+ subject.must_respond_to(:run)
13
+ end
14
+
15
+ it 'extracts proper ping headers' do
16
+ stub_request(:any, 'uri/ping').to_return(:headers => { 'x-ping-appversion' => 1.0 })
17
+ res = subject.run
18
+ res.success?.must_equal true
19
+ res.data[:appversion].must_equal "1.0"
20
+ end
21
+ end
22
+
23
+ describe 'time pings' do
24
+ before do
25
+ stub_request(:any, 'uri/ping')
26
+ end
27
+
28
+ it "returns average of pings" do
29
+ refute_equal subject.run.data[:average], nil
30
+ end
31
+ end
32
+
33
+ describe 'errors' do
34
+ it 'timeout error' do
35
+ stub_request(:any, 'uri/ping').to_timeout
36
+ errs = subject.run.errors
37
+ errs.wont_be_empty
38
+ errs[0].class.must_equal RestClient::RequestTimeout
39
+ end
40
+
41
+ [500, 401, 404].each do |err_code|
42
+ it "#{err_code} error" do
43
+ stub_request(:any, 'uri/ping').to_return(:status => err_code)
44
+ errs = subject.run.errors
45
+ errs.wont_be_empty
46
+ end
47
+ end
48
+
49
+ it 'no uri' do
50
+ errs = subject.run.errors
51
+ errs.wont_be_empty
52
+ end
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pingify
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - tor
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ requirement: !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ type: :development
29
+ prerelease: false
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirement: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirement: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ type: :development
61
+ prerelease: false
62
+ - !ruby/object:Gem::Dependency
63
+ name: webmock
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirement: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ - !ruby/object:Gem::Dependency
79
+ name: rest-client
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirement: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ type: :runtime
93
+ prerelease: false
94
+ description: ''
95
+ email:
96
+ - torkale@gmail.com
97
+ executables:
98
+ - pingify
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - Gemfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - bin/pingify
108
+ - lib/pingify.rb
109
+ - lib/pingify/result.rb
110
+ - lib/pingify/version.rb
111
+ - pingify.gemspec
112
+ - spec/acceptance/network_spec.rb
113
+ - spec/pingify/result_spec.rb
114
+ - spec/pingify_spec.rb
115
+ homepage: ''
116
+ licenses:
117
+ - MIT
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 1.8.25
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: ''
140
+ test_files:
141
+ - spec/acceptance/network_spec.rb
142
+ - spec/pingify/result_spec.rb
143
+ - spec/pingify_spec.rb