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
149 def parse(string)149 def parse(string)
150 Parser.new(string).html150 Parser.new(string).html
151 end151 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
152 end176 end
153end177end
lib/docs/scrapers/angular.rb+3−21
@@ -1,5 +1,7 @@
1module Docs1module Docs
2 class Angular < UrlScraper2 class Angular < UrlScraper
3 include StubRootPage
4
3 self.name = 'Angular.js'5 self.name = 'Angular.js'
4 self.slug = 'angular'6 self.slug = 'angular'
5 self.type = 'angular'7 self.type = 'angular'
@@ -28,27 +30,7 @@ module Docs · 22 unmodified lines
28 30
29 private31 private
30 32
31 def request_one(url)33 def root_page_body
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
52 require 'capybara'34 require 'capybara'
53 Capybara.current_driver = :selenium35 Capybara.current_driver = :selenium
54 Capybara.visit("https://code.angularjs.org/#{self.class.version}/docs/api")36 Capybara.visit("https://code.angularjs.org/#{self.class.version}/docs/api")
lib/docs/scrapers/meteor.rb+3−21
@@ -1,5 +1,7 @@
1module Docs1module Docs
2 class Meteor < UrlScraper2 class Meteor < UrlScraper
3 include StubRootPage
4
3 self.type = 'meteor'5 self.type = 'meteor'
4 self.version = '1.1.0'6 self.version = '1.1.0'
5 self.base_url = 'http://docs.meteor.com'7 self.base_url = 'http://docs.meteor.com'
@@ -21,27 +23,7 @@ module Docs · 15 unmodified lines
21 23
22 private24 private
23 25
24 def request_one(url)26 def root_page_body
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
45 require 'capybara'27 require 'capybara'
46 Capybara.current_driver = :selenium28 Capybara.current_driver = :selenium
47 Capybara.visit(root_url.to_s)29 Capybara.visit(root_url.to_s)