From c6be9b6ae472fd85db5a84f4c3a40511e8a412d0 Mon Sep 17 00:00:00 2001 From: Thibaut Date: Wed, 11 Dec 2013 10:49:43 +0000 Subject: [PATCH] Make Docs:Requester#request accept an array of URLs --- lib/docs/core/requester.rb | 21 ++++--- test/lib/docs/core/requester_test.rb | 84 ++++++++++++++++++---------- 2 files changed, 70 insertions(+), 35 deletions(-) diff --git a/lib/docs/core/requester.rb b/lib/docs/core/requester.rb index fb086388..cae1ca5d 100644 --- a/lib/docs/core/requester.rb +++ b/lib/docs/core/requester.rb @@ -4,10 +4,10 @@ module Docs class Requester < Typhoeus::Hydra attr_reader :request_options - def self.run(url, options = {}, &block) + def self.run(urls, options = {}, &block) requester = new(options) requester.on_response(&block) if block - requester.request(url) + requester.request(urls) requester.run requester end @@ -18,11 +18,11 @@ module Docs super end - def request(url, options = {}, &block) - request = Request.new(url, request_options.merge(options)) - request.on_complete(&block) if block - queue(request) - request + def request(urls, options = {}, &block) + requests = [urls].flatten.map do |url| + build_and_queue_request(url, options, &block) + end + requests.length == 1 ? requests.first : requests end def queue(request) @@ -38,6 +38,13 @@ module Docs private + def build_and_queue_request(url, options, &block) + request = Request.new(url, request_options.merge(options)) + request.on_complete(&block) if block + queue(request) + request + end + def handle_response(response) on_response.each do |callback| result = callback.call(response) diff --git a/test/lib/docs/core/requester_test.rb b/test/lib/docs/core/requester_test.rb index 96f64c10..63eace1a 100644 --- a/test/lib/docs/core/requester_test.rb +++ b/test/lib/docs/core/requester_test.rb @@ -10,12 +10,16 @@ class DocsRequesterTest < MiniTest::Spec Docs::Requester.new(options) end + let :url do + 'http://example.com' + end + let :options do Hash.new end - let :url do - 'http://example.com' + let :block do + Proc.new {} end after do @@ -36,37 +40,62 @@ class DocsRequesterTest < MiniTest::Spec end describe "#request" do - it "returns a request" do - assert_instance_of Docs::Request, requester.request(url) - end - - describe "the request" do - it "is queued" do - request = requester.request(url) - assert_includes requester.queued_requests, request + context "with a url" do + it "returns a request" do + assert_instance_of Docs::Request, requester.request(url) end - it "has the given url" do - request = requester.request(url) - assert_equal url, request.base_url + describe "the request" do + it "is queued" do + request = requester.request(url) + assert_includes requester.queued_requests, request + end + + it "has the given url" do + request = requester.request(url) + assert_equal url, request.base_url + end + + it "has the default :request_options" do + options[:request_options] = { params: 'test' } + request = requester.request(url) + assert_equal 'test', request.options[:params] + end + + it "has the given options" do + options[:request_options] = { params: '' } + request = requester.request(url, params: 'test') + assert_equal 'test', request.options[:params] + end + + it "has the given block as an on_complete callback" do + request = requester.request(url, &block) + assert_includes request.on_complete, block + end end + end - it "has the default :request_options" do - options[:request_options] = { params: 'test' } - request = requester.request(url) - assert_equal 'test', request.options[:params] + context "with an array of urls" do + let :urls do + ['one', 'two'] end - it "has the given options" do - options[:request_options] = { params: '' } - request = requester.request(url, params: 'test') - assert_equal 'test', request.options[:params] + it "returns an array of requests" do + result = requester.request(urls, { params: 'test' }, &block) + assert_instance_of Array, result + assert_equal urls.length, result.length + assert result.all? { |obj| obj.instance_of? Docs::Request } + urls.each_with_index do |url, i| + assert_equal url, result[i].base_url + assert_equal 'test', result[i].options[:params] + assert_includes result[i].on_complete, block + end end - it "has the given block as an on_complete callback" do - block = Proc.new {} - request = requester.request(url, &block) - assert_includes request.on_complete, block + it "queues the requests in the given order" do + queue = [] + stub(requester).queue { |request| queue << request } + assert_equal urls, requester.request(urls).map(&:base_url) end end end @@ -77,9 +106,8 @@ class DocsRequesterTest < MiniTest::Spec end it "stores a callback" do - proc = Proc.new {} - requester.on_response(&proc) - assert_includes requester.on_response, proc + requester.on_response(&block) + assert_includes requester.on_response, block end end