Skip to content
This repository has been archived by the owner on Jul 23, 2019. It is now read-only.

data query clears params on fetch to allow next / cycle queries #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/world_bank/data_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,5 +65,10 @@ def ensure_unconflicting_qualifiers
end
@params_filled = true
end

def clear_params!
@param_dir = []
@params_filled = false
end
end
end
26 changes: 26 additions & 0 deletions spec/world_bank/data_spec.rb
Original file line number Diff line number Diff line change
@@ -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