From 9089d03211a7ccd8aa9827587fcd6e161d903ab0 Mon Sep 17 00:00:00 2001 From: Thibaut Date: Sat, 16 Nov 2013 16:32:43 +0100 Subject: [PATCH] Set the default FileScraper::base_url to localhost --- lib/docs/core/scrapers/file_scraper.rb | 11 ++++++-- .../docs/core/scrapers/file_scraper_test.rb | 27 +++++++++++-------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/docs/core/scrapers/file_scraper.rb b/lib/docs/core/scrapers/file_scraper.rb index 1138f78e..b71da75f 100644 --- a/lib/docs/core/scrapers/file_scraper.rb +++ b/lib/docs/core/scrapers/file_scraper.rb @@ -4,16 +4,23 @@ module Docs class << self attr_accessor :dir + + def inherited(subclass) + super + subclass.base_url = base_url + end end + self.base_url = 'http://localhost/' + private def request_one(url) Response.new read_file(file_path_for(url)), URL.parse(url) end - def request_all(start_url) - queue = [start_url] + def request_all(url) + queue = [url] until queue.empty? result = yield request_one(queue.shift) queue.concat(result) if result.is_a? Array diff --git a/test/lib/docs/core/scrapers/file_scraper_test.rb b/test/lib/docs/core/scrapers/file_scraper_test.rb index d438934f..d6ce9f8d 100644 --- a/test/lib/docs/core/scrapers/file_scraper_test.rb +++ b/test/lib/docs/core/scrapers/file_scraper_test.rb @@ -3,8 +3,7 @@ require 'docs' class FileScraperTest < MiniTest::Spec class Scraper < Docs::FileScraper - self.dir = '/dir' - self.base_url = 'http://example.com' + self.dir = '/' self.html_filters = Docs::FilterStack.new self.text_filters = Docs::FilterStack.new end @@ -17,19 +16,30 @@ class FileScraperTest < MiniTest::Spec OpenStruct.new body: 'body', url: Docs::URL.parse(Scraper.base_url) end + describe ".inherited" do + it "sets .base_url" do + assert_equal Scraper.base_url, Class.new(Scraper).base_url + end + end + describe "#request_one" do - let :url do - File.join(Scraper.base_url, 'path') + let :path do + File.join(Scraper.dir, 'path') end let :result do - scraper.send :request_one, url + scraper.send :request_one, path end before do stub(scraper).read_file end + it "reads a file" do + mock(scraper).read_file(path) + result + end + describe "the returned response object" do it "has a #body" do stub(scraper).read_file { 'body' } @@ -37,15 +47,10 @@ class FileScraperTest < MiniTest::Spec end it "has a #url" do - assert_equal url, result.url.to_s + assert_equal path, result.url.to_s assert_instance_of Docs::URL, result.url end end - - it "reads .dir/path when the url is .base_url/path" do - stub(scraper).read_file(File.join(Scraper.dir, 'path')) { 'test' } - assert_equal 'test', result.body - end end describe "#request_all" do