site_watcher 1.0.0 → 1.1.0

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
- SHA1:
3
- metadata.gz: ea3cfe2850109db82c2266644fcc33cdbc566bd2
4
- data.tar.gz: 8527bef8d97dc2010151c7ebd554625b03345a46
2
+ SHA256:
3
+ metadata.gz: cfa1a2dfba58f7bc84fbcba7db6a696b160688f7f1fe975f69186b7dd026a1c2
4
+ data.tar.gz: 3480fda5101368505953fba89458fb56d04cb3c78ebabd43ce5387ed89fd02cc
5
5
  SHA512:
6
- metadata.gz: 81c88964d9a96b186ffb7744a808c1046886fd4fbb8f5c4e1b9a20e2e6628b1bf5ba223b95208dcb9aaf24a56b73ed55bcebd2295513e1132ee94c15375291eb
7
- data.tar.gz: 83668aa2bf42d9f9067a1c73b77cc4b24c494a9212457efd81e6470082c636a06c45e6cfe9e363404d946a480d7fb60d97056231a0c85b6624773f8925bac895
6
+ metadata.gz: 03aada920765c224b9776d357f8edebcd00e0a299af52cd91eb7ab4802a3918ac58f26ceee361f006d5af51d40f9580ee3d501ca9a9a50a0882460a774b5b657
7
+ data.tar.gz: b2930dc82e888aba5764ab8bbf4cb29c82ce7ab7eaf044904e8c4d5ee8528ef06c04af58a86f9ee6550af42a8a622cacfb4ece5705234ffb2ce6395043452012
data/README.md CHANGED
@@ -49,6 +49,8 @@ end
49
49
 
50
50
  This script will block until all expectations have been fulfilled and removed. Using `remove_on_fulfillment false` will cause it to block indefinitely.
51
51
 
52
+ See [integration specs](spec/integration/watch_spec.rb) for more up-to-date usage examples.
53
+
52
54
  ## Testing fulfillment
53
55
 
54
56
  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.
@@ -1,10 +1,10 @@
1
1
  require "site_watcher/version"
2
2
 
3
3
  require "capybara"
4
- require "rspec/expectations"
5
- require "open-uri"
4
+ require "http"
6
5
  require "json"
7
6
  require "logger"
7
+ require "rspec/expectations"
8
8
 
9
9
  class SiteWatcher
10
10
  def self.watch(opts={}, &block)
@@ -15,11 +15,20 @@ class SiteWatcher
15
15
 
16
16
  delay = opts.fetch(:every, 5)
17
17
  logger = opts.fetch(:logger, ::Logger.new($stderr))
18
- new(dsl.__sw_pages, delay, logger).watch
18
+
19
+ new(
20
+ dsl.__sw_before_hooks,
21
+ dsl.__sw_pages,
22
+ dsl.__sw_after_hooks,
23
+ delay,
24
+ logger
25
+ ).watch
19
26
  end
20
27
 
21
- def initialize(pages, delay, logger)
28
+ def initialize(before_hooks, pages, after_hooks, delay, logger)
29
+ @before_hooks = before_hooks
22
30
  @pages = pages
31
+ @after_hooks = after_hooks
23
32
  @delay = delay
24
33
  @logger = logger
25
34
  @force = false
@@ -61,22 +70,35 @@ class SiteWatcher
61
70
  @force = true
62
71
  end
63
72
 
73
+ @before_hooks.each(&:call)
64
74
  yield
65
75
  ensure
66
76
  ::Signal.trap(:INT, "DEFAULT")
67
77
  ::Signal.trap(:USR1, "DEFAULT")
78
+
79
+ @after_hooks.each(&:call)
68
80
  end
69
81
 
70
82
  module DSL
71
83
  class Top
72
- attr_reader :__sw_pages
84
+ attr_reader :__sw_pages, :__sw_before_hooks, :__sw_after_hooks
73
85
 
74
86
  def initialize
75
87
  @__sw_pages = []
88
+ @__sw_before_hooks = []
89
+ @__sw_after_hooks = []
90
+ end
91
+
92
+ def before(&block)
93
+ @__sw_before_hooks << block
94
+ end
95
+
96
+ def after(&block)
97
+ @__sw_after_hooks << block
76
98
  end
77
99
 
78
- def page(url, &block)
79
- page = Page.new(url)
100
+ def page(url, **opts, &block)
101
+ page = Page.new(url, **opts)
80
102
  page.instance_eval(&block)
81
103
  @__sw_pages << page
82
104
  end
@@ -86,9 +108,13 @@ class SiteWatcher
86
108
  include ::RSpec::Matchers
87
109
  attr_reader :__sw_url, :__sw_remove_on_fulfillment
88
110
 
89
- def initialize(url)
111
+ def initialize(url, method: :get, headers: {}, **opts)
90
112
  @__sw_url = url
91
113
  @__sw_tests = []
114
+ @__sw_method = method
115
+ @__sw_headers = headers
116
+ @__sw_http_opts = opts
117
+ @__sw_fulfilled = nil
92
118
  @__sw_remove_on_fulfillment = true
93
119
  end
94
120
 
@@ -100,28 +126,42 @@ class SiteWatcher
100
126
  @__sw_fulfilled = block
101
127
  end
102
128
 
129
+ def headers(hash)
130
+ @__sw_headers = hash
131
+ end
132
+
133
+ def http_method(http_method)
134
+ @__sw_http_method = http_method
135
+ end
136
+
137
+ def body(body)
138
+ @__sw_body = body
139
+ end
140
+
103
141
  def remove_on_fulfillment(bool)
104
142
  @__sw_remove_on_fulfillment = !!bool
105
143
  end
106
144
 
107
145
  def __sw_run!(force=false)
108
- ::OpenURI.open_uri(@__sw_url) do |response|
109
- case response.content_type
110
- when /json/i
111
- page = ::JSON.parse(response.read)
112
- else
113
- page = ::Capybara::Node::Simple.new(response.read)
114
- end
115
-
116
- begin
117
- @__sw_tests.each { |test| test.call(page) }
118
- rescue ::RSpec::Expectations::ExpectationNotMetError => err
119
- __sw_fulfilled! if force
120
- raise(err)
121
- end
146
+ response = ::HTTP
147
+ .headers(@__sw_headers)
148
+ .request(@__sw_method, @__sw_url, **@__sw_http_opts)
149
+
150
+ case response.content_type.mime_type
151
+ when /json/i
152
+ page = ::JSON.parse(response.to_s)
153
+ else
154
+ page = ::Capybara::Node::Simple.new(response.to_s)
155
+ end
122
156
 
123
- __sw_fulfilled!
157
+ begin
158
+ @__sw_tests.each { |test| test.call(page) }
159
+ rescue ::RSpec::Expectations::ExpectationNotMetError => err
160
+ __sw_fulfilled! if force
161
+ raise(err)
124
162
  end
163
+
164
+ __sw_fulfilled!
125
165
  end
126
166
 
127
167
  private
@@ -1,3 +1,3 @@
1
1
  class SiteWatcher
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
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: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Genco
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-15 00:00:00.000000000 Z
11
+ date: 2018-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capybara
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: http
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'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec-expectations
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,20 @@ dependencies:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
96
  version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
83
111
  description:
84
112
  email:
85
113
  - alexgenco@gmail.com
@@ -111,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
139
  version: '0'
112
140
  requirements: []
113
141
  rubyforge_project:
114
- rubygems_version: 2.6.8
142
+ rubygems_version: 2.7.6
115
143
  signing_key:
116
144
  specification_version: 4
117
145
  summary: Periodically monitor a website