Forráskód Böngészése

Refactor StubRootPage

Thibaut 10 éve
szülő
commit
9ecb1f9498
3 módosított fájl, 30 hozzáadás és 42 törlés
  1. 24 0
      lib/docs/core/scraper.rb
  2. 3 21
      lib/docs/scrapers/angular.rb
  3. 3 21
      lib/docs/scrapers/meteor.rb

+ 24 - 0
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

+ 3 - 21
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")

+ 3 - 21
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)