From 9ecb1f94984ad26bf0bee0a0f4d3f7227e576b77 Mon Sep 17 00:00:00 2001 From: Thibaut Date: Sun, 5 Apr 2015 09:44:11 -0400 Subject: [PATCH] Refactor StubRootPage --- lib/docs/core/scraper.rb | 24 ++++++++++++++++++++++++ lib/docs/scrapers/angular.rb | 24 +++--------------------- lib/docs/scrapers/meteor.rb | 24 +++--------------------- 3 files changed, 30 insertions(+), 42 deletions(-) diff --git a/lib/docs/core/scraper.rb b/lib/docs/core/scraper.rb index 634ee857..4e0edadb 100644 --- a/lib/docs/core/scraper.rb +++ b/lib/docs/core/scraper.rb @@ -149,5 +149,29 @@ module Docs def parse(string) Parser.new(string).html end + + module StubRootPage + private + + def request_one(url) + stub_root_page if url == root_url.to_s + super + end + + def request_all(urls, &block) + stub_root_page + super + end + + def stub_root_page + response = Typhoeus::Response.new( + effective_url: root_url.to_s, + code: 200, + headers: { 'Content-Type' => 'text/html' }, + body: root_page_body) + + Typhoeus.stub(root_url.to_s).and_return(response) + end + end end end diff --git a/lib/docs/scrapers/angular.rb b/lib/docs/scrapers/angular.rb index 20214c5b..fa364c41 100644 --- a/lib/docs/scrapers/angular.rb +++ b/lib/docs/scrapers/angular.rb @@ -1,5 +1,7 @@ module Docs class Angular < UrlScraper + include StubRootPage + self.name = 'Angular.js' self.slug = 'angular' self.type = 'angular' @@ -28,27 +30,7 @@ module Docs private - def request_one(url) - stub_root_page if url == root_url.to_s - super - end - - def request_all(urls, &block) - stub_root_page - super - end - - def stub_root_page - response = Typhoeus::Response.new( - effective_url: root_url.to_s, - code: 200, - headers: { 'Content-Type' => 'text/html' }, - body: get_root_page_body) - - Typhoeus.stub(root_url.to_s).and_return(response) - end - - def get_root_page_body + def root_page_body require 'capybara' Capybara.current_driver = :selenium Capybara.visit("https://code.angularjs.org/#{self.class.version}/docs/api") diff --git a/lib/docs/scrapers/meteor.rb b/lib/docs/scrapers/meteor.rb index 7bd87019..8194149f 100644 --- a/lib/docs/scrapers/meteor.rb +++ b/lib/docs/scrapers/meteor.rb @@ -1,5 +1,7 @@ module Docs class Meteor < UrlScraper + include StubRootPage + self.type = 'meteor' self.version = '1.1.0' self.base_url = 'http://docs.meteor.com' @@ -21,27 +23,7 @@ module Docs private - def request_one(url) - stub_root_page if url == root_url.to_s - super - end - - def request_all(urls, &block) - stub_root_page - super - end - - def stub_root_page - response = Typhoeus::Response.new( - effective_url: root_url.to_s, - code: 200, - headers: { 'Content-Type' => 'text/html' }, - body: get_root_page_body) - - Typhoeus.stub(root_url.to_s).and_return(response) - end - - def get_root_page_body + def root_page_body require 'capybara' Capybara.current_driver = :selenium Capybara.visit(root_url.to_s)