site_watcher 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d77a89f34668dbd4a2665be533691fe7a6c879b
4
- data.tar.gz: a080492d4a1e3469f639a9f8300e93df86acefa2
3
+ metadata.gz: 5767baf72233d2af5eec6adb9b2810e0b8e8bd6c
4
+ data.tar.gz: 77bfeb19efc58a3d42e993bef52ce38f9e8087a5
5
5
  SHA512:
6
- metadata.gz: 0cec86dcdc2db4a8988c67a196546b4a6eed64bcfdcbb005ed8c5e341a619bedcbb7e76bc0bd725dcb65f9af7d48fe6f2b25c5ccea3519eae407387569116a70
7
- data.tar.gz: fb9278ea99c31e2b803d3996c5277f44e205639822d48fd70da70dd97505b51c20566ac7a0385727e302f6c2a7b0befd2427f5a29465e44f19a353f87e19bfbe
6
+ metadata.gz: f2491e2466343d145acf42246243612d336392fd3850138e10135b183418a23ec9e8e5245be49ed259311808484dbdf675fe79bf80820c36f08cdcc5eb21d8d0
7
+ data.tar.gz: 282ee975b7b81731b4e06e3e42f7e3b10435a360d3181c92ea5ead7cf2859cb8a2d3dc3775dfa478a5186357ed9f44bc95ca7b827fd01e5e1a77219463352827
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # SiteWatcher
1
+ # SiteWatcher [![Gem Version](https://badge.fury.io/rb/site_watcher.svg)](http://badge.fury.io/rb/site_watcher)
2
2
 
3
3
  I wrote this to notify me when the Wii U Gamecube controller adapter becomes
4
4
  available.
@@ -46,6 +46,10 @@ end
46
46
 
47
47
  This script will block until all expectations have been fulfilled.
48
48
 
49
+ ## Testing fulfillment
50
+
51
+ You can force a `SiteWatcher.watch` process to run each of its `fulfilled` blocks by sending it the `USR1` signal. The process will continue to run as normal after each block has been called.
52
+
49
53
  ## Contributing
50
54
 
51
55
  1. Fork it ( https://github.com/[my-github-username]/site_watcher/fork )
@@ -10,67 +10,97 @@ class SiteWatcher
10
10
  def self.watch(opts={}, &block)
11
11
  trap(:SIGINT) { abort(?\n) }
12
12
 
13
- delay = opts.fetch(:every, 5)
14
13
  dsl = DSL::Top.new
15
14
  dsl.instance_eval(&block)
16
15
 
17
- new(dsl.pages).watch(delay)
16
+ delay = opts.fetch(:every, 5)
17
+ logger = opts.fetch(:logger, ::Logger.new($stderr))
18
+ new(dsl.__sw_pages, delay, logger).watch
18
19
  end
19
20
 
20
- def initialize(pages)
21
+ def initialize(pages, delay, logger)
21
22
  @pages = pages
22
- @logger = ::Logger.new($stderr)
23
+ @delay = delay
24
+ @logger = logger
25
+ @force = false
23
26
  end
24
27
 
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}")
28
+ def watch
29
+ capture_signals do
30
+ loop do
31
+ break if @pages.empty?
32
+
33
+ force = @force
34
+ @force &&= false
35
+
36
+ @pages.each do |page|
37
+ begin
38
+ page.__sw_run!(force)
39
+ @pages.delete(page)
40
+ rescue ::RSpec::Expectations::ExpectationNotMetError
41
+ rescue => e
42
+ @logger.warn("Exception on #{page.__sw_url}: #{e.inspect}")
43
+ end
44
+ end
45
+
46
+ sleep(@delay)
33
47
  end
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def capture_signals
54
+ ::Signal.trap(:INT) { abort(?\n) }
34
55
 
35
- sleep(delay) if @pages.last == page
56
+ ::Signal.trap(:USR1) do
57
+ ::Thread.new do
58
+ @logger.warn("Received USR1, next round of tests will force fulfillment")
59
+ end.join
60
+
61
+ @force = true
36
62
  end
63
+
64
+ yield
65
+ ensure
66
+ ::Signal.trap(:INT, "DEFAULT")
67
+ ::Signal.trap(:USR1, "DEFAULT")
37
68
  end
38
69
 
39
70
  module DSL
40
71
  class Top
41
- attr_reader :pages
72
+ attr_reader :__sw_pages
42
73
 
43
74
  def initialize
44
- @pages = []
75
+ @__sw_pages = []
45
76
  end
46
77
 
47
78
  def page(url, &block)
48
- Page.new(url).tap do |page|
49
- page.instance_eval(&block)
50
- @pages << page
51
- end
79
+ page = Page.new(url)
80
+ page.instance_eval(&block)
81
+ @__sw_pages << page
52
82
  end
53
83
  end
54
84
 
55
85
  class Page
56
86
  include ::RSpec::Matchers
57
- attr_reader :url
87
+ attr_reader :__sw_url
58
88
 
59
89
  def initialize(url)
60
- @url = url
61
- @tests = []
90
+ @__sw_url = url
91
+ @__sw_tests = []
62
92
  end
63
93
 
64
94
  def test(&block)
65
- @tests << block
95
+ @__sw_tests << block
66
96
  end
67
97
 
68
98
  def fulfilled(&block)
69
- @fulfilled = block
99
+ @__sw_fulfilled = block
70
100
  end
71
101
 
72
- def __run!
73
- open(@url) do |response|
102
+ def __sw_run!(force=false)
103
+ ::OpenURI.open_uri(@__sw_url) do |response|
74
104
  case response.content_type
75
105
  when /json/i
76
106
  page = ::JSON.parse(response.read)
@@ -78,10 +108,22 @@ class SiteWatcher
78
108
  page = ::Capybara::Node::Simple.new(response.read)
79
109
  end
80
110
 
81
- @tests.each { |test| test.call(page) }
82
- @fulfilled.call(@url) if @fulfilled.respond_to?(:call)
111
+ begin
112
+ @__sw_tests.each { |test| test.call(page) }
113
+ rescue ::RSpec::Expectations::ExpectationNotMetError => err
114
+ __sw_fulfilled! if force
115
+ raise(err)
116
+ end
117
+
118
+ __sw_fulfilled!
83
119
  end
84
120
  end
121
+
122
+ private
123
+
124
+ def __sw_fulfilled!
125
+ @__sw_fulfilled.call(@__sw_url) if @__sw_fulfilled.respond_to?(:call)
126
+ end
85
127
  end
86
128
  end
87
129
  end
@@ -1,3 +1,3 @@
1
1
  class SiteWatcher
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: site_watcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Genco
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-07 00:00:00.000000000 Z
11
+ date: 2015-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capybara