History

Refactor StubRootPage

Thibaut committed 11 years ago

Commit 9ecb1f91 parent e52bbcd3 files · +30 −42Browse files

Unified · Split

3 files
  1. lib/docs/core/scraper.rb +24 −0
  2. lib/docs/scrapers/angular.rb +3 −21
  3. lib/docs/scrapers/meteor.rb +3 −21
lib/docs/core/scraper.rb+24−0
@@ -149,5 +149,29 @@ module Docs · 148 unmodified lines
149149 def parse(string)
150150 Parser.new(string).html
151151 end
152+
153+ module StubRootPage
154+ private
155+
156+ def request_one(url)
157+ stub_root_page if url == root_url.to_s
158+ super
159+ end
160+
161+ def request_all(urls, &block)
162+ stub_root_page
163+ super
164+ end
165+
166+ def stub_root_page
167+ response = Typhoeus::Response.new(
168+ effective_url: root_url.to_s,
169+ code: 200,
170+ headers: { 'Content-Type' => 'text/html' },
171+ body: root_page_body)
172+
173+ Typhoeus.stub(root_url.to_s).and_return(response)
174+ end
175+ end
152176 end
153177 end
lib/docs/scrapers/angular.rb+3−21
@@ -1,5 +1,7 @@
11 module Docs
22 class Angular < UrlScraper
3+ include StubRootPage
4+
35 self.name = 'Angular.js'
46 self.slug = 'angular'
57 self.type = 'angular'
@@ -28,27 +30,7 @@ module Docs · 22 unmodified lines
2830
2931 private
3032
31− def request_one(url)
32− stub_root_page if url == root_url.to_s
33− super
34− end
35−
36− def request_all(urls, &block)
37− stub_root_page
38− super
39− end
40−
41− def stub_root_page
42− response = Typhoeus::Response.new(
43− effective_url: root_url.to_s,
44− code: 200,
45− headers: { 'Content-Type' => 'text/html' },
46− body: get_root_page_body)
47−
48− Typhoeus.stub(root_url.to_s).and_return(response)
49− end
50−
51− def get_root_page_body
33+ def root_page_body
5234 require 'capybara'
5335 Capybara.current_driver = :selenium
5436 Capybara.visit("https://code.angularjs.org/#{self.class.version}/docs/api")
lib/docs/scrapers/meteor.rb+3−21
@@ -1,5 +1,7 @@
11 module Docs
22 class Meteor < UrlScraper
3+ include StubRootPage
4+
35 self.type = 'meteor'
46 self.version = '1.1.0'
57 self.base_url = 'http://docs.meteor.com'
@@ -21,27 +23,7 @@ module Docs · 15 unmodified lines
2123
2224 private
2325
24− def request_one(url)
25− stub_root_page if url == root_url.to_s
26− super
27− end
28−
29− def request_all(urls, &block)
30− stub_root_page
31− super
32− end
33−
34− def stub_root_page
35− response = Typhoeus::Response.new(
36− effective_url: root_url.to_s,
37− code: 200,
38− headers: { 'Content-Type' => 'text/html' },
39− body: get_root_page_body)
40−
41− Typhoeus.stub(root_url.to_s).and_return(response)
42− end
43−
44− def get_root_page_body
26+ def root_page_body
4527 require 'capybara'
4628 Capybara.current_driver = :selenium
4729 Capybara.visit(root_url.to_s)