snoop 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/snoop/http.rb +11 -3
- data/lib/snoop/version.rb +1 -1
- data/snoop.gemspec +1 -0
- data/spec/acceptance/http_spec.rb +12 -0
- data/spec/support/http_server.rb +12 -1
- data/spec/unit/http_spec.rb +37 -5
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 611d593f137dee3cf2e533c2dc0a8e5d20f99e8b
|
4
|
+
data.tar.gz: e12b2633526805a0bbacf0197308644278716308
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 387dc11ed48e3e534915b06bab3ade86f50f72372b742d776e78f202679616ceb0897337cc559d457a9e61c9feffe56c4d29f54c6de11c27e4f9460e7f4f2b24
|
7
|
+
data.tar.gz: e31a2bd0ca5015727ce06cb1877cc5c14ecc53b6ca81333e32385d017e1db2749588bbe92cb7027f334ae224ca705c2f4aa7b8c8ba05520b044d3438e68cf8a1
|
data/lib/snoop/http.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
require 'httparty'
|
2
|
+
require 'nokogiri'
|
2
3
|
|
3
4
|
module Snoop
|
4
5
|
class Http
|
5
6
|
UrlRequiredException = Class.new(StandardError)
|
6
7
|
|
7
|
-
attr_reader :url, :http_client, :interval, :content
|
8
|
+
attr_reader :url, :css, :http_client, :interval, :content
|
8
9
|
attr_accessor :content
|
9
10
|
|
10
|
-
def initialize(url: nil, http_client: HTTParty)
|
11
|
+
def initialize(url: nil, css: nil, http_client: HTTParty)
|
11
12
|
raise UrlRequiredException if url.nil?
|
12
13
|
|
13
14
|
@url = url
|
15
|
+
@css = css
|
14
16
|
@http_client = http_client
|
15
17
|
end
|
16
18
|
|
@@ -30,7 +32,13 @@ module Snoop
|
|
30
32
|
end
|
31
33
|
|
32
34
|
def fetch_content
|
33
|
-
http_client.get(url).body
|
35
|
+
content = http_client.get(url).body
|
36
|
+
|
37
|
+
if css
|
38
|
+
content = Nokogiri::HTML(content).css(css).map(&:text).join
|
39
|
+
end
|
40
|
+
|
41
|
+
content
|
34
42
|
end
|
35
43
|
end
|
36
44
|
end
|
data/lib/snoop/version.rb
CHANGED
data/snoop.gemspec
CHANGED
@@ -26,5 +26,17 @@ describe Snoop::Http do
|
|
26
26
|
|
27
27
|
expect(notification_count).to eq 1
|
28
28
|
end
|
29
|
+
|
30
|
+
it 'can target specific html elements' do
|
31
|
+
snoop = described_class.new(url: "#{url}/html-content", css: 'p#message')
|
32
|
+
|
33
|
+
html_content = nil
|
34
|
+
|
35
|
+
snoop.notify do |content|
|
36
|
+
html_content = content
|
37
|
+
end
|
38
|
+
|
39
|
+
expect(html_content).to eq 'Hello Rspec'
|
40
|
+
end
|
29
41
|
end
|
30
42
|
end
|
data/spec/support/http_server.rb
CHANGED
@@ -18,12 +18,23 @@ module Snoop
|
|
18
18
|
get '/static-content' do
|
19
19
|
'same ol stuff'
|
20
20
|
end
|
21
|
+
|
22
|
+
get '/html-content' do
|
23
|
+
<<-HTML
|
24
|
+
<html>
|
25
|
+
<body>
|
26
|
+
<h1>Awesome HTML</h1>
|
27
|
+
<p id='message'>Hello Rspec</p>
|
28
|
+
</body>
|
29
|
+
</html>
|
30
|
+
HTML
|
31
|
+
end
|
21
32
|
end
|
22
33
|
end
|
23
34
|
|
24
35
|
def with_http_server
|
25
36
|
url = "http://localhost:#{Snoop::HttpServer.port}"
|
26
|
-
|
37
|
+
Thread.new { Snoop::HttpServer.run! }
|
27
38
|
|
28
39
|
while true
|
29
40
|
begin
|
data/spec/unit/http_spec.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'snoop/http'
|
2
2
|
|
3
3
|
describe Snoop::Http do
|
4
|
-
subject { described_class.new(url: url) }
|
4
|
+
subject { described_class.new(url: url, css: css) }
|
5
5
|
|
6
6
|
let(:url) { 'http://example.com' }
|
7
|
+
let(:css) { nil }
|
7
8
|
|
8
9
|
describe '#initialize' do
|
9
10
|
it 'saves the url' do
|
@@ -19,6 +20,14 @@ describe Snoop::Http do
|
|
19
20
|
}.to raise_error Snoop::Http::UrlRequiredException
|
20
21
|
end
|
21
22
|
end
|
23
|
+
|
24
|
+
context 'when a css matches is provided' do
|
25
|
+
let(:css) { 'p#awesome' }
|
26
|
+
|
27
|
+
it 'saves the css matcher' do
|
28
|
+
expect(subject.css).to eq css
|
29
|
+
end
|
30
|
+
end
|
22
31
|
end
|
23
32
|
|
24
33
|
describe '#notify' do
|
@@ -117,15 +126,38 @@ describe Snoop::Http do
|
|
117
126
|
end
|
118
127
|
|
119
128
|
describe '#fetch_content' do
|
120
|
-
|
121
|
-
response = stub('HttpResponse', body:
|
129
|
+
before do
|
130
|
+
response = stub('HttpResponse', body: body)
|
131
|
+
|
122
132
|
http_client = stub('HttpClient')
|
133
|
+
http_client.should_receive(:get).with(url).and_return(response)
|
123
134
|
|
124
135
|
subject.stub(http_client: http_client)
|
136
|
+
end
|
125
137
|
|
126
|
-
|
138
|
+
let(:body) { 'Awesome HTML' }
|
127
139
|
|
128
|
-
|
140
|
+
it 'fetches content over http' do
|
141
|
+
expect(subject.fetch_content).to eq body
|
142
|
+
end
|
143
|
+
|
144
|
+
context 'when a css selector is provided' do
|
145
|
+
let(:body) {<<-HTML
|
146
|
+
<html>
|
147
|
+
<body>
|
148
|
+
This is some annoying text we don't want to see.
|
149
|
+
<p id='message'>#{content}</p>
|
150
|
+
</body>
|
151
|
+
</html>
|
152
|
+
HTML
|
153
|
+
}
|
154
|
+
|
155
|
+
let(:css) { 'p#message' }
|
156
|
+
let(:content) { 'Hello RSpec!' }
|
157
|
+
|
158
|
+
it 'only fetches matching content' do
|
159
|
+
expect(subject.fetch_content).to eq content
|
160
|
+
end
|
129
161
|
end
|
130
162
|
end
|
131
163
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snoop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Hunt
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - '>='
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: nokogiri
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
97
111
|
description: Monitor content for changes
|
98
112
|
email:
|
99
113
|
- c@chrishunt.co
|