site_watcher 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5d77a89f34668dbd4a2665be533691fe7a6c879b
4
+ data.tar.gz: a080492d4a1e3469f639a9f8300e93df86acefa2
5
+ SHA512:
6
+ metadata.gz: 0cec86dcdc2db4a8988c67a196546b4a6eed64bcfdcbb005ed8c5e341a619bedcbb7e76bc0bd725dcb65f9af7d48fe6f2b25c5ccea3519eae407387569116a70
7
+ data.tar.gz: fb9278ea99c31e2b803d3996c5277f44e205639822d48fd70da70dd97505b51c20566ac7a0385727e302f6c2a7b0befd2427f5a29465e44f19a353f87e19bfbe
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alex Genco
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,55 @@
1
+ # SiteWatcher
2
+
3
+ I wrote this to notify me when the Wii U Gamecube controller adapter becomes
4
+ available.
5
+
6
+ ## Usage
7
+
8
+ Here's an example script. I'm using this to monitor gamestop.com and bestbuy.com:
9
+
10
+ ```ruby
11
+ require "site_watcher"
12
+
13
+ SiteWatcher.watch(every: 2) do
14
+ # HTML
15
+ page("http://www.gamestop.com/wii-u/accessories/wii-u-gamecube-adapter/115426") do
16
+ # Use RSpec to describe your expectations.
17
+ test do |page|
18
+ # `page` is a `Capybara::Node::Simple`. See available matchers here:
19
+ # http://www.rubydoc.info/github/jnicklas/capybara/Capybara/Node/Matchers
20
+ expect(page).not_to have_selector("div.buy1 div.buttonna")
21
+ end
22
+
23
+ fulfilled do |url|
24
+ # Call this block when your expectations are met. Here I send myself an email.
25
+ IO.popen(
26
+ [
27
+ "/usr/bin/mail",
28
+ "-s", "GC controller adapter is available!",
29
+ "alexgenco@gmail.com"
30
+ ], "w"
31
+ ) { |io| io.puts(url) }
32
+ end
33
+ end
34
+
35
+ # JSON
36
+ page("http://www.bestbuy.com/api/1.0/product/summaries?skus=7522006") do
37
+ test do |json|
38
+ # `json` is a hash of the parsed JSON body.
39
+ expect(
40
+ json[0]["availability"]["ship"]["displayMessage"]
41
+ ).not_to match(/not available/i)
42
+ end
43
+ end
44
+ end
45
+ ```
46
+
47
+ This script will block until all expectations have been fulfilled.
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it ( https://github.com/[my-github-username]/site_watcher/fork )
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create a new Pull Request
@@ -0,0 +1,87 @@
1
+ require "site_watcher/version"
2
+
3
+ require "capybara"
4
+ require "rspec/expectations"
5
+ require "open-uri"
6
+ require "json"
7
+ require "logger"
8
+
9
+ class SiteWatcher
10
+ def self.watch(opts={}, &block)
11
+ trap(:SIGINT) { abort(?\n) }
12
+
13
+ delay = opts.fetch(:every, 5)
14
+ dsl = DSL::Top.new
15
+ dsl.instance_eval(&block)
16
+
17
+ new(dsl.pages).watch(delay)
18
+ end
19
+
20
+ def initialize(pages)
21
+ @pages = pages
22
+ @logger = ::Logger.new($stderr)
23
+ end
24
+
25
+ def watch(delay)
26
+ @pages.cycle do |page|
27
+ begin
28
+ page.__run!
29
+ @pages.delete(page)
30
+ rescue ::RSpec::Expectations::ExpectationNotMetError
31
+ rescue => e
32
+ @logger.warn("Exception on #{page.url}: #{e.inspect}")
33
+ end
34
+
35
+ sleep(delay) if @pages.last == page
36
+ end
37
+ end
38
+
39
+ module DSL
40
+ class Top
41
+ attr_reader :pages
42
+
43
+ def initialize
44
+ @pages = []
45
+ end
46
+
47
+ def page(url, &block)
48
+ Page.new(url).tap do |page|
49
+ page.instance_eval(&block)
50
+ @pages << page
51
+ end
52
+ end
53
+ end
54
+
55
+ class Page
56
+ include ::RSpec::Matchers
57
+ attr_reader :url
58
+
59
+ def initialize(url)
60
+ @url = url
61
+ @tests = []
62
+ end
63
+
64
+ def test(&block)
65
+ @tests << block
66
+ end
67
+
68
+ def fulfilled(&block)
69
+ @fulfilled = block
70
+ end
71
+
72
+ def __run!
73
+ open(@url) do |response|
74
+ case response.content_type
75
+ when /json/i
76
+ page = ::JSON.parse(response.read)
77
+ else
78
+ page = ::Capybara::Node::Simple.new(response.read)
79
+ end
80
+
81
+ @tests.each { |test| test.call(page) }
82
+ @fulfilled.call(@url) if @fulfilled.respond_to?(:call)
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,3 @@
1
+ class SiteWatcher
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: site_watcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Genco
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capybara
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-expectations
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description:
84
+ email:
85
+ - alexgenco@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE.txt
91
+ - README.md
92
+ - lib/site_watcher.rb
93
+ - lib/site_watcher/version.rb
94
+ homepage: https://github.com/alexgenco/site_watcher
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.2.2
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Periodically monitor a website
118
+ test_files: []
119
+ has_rdoc: