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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4cd732ffdcde8dc20c8859e3206b5d3b860e7865
4
- data.tar.gz: 5ef83ae68a60de3e5ad236b9c6f4bbb757218769
3
+ metadata.gz: 611d593f137dee3cf2e533c2dc0a8e5d20f99e8b
4
+ data.tar.gz: e12b2633526805a0bbacf0197308644278716308
5
5
  SHA512:
6
- metadata.gz: 50552b68abf7e15d0c19b7b5a38e6fd46c13404ba2a79c2833ecec87dd58f761349e0be0c4cf8873c6bcddae31fd5bd0e2c1af42716a6cfc27751432011bd9e4
7
- data.tar.gz: 1a04911d8ed62afb356a253c87b0b36d6299c073798130042ad3c35f7dc31fcf86efbe98cdd4e460e050a16149692ea04789fdae168496dfec30ad485dd25b40
6
+ metadata.gz: 387dc11ed48e3e534915b06bab3ade86f50f72372b742d776e78f202679616ceb0897337cc559d457a9e61c9feffe56c4d29f54c6de11c27e4f9460e7f4f2b24
7
+ data.tar.gz: e31a2bd0ca5015727ce06cb1877cc5c14ecc53b6ca81333e32385d017e1db2749588bbe92cb7027f334ae224ca705c2f4aa7b8c8ba05520b044d3438e68cf8a1
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Snoop
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency 'sinatra'
26
26
 
27
27
  spec.add_runtime_dependency 'httparty'
28
+ spec.add_runtime_dependency 'nokogiri'
28
29
  end
@@ -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
@@ -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
- server_thread = Thread.new { Snoop::HttpServer.run! }
37
+ Thread.new { Snoop::HttpServer.run! }
27
38
 
28
39
  while true
29
40
  begin
@@ -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
- it 'fetches content over http' do
121
- response = stub('HttpResponse', body: 'stuff')
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
- http_client.should_receive(:get).with(url).and_return(response)
138
+ let(:body) { 'Awesome HTML' }
127
139
 
128
- expect(subject.fetch_content).to eq response.body
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.2
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