snoop 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: 5110d8e59d54d0852a8345496faf7c98a1b8499a
4
+ data.tar.gz: 8fa5cda09082e4da2e339e5018aa12b7d216ac39
5
+ SHA512:
6
+ metadata.gz: 5981dddcfee4514309c22cc7fb51c0256c3e2452adc5d5b7e3167a56f28cd922176bf4c5d3ba1b1249eabefe93683a72476087ea4d3ff571e5c68ee6bee675d3
7
+ data.tar.gz: 9189b8ef3880a7d7a0d14aa20c5f5b82147ff5a2a4d8df23514cf0139f526f5990d9c28fdebfe9f83f1626caccafd08c7494c128f5a5048dca26e85ba4b1e8a0
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.swp
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in snoop.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ snoop (0.0.1)
5
+ httparty
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ daemons (1.1.9)
11
+ diff-lcs (1.2.2)
12
+ eventmachine (1.0.3)
13
+ httparty (0.10.2)
14
+ multi_json (~> 1.0)
15
+ multi_xml (>= 0.5.2)
16
+ multi_json (1.7.2)
17
+ multi_xml (0.5.3)
18
+ rack (1.5.2)
19
+ rack-protection (1.5.0)
20
+ rack
21
+ rake (10.0.4)
22
+ rspec (2.13.0)
23
+ rspec-core (~> 2.13.0)
24
+ rspec-expectations (~> 2.13.0)
25
+ rspec-mocks (~> 2.13.0)
26
+ rspec-core (2.13.1)
27
+ rspec-expectations (2.13.0)
28
+ diff-lcs (>= 1.1.3, < 2.0)
29
+ rspec-mocks (2.13.1)
30
+ sinatra (1.4.2)
31
+ rack (~> 1.5, >= 1.5.2)
32
+ rack-protection (~> 1.4)
33
+ tilt (~> 1.3, >= 1.3.4)
34
+ thin (1.5.1)
35
+ daemons (>= 1.0.9)
36
+ eventmachine (>= 0.12.6)
37
+ rack (>= 1.0.0)
38
+ tilt (1.3.6)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ bundler (~> 1.3)
45
+ rake
46
+ rspec
47
+ sinatra
48
+ snoop!
49
+ thin
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Chris Hunt
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,35 @@
1
+ # Snoop `wip`
2
+ Monitor content for changes and be notified
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'snoop'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ ```bash
15
+ $ bundle
16
+ ```
17
+
18
+ Or install it yourself as:
19
+
20
+ ```bash
21
+ $ gem install snoop
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ `wip`
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Run the tests (`bundle exec rake spec`)
34
+ 5. Push to the branch (`git push origin my-new-feature`)
35
+ 6. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |t|
5
+ t.rspec_opts = '--color --order random'
6
+ end
7
+
8
+ task default: :spec
@@ -0,0 +1,44 @@
1
+ require 'httparty'
2
+
3
+ module Snoop
4
+ class HttpMonitor
5
+ UrlRequiredException = Class.new(StandardError)
6
+
7
+ attr_reader :url, :notifier, :content, :http_client, :interval
8
+ attr_accessor :content
9
+
10
+ def initialize(
11
+ url: nil, http_client: HTTParty, notifier: MacOSNotifier.new, interval: 1
12
+ )
13
+ raise UrlRequiredException if url.nil?
14
+ @url = url
15
+ @http_client = http_client
16
+ @notifier = notifier
17
+ @interval = interval
18
+ end
19
+
20
+ def monitor
21
+ while true
22
+ notify
23
+ sleep interval
24
+ end
25
+ end
26
+
27
+ def notify
28
+ notifier.notify if content_changed?
29
+ end
30
+
31
+ def content_changed?
32
+ old_content = @content
33
+ @content = fetch_content
34
+ old_content != @content
35
+ end
36
+
37
+ def fetch_content
38
+ http_client.get(url).body
39
+ end
40
+ end
41
+
42
+ class MacOSNotifier
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Snoop
2
+ VERSION = '0.0.1'
3
+ end
data/lib/snoop.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'snoop/version'
2
+ require 'snoop/http_monitor'
data/snoop.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'snoop/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'snoop'
8
+ spec.version = Snoop::VERSION
9
+ spec.authors = ['Chris Hunt']
10
+ spec.email = ['c@chrishunt.co']
11
+ spec.description = %q{Monitor content for changes}
12
+ spec.summary = %q{Monitor content for changes and be notified}
13
+ spec.homepage = 'https://github.com/chrishunt/snoop'
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 'rspec'
24
+ spec.add_development_dependency 'thin'
25
+ spec.add_development_dependency 'sinatra'
26
+
27
+ spec.add_runtime_dependency 'httparty'
28
+ end
@@ -0,0 +1,40 @@
1
+ require 'snoop/http_monitor'
2
+ require 'support/http_server'
3
+
4
+ describe Snoop::HttpMonitor do
5
+ class TestNotifier
6
+ attr_reader :notification_count
7
+
8
+ def initialize(notification_count = 0)
9
+ @notification_count = notification_count
10
+ end
11
+
12
+ def notify
13
+ @notification_count += 1
14
+ end
15
+ end
16
+
17
+ let(:notifier) { TestNotifier.new }
18
+
19
+ with_http_server do |url|
20
+ it 'notifies when http content has changed' do
21
+ monitor = described_class.new(
22
+ url: "#{url}/dynamic-content",
23
+ notifier: notifier
24
+ )
25
+
26
+ 2.times { monitor.notify }
27
+ expect(notifier.notification_count).to eq 2
28
+ end
29
+
30
+ it 'does not notify when http content has not changed' do
31
+ monitor = described_class.new(
32
+ url: "#{url}/static-content",
33
+ notifier: notifier
34
+ )
35
+
36
+ 2.times { monitor.notify }
37
+ expect(notifier.notification_count).to eq 1
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,38 @@
1
+ require 'httparty'
2
+ require 'sinatra/base'
3
+
4
+ module Snoop
5
+ class HttpServer < Sinatra::Base
6
+ set :port, 8001
7
+
8
+ get '/' do
9
+ 'success'
10
+ end
11
+
12
+ get '/dynamic-content' do
13
+ @@count ||= 0
14
+ @@count += 1
15
+ @@count.to_s
16
+ end
17
+
18
+ get '/static-content' do
19
+ 'same ol stuff'
20
+ end
21
+ end
22
+ end
23
+
24
+ def with_http_server
25
+ url = "http://localhost:#{Snoop::HttpServer.port}"
26
+ server_thread = Thread.new { Snoop::HttpServer.run! }
27
+
28
+ while true
29
+ begin
30
+ HTTParty.get(url)
31
+ break
32
+ rescue Errno::ECONNREFUSED
33
+ sleep 0.5
34
+ end
35
+ end
36
+
37
+ yield(url)
38
+ end
@@ -0,0 +1,98 @@
1
+ require 'snoop/http_monitor'
2
+
3
+ describe Snoop::HttpMonitor do
4
+ subject { described_class.new(url: url) }
5
+
6
+ let(:url) { 'http://example.com' }
7
+
8
+ describe '#initialize' do
9
+ it 'saves the url' do
10
+ expect(subject.url).to eq url
11
+ end
12
+
13
+ context 'when a url is not provided' do
14
+ let(:url) { nil }
15
+
16
+ it 'raises an exception' do
17
+ expect {
18
+ subject
19
+ }.to raise_error Snoop::HttpMonitor::UrlRequiredException
20
+ end
21
+ end
22
+ end
23
+
24
+ describe '#notify' do
25
+ let(:notifier) { stub }
26
+
27
+ before do
28
+ subject.stub(
29
+ content_changed?: content_changed?,
30
+ notifier: notifier
31
+ )
32
+ end
33
+
34
+ context 'when the content changes' do
35
+ let(:content_changed?) { true }
36
+
37
+ it 'triggers a notification' do
38
+ notifier.should_receive(:notify)
39
+ subject.notify
40
+ end
41
+ end
42
+
43
+ context 'when the content does not change' do
44
+ let(:content_changed?) { false }
45
+
46
+ it 'does not trigger a notification' do
47
+ notifier.should_not_receive(:notify)
48
+ subject.notify
49
+ end
50
+ end
51
+ end
52
+
53
+ describe '#content_changed?' do
54
+ it 'updates the content' do
55
+ subject.content = 'abc123'
56
+ subject.stub(fetch_content: 'def456')
57
+
58
+ subject.content_changed?
59
+
60
+ expect(subject.content).to eq 'def456'
61
+ end
62
+
63
+ context 'when content has changed' do
64
+ before do
65
+ subject.content = 'abc123'
66
+ subject.stub(fetch_content: 'def456')
67
+ end
68
+
69
+ it 'returns true' do
70
+ expect(subject.content_changed?).to be_true
71
+ end
72
+ end
73
+
74
+ context 'when content has not changed' do
75
+ before do
76
+ subject.content = 'abc123'
77
+ subject.stub(fetch_content: 'abc123')
78
+ end
79
+
80
+ it 'returns false' do
81
+ expect(subject.content_changed?).to be_false
82
+ end
83
+ end
84
+ end
85
+
86
+ describe '#fetch_content' do
87
+ it 'fetches content over http' do
88
+ response = stub('HttpResponse', body: 'stuff')
89
+ http_client = stub('HttpClient')
90
+
91
+ subject.stub(http_client: http_client)
92
+
93
+ http_client.should_receive(:get).with(url).and_return(response)
94
+
95
+ expect(subject.fetch_content).to eq response.body
96
+ end
97
+ end
98
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snoop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Hunt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-10 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thin
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sinatra
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: httparty
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Monitor content for changes
98
+ email:
99
+ - c@chrishunt.co
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/snoop.rb
111
+ - lib/snoop/http_monitor.rb
112
+ - lib/snoop/version.rb
113
+ - snoop.gemspec
114
+ - spec/acceptance/http_monitor_spec.rb
115
+ - spec/support/http_server.rb
116
+ - spec/unit/http_monitor_spec.rb
117
+ homepage: https://github.com/chrishunt/snoop
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.0.3
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Monitor content for changes and be notified
141
+ test_files:
142
+ - spec/acceptance/http_monitor_spec.rb
143
+ - spec/support/http_server.rb
144
+ - spec/unit/http_monitor_spec.rb