site_watcher 0.0.2 → 1.3.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: 5767baf72233d2af5eec6adb9b2810e0b8e8bd6c
4
- data.tar.gz: 77bfeb19efc58a3d42e993bef52ce38f9e8087a5
2
+ SHA256:
3
+ metadata.gz: 3871bda59d433bb2e423b760919479b402714bcb2ac18ef0d2e6312f3e06a3ee
4
+ data.tar.gz: b154e6c67de541fe782cdf1583c335ddd30fbc92d98cdc8895d1541f397ac414
5
5
  SHA512:
6
- metadata.gz: f2491e2466343d145acf42246243612d336392fd3850138e10135b183418a23ec9e8e5245be49ed259311808484dbdf675fe79bf80820c36f08cdcc5eb21d8d0
7
- data.tar.gz: 282ee975b7b81731b4e06e3e42f7e3b10435a360d3181c92ea5ead7cf2859cb8a2d3dc3775dfa478a5186357ed9f44bc95ca7b827fd01e5e1a77219463352827
6
+ metadata.gz: 50a42cc608c857812f62578cac2f37eca9a7c2a8b1d3b73850c6a95a5ff75f1a940f71b856a069ccb4e93891906b097cc051bcd4b9284ced82beaa0b6e62437b
7
+ data.tar.gz: 5b2587837c2d4db18c2565c7818146ce7b70e83c2989a39197dde9f3e013456af86d721e3e71f58daf76f66af43b78b945a4ed9d9122ccc7886aa684b9b33049
data/README.md CHANGED
@@ -34,6 +34,9 @@ SiteWatcher.watch(every: 2) do
34
34
 
35
35
  # JSON
36
36
  page("http://www.bestbuy.com/api/1.0/product/summaries?skus=7522006") do
37
+ # Continue watching this page even after fulfillment
38
+ remove_on_fulfillment false
39
+
37
40
  test do |json|
38
41
  # `json` is a hash of the parsed JSON body.
39
42
  expect(
@@ -44,7 +47,9 @@ SiteWatcher.watch(every: 2) do
44
47
  end
45
48
  ```
46
49
 
47
- This script will block until all expectations have been fulfilled.
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
+
52
+ See [integration specs](spec/integration/watch_spec.rb) for more up-to-date usage examples.
48
53
 
49
54
  ## Testing fulfillment
50
55
 
@@ -1,3 +1,3 @@
1
1
  class SiteWatcher
2
- VERSION = "0.0.2"
2
+ VERSION = "1.3.0"
3
3
  end
data/lib/site_watcher.rb CHANGED
@@ -1,27 +1,37 @@
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)
11
- trap(:SIGINT) { abort(?\n) }
12
-
13
11
  dsl = DSL::Top.new
14
12
  dsl.instance_eval(&block)
15
13
 
16
14
  delay = opts.fetch(:every, 5)
17
- logger = opts.fetch(:logger, ::Logger.new($stderr))
18
- new(dsl.__sw_pages, delay, logger).watch
15
+ logger = opts.fetch(:logger, ::Logger.new($stderr, level: :warn))
16
+ timeout = opts.fetch(:timeout, {connect: 3, read: 10})
17
+
18
+ new(
19
+ dsl.__sw_before_hooks,
20
+ dsl.__sw_pages,
21
+ dsl.__sw_after_hooks,
22
+ delay,
23
+ logger,
24
+ timeout,
25
+ ).watch
19
26
  end
20
27
 
21
- def initialize(pages, delay, logger)
28
+ def initialize(before_hooks, pages, after_hooks, delay, logger, timeout)
29
+ @before_hooks = before_hooks
22
30
  @pages = pages
31
+ @after_hooks = after_hooks
23
32
  @delay = delay
24
33
  @logger = logger
34
+ @timeout = timeout
25
35
  @force = false
26
36
  end
27
37
 
@@ -35,8 +45,8 @@ class SiteWatcher
35
45
 
36
46
  @pages.each do |page|
37
47
  begin
38
- page.__sw_run!(force)
39
- @pages.delete(page)
48
+ page.__sw_run!(force, @logger, @timeout)
49
+ @pages.delete(page) if page.__sw_remove_on_fulfillment
40
50
  rescue ::RSpec::Expectations::ExpectationNotMetError
41
51
  rescue => e
42
52
  @logger.warn("Exception on #{page.__sw_url}: #{e.inspect}")
@@ -61,22 +71,35 @@ class SiteWatcher
61
71
  @force = true
62
72
  end
63
73
 
74
+ @before_hooks.each(&:call)
64
75
  yield
65
76
  ensure
66
77
  ::Signal.trap(:INT, "DEFAULT")
67
78
  ::Signal.trap(:USR1, "DEFAULT")
79
+
80
+ @after_hooks.each(&:call)
68
81
  end
69
82
 
70
83
  module DSL
71
84
  class Top
72
- attr_reader :__sw_pages
85
+ attr_reader :__sw_pages, :__sw_before_hooks, :__sw_after_hooks
73
86
 
74
87
  def initialize
75
88
  @__sw_pages = []
89
+ @__sw_before_hooks = []
90
+ @__sw_after_hooks = []
91
+ end
92
+
93
+ def before(&block)
94
+ @__sw_before_hooks << block
76
95
  end
77
96
 
78
- def page(url, &block)
79
- page = Page.new(url)
97
+ def after(&block)
98
+ @__sw_after_hooks << block
99
+ end
100
+
101
+ def page(url, **opts, &block)
102
+ page = Page.new(url, **opts)
80
103
  page.instance_eval(&block)
81
104
  @__sw_pages << page
82
105
  end
@@ -84,11 +107,17 @@ class SiteWatcher
84
107
 
85
108
  class Page
86
109
  include ::RSpec::Matchers
87
- attr_reader :__sw_url
110
+ attr_reader :__sw_url, :__sw_remove_on_fulfillment
88
111
 
89
- def initialize(url)
112
+ def initialize(url, method: :get, headers: {}, **opts)
90
113
  @__sw_url = url
91
114
  @__sw_tests = []
115
+ @__sw_method = method
116
+ @__sw_headers = headers
117
+ @__sw_http_opts = opts
118
+ @__sw_fulfilled = nil
119
+ @__sw_fetch = nil
120
+ @__sw_remove_on_fulfillment = true
92
121
  end
93
122
 
94
123
  def test(&block)
@@ -99,24 +128,52 @@ class SiteWatcher
99
128
  @__sw_fulfilled = block
100
129
  end
101
130
 
102
- def __sw_run!(force=false)
103
- ::OpenURI.open_uri(@__sw_url) do |response|
104
- case response.content_type
131
+ def fetch(&block)
132
+ @__sw_fetch = block
133
+ end
134
+
135
+ def headers(hash)
136
+ @__sw_headers = hash
137
+ end
138
+
139
+ def http_method(http_method)
140
+ @__sw_http_method = http_method
141
+ end
142
+
143
+ def body(body)
144
+ @__sw_body = body
145
+ end
146
+
147
+ def remove_on_fulfillment(bool)
148
+ @__sw_remove_on_fulfillment = !!bool
149
+ end
150
+
151
+ def __sw_run!(force, logger, timeout)
152
+ if @__sw_fetch
153
+ response = @__sw_fetch.call(@__sw_url)
154
+ else
155
+ response = ::HTTP
156
+ .use(logging: {logger: logger})
157
+ .timeout(timeout)
158
+ .headers(@__sw_headers)
159
+ .request(@__sw_method, @__sw_url, **@__sw_http_opts)
160
+
161
+ case response.content_type.mime_type
105
162
  when /json/i
106
- page = ::JSON.parse(response.read)
163
+ response = ::JSON.parse(response.to_s)
107
164
  else
108
- page = ::Capybara::Node::Simple.new(response.read)
109
- end
110
-
111
- begin
112
- @__sw_tests.each { |test| test.call(page) }
113
- rescue ::RSpec::Expectations::ExpectationNotMetError => err
114
- __sw_fulfilled! if force
115
- raise(err)
165
+ response = ::Capybara::Node::Simple.new(response.to_s)
116
166
  end
167
+ end
117
168
 
118
- __sw_fulfilled!
169
+ begin
170
+ @__sw_tests.each { |test| test.call(response) }
171
+ rescue ::RSpec::Expectations::ExpectationNotMetError => err
172
+ __sw_fulfilled! if force
173
+ raise(err)
119
174
  end
175
+
176
+ __sw_fulfilled!
120
177
  end
121
178
 
122
179
  private
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.2
4
+ version: 1.3.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: 2015-01-30 00:00:00.000000000 Z
11
+ date: 2021-11-11 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
@@ -44,28 +58,28 @@ dependencies:
44
58
  requirements:
45
59
  - - "~>"
46
60
  - !ruby/object:Gem::Version
47
- version: '1.7'
61
+ version: '2.0'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
- version: '1.7'
68
+ version: '2.0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rake
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
73
  - - "~>"
60
74
  - !ruby/object:Gem::Version
61
- version: '10.0'
75
+ version: '13.0'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
- version: '10.0'
82
+ version: '13.0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rspec
71
85
  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
@@ -110,10 +138,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
138
  - !ruby/object:Gem::Version
111
139
  version: '0'
112
140
  requirements: []
113
- rubyforge_project:
114
- rubygems_version: 2.2.2
141
+ rubygems_version: 3.1.4
115
142
  signing_key:
116
143
  specification_version: 4
117
144
  summary: Periodically monitor a website
118
145
  test_files: []
119
- has_rdoc: