From 026b85a2e0eea2ecb489b982dc8478e4870bcbe4 Mon Sep 17 00:00:00 2001 From: Alexander Auritt Date: Fri, 10 Aug 2012 15:02:54 -0400 Subject: [PATCH] data query clears params on fetch to allow next / cycle queries --- lib/world_bank/data_query.rb | 16 +++++++++++++--- spec/world_bank/data_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 spec/world_bank/data_spec.rb diff --git a/lib/world_bank/data_query.rb b/lib/world_bank/data_query.rb index b4fd242..c951fad 100644 --- a/lib/world_bank/data_query.rb +++ b/lib/world_bank/data_query.rb @@ -4,10 +4,15 @@ class DataQuery < Query def initialize(name, id, model) super - @param_dir = [] - @params_filled = false + clear_params! end - + + def fetch + results = super + clear_params! + results + end + def lending_type(lending_type) ensure_unconflicting_qualifiers parsed = indifferent_number lending_type @@ -60,5 +65,10 @@ def ensure_unconflicting_qualifiers end @params_filled = true end + + def clear_params! + @param_dir = [] + @params_filled = false + end end end diff --git a/spec/world_bank/data_spec.rb b/spec/world_bank/data_spec.rb new file mode 100644 index 0000000..f4688a6 --- /dev/null +++ b/spec/world_bank/data_spec.rb @@ -0,0 +1,26 @@ +require 'helper' + +describe WorldBank::Data do + context "cycle" do + it "cycles through 2 pages of results for data query on all countries, specific indicator" do + 2.times do |i| + request_string = "countries/all/indicators/SP.POP.TOTL?page=#{i+1}&format=json" + stub_get(request_string). + to_return(:status => 200, :body => ["#{i+1}"]) + end + query = WorldBank::Data.country('all').indicator('SP.POP.TOTL').raw.page(1) + query.instance_variable_set(:@pages, 2) + query.cycle.should == ["2"] + end + it "cycles through all 5 pages of results for data query on all countries, specific indicator" do + 5.times do |i| + request_string = "countries/all/indicators/SP.POP.TOTL?page=#{i+1}&format=json" + stub_get(request_string). + to_return(:status => 200, :body => ["#{i+1}"]) + end + query = WorldBank::Data.country('all').indicator('SP.POP.TOTL').raw.page(1) + query.instance_variable_set(:@pages, 5) + query.cycle.should == ["2", "3", "4", "5"] + end + end +end \ No newline at end of file