world_bank 0.0.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,26 +3,19 @@ module WorldBank
3
3
 
4
4
  attr_reader :raw, :id, :name, :code, :type
5
5
 
6
- def self.client
7
- @client ||= WorldBank::Client.new
8
- end
9
-
10
- def self.all(client)
11
- client.query[:dirs] = ['regions']
12
- client.get_query
6
+ def self.all
7
+ find('all')
13
8
  end
14
9
 
15
10
  def self.find(id)
16
- client.query[:dirs] = ['regions', id.to_s]
17
- result = client.get_query
18
- new(result[1][0])
11
+ WorldBank::ParamQuery.new('regions', id, self)
19
12
  end
20
13
 
21
14
  def initialize(values={})
22
15
  @raw = values
23
16
  @id = values['id']
24
- @name = values['name']
25
17
  @code = values['code']
18
+ @name = values['name']
26
19
  @type = 'regions'
27
20
  end
28
21
  end
@@ -1,21 +1,15 @@
1
1
  module WorldBank
2
2
 
3
3
  class Source
4
-
4
+
5
5
  attr_reader :raw, :id, :name, :description, :url, :type
6
6
 
7
- def self.client
8
- @client ||= WorldBank::Client.new
9
- end
10
- def self.all(client)
11
- client.query[:dirs] = ['sources']
12
- client.get_query
7
+ def self.all
8
+ find('all')
13
9
  end
14
-
10
+
15
11
  def self.find(id)
16
- client.query[:dirs] = ['sources', id.to_s]
17
- result = client.get_query
18
- new(result[1][0])
12
+ WorldBank::ParamQuery.new('sources', id, self)
19
13
  end
20
14
 
21
15
  def initialize(values={})
@@ -1,24 +1,17 @@
1
1
  module WorldBank
2
2
 
3
3
  class Topic
4
-
4
+
5
5
  attr_reader :raw, :id, :name, :note, :type
6
-
7
- def self.client
8
- @client ||= WorldBank::Client.new
9
- end
10
6
 
11
- def self.all(client)
12
- client.query[:dirs] = ['topics']
13
- client.get_query
7
+ def self.all
8
+ find('all')
14
9
  end
15
-
10
+
16
11
  def self.find(id)
17
- client.query[:dirs] = ['topics', id.to_s]
18
- result = client.get_query
19
- new(result[1][0])
12
+ WorldBank::ParamQuery.new('topics', id, self)
20
13
  end
21
-
14
+
22
15
  def initialize(values={})
23
16
  @raw = values
24
17
  @id = values['id']
@@ -26,7 +19,7 @@ module WorldBank
26
19
  @note = values['sourceNote']
27
20
  @type = 'topics'
28
21
  end
29
-
22
+
30
23
  end
31
-
24
+
32
25
  end
@@ -1,3 +1,3 @@
1
1
  module WorldBank
2
- VERSION = "0.0.1"
2
+ VERSION = "0.9.0"
3
3
  end
@@ -2,83 +2,15 @@ require 'helper'
2
2
 
3
3
  describe WorldBank::Client do
4
4
 
5
- before do
6
- @client = WorldBank::Client.new
7
- end
8
-
9
- context 'topics' do
10
- it 'returns all of the topics for the world bank' do
11
- stub_get('topics?format=json').
12
- to_return(:status => 200, :body => fixture('topics.json'))
13
- test = @client.topics
14
- a_get('topics?format=json').should have_been_made
15
- test[0][:total].should eql 18
16
- end
17
- end
18
-
19
- context 'indicators' do
20
- it 'returns all of the indicators for the world bank' do
21
- stub_get('indicators?format=json').
22
- to_return(:status => 200, :body => fixture('indicators.json'))
23
- test = @client.indicators
24
- a_get('indicators?format=json').should have_been_made
25
- test[0][:pages].should eql 82
26
- end
27
- end
28
-
29
- context 'countries' do
30
- it 'returns all of the countries for the world bank' do
31
- stub_get('countries?format=json').
32
- to_return(:status => 200, :body => fixture('countries.json'))
33
- test = @client.countries
34
- a_get('countries?format=json').should have_been_made
35
- test[0][:pages].should eql 5
36
- end
37
- end
38
-
39
- context 'lending_types' do
40
- it 'returns all of the lending types for the world bank' do
41
- stub_get('lendingTypes?format=json').
42
- to_return(:status => 200, :body => fixture('lending_types.json'))
43
- test = @client.lending_types
44
- a_get('lendingTypes?format=json').should have_been_made
45
- test[0][:total].should eql '4'
46
- end
47
- end
48
-
49
- context 'income_levels' do
50
- it 'returns all of the income levels for the world bank' do
51
- stub_get('incomeLevels?format=json').
52
- to_return(:status => 200, :body => fixture('income_levels.json'))
53
- test = @client.income_levels
54
- a_get('incomeLevels?format=json').should have_been_made
55
- test[0][:total].should eql '9'
56
- end
57
- end
58
-
59
- context 'sources' do
60
- it 'returns all of the sources for the world bank' do
61
- stub_get('sources?format=json').
62
- to_return(:status => 200, :body => fixture('sources.json'))
63
- test = @client.sources
64
- a_get('sources?format=json').should have_been_made
65
- test[0][:total].should eql '14'
66
- end
67
- end
68
-
69
5
  context 'get' do
70
6
  it 'returns the response from the specified path' do
71
- stub_get('sources?format=json').
7
+ stub_get('sources/all?format=json').
72
8
  to_return(:status => 200, :body => fixture('sources.json'))
73
- test = @client.sources
74
- a_get('sources?format=json').should have_been_made
75
- test[0][:total].should eql '14'
76
- end
77
- end
78
-
79
- context 'connection' do
80
- it 'returns an instance of Faraday' do
81
- @client.send(:connection).should be_a Faraday::Connection
9
+ client = WorldBank::Client.new( { :dirs => ['sources', 'all'],
10
+ :params => {:format => 'json'}
11
+ }, false)
12
+ client.get('sources/all?format=json')
13
+ a_get('sources/all?format=json').should have_been_made
82
14
  end
83
15
  end
84
16
 
@@ -1,47 +1,32 @@
1
1
  require 'helper'
2
2
 
3
3
  describe WorldBank::Country do
4
- context 'client' do
5
- it 'returns WorldBank::Client' do
6
- client = WorldBank::Country.client
7
- client.should be_a WorldBank::Client
8
- end
9
- end
10
- context 'find' do
11
- it 'returns the correct Country' do
4
+ context 'returned Country has' do
5
+ before do
12
6
  stub_get('countries/in?format=json').
13
7
  to_return(:status => 200, :body => fixture('countries_india.json'))
14
- india = WorldBank::Country.find('in')
15
- a_get('countries/in?format=json').should have_been_made
16
- india.should be_a WorldBank::Country
17
- end
18
- context 'returned Country has' do
19
- before do
20
- stub_get('countries/in?format=json').
21
- to_return(:status => 200, :body => fixture('countries_india.json'))
22
- @india = WorldBank::Country.find('in')
23
- end
24
- it 'a three letter code' do
25
- @india.iso3_code.should eql 'IND'
26
- end
27
- it 'a two letter code' do
28
- @india.iso2_code.should eql 'IN'
29
- end
30
- it 'a name' do
31
- @india.name.should eql 'India'
32
- end
33
- it 'an IncomeLevel' do
34
- @india.income_level.should be_a WorldBank::IncomeLevel
35
- end
36
- it 'a LendingType' do
37
- @india.lending_type.should be_a WorldBank::LendingType
38
- end
39
- it 'a capital' do
40
- @india.capital.should eql 'New Delhi'
41
- end
42
- it 'a region' do
43
- @india.region.should be_a WorldBank::Region
44
- end
8
+ @india = WorldBank::Country.find('in').fetch
9
+ end
10
+ it 'a three letter code' do
11
+ @india.iso3_code.should eql 'IND'
12
+ end
13
+ it 'a two letter code' do
14
+ @india.iso2_code.should eql 'IN'
15
+ end
16
+ it 'a name' do
17
+ @india.name.should eql 'India'
18
+ end
19
+ it 'an IncomeLevel' do
20
+ @india.income_level.should be_a WorldBank::IncomeLevel
21
+ end
22
+ it 'a LendingType' do
23
+ @india.lending_type.should be_a WorldBank::LendingType
24
+ end
25
+ it 'a capital' do
26
+ @india.capital.should eql 'New Delhi'
27
+ end
28
+ it 'a region' do
29
+ @india.region.should be_a WorldBank::Region
45
30
  end
46
31
  end
47
32
  end
@@ -0,0 +1,38 @@
1
+ require 'helper'
2
+
3
+ describe WorldBank::DataQuery do
4
+ context 'lending types' do
5
+ it 'accepts a single LendingType as param' do
6
+ stub_get('lendingTypes/IDB/indicators/all?format=json')
7
+ lending_type = WorldBank::LendingType.new({"id" => "IDB","value" => "Blend"})
8
+ WorldBank::Data.raw.lending_type(lending_type).fetch
9
+ a_get('lendingTypes/IDB/indicators/all?format=json').should have_been_made
10
+ end
11
+ end
12
+ context 'income levels' do
13
+ it 'accepts a single IncomeLevels as param' do
14
+ stub_get('incomeLevels/LMC/indicators/all?format=json')
15
+ WorldBank::Data.raw.income_level('LMC').fetch
16
+ a_get('incomeLevels/LMC/indicators/all?format=json').should have_been_made
17
+ end
18
+ end
19
+ context 'sources' do
20
+ it 'accepts a single Source as param' do
21
+ stub_get('sources/1/indicators/all?format=json')
22
+ WorldBank::Data.raw.source(1).fetch
23
+ a_get('sources/1/indicators/all?format=json').should have_been_made
24
+ end
25
+ end
26
+ context 'country' do
27
+ it 'adds countries to the params if given ISO-2 code' do
28
+ stub_get('countries/br/indicators/AB.LND.ARBL.ZS?format=json')
29
+ WorldBank::Data.find('AB.LND.ARBL.ZS').country('br').raw.fetch
30
+ a_get('countries/br/indicators/AB.LND.ARBL.ZS?format=json').should have_been_made
31
+ end
32
+ it 'adds countries to the params if given human name' do
33
+ stub_get('countries/BR/indicators/AB.LND.ARBL.ZS?format=json')
34
+ WorldBank::Data.find('AB.LND.ARBL.ZS').country('brazil').raw.fetch
35
+ a_get('countries/BR/indicators/AB.LND.ARBL.ZS?format=json').should have_been_made
36
+ end
37
+ end
38
+ end
@@ -1,18 +1,11 @@
1
1
  require 'helper'
2
2
 
3
3
  describe WorldBank::IncomeLevel do
4
- context 'client' do
5
- # my god these need to be refactored... but can they be? what about including client within????
6
- it 'returns a WorldBank::Client' do
7
- client = WorldBank::IncomeLevel.client
8
- client.should be_a WorldBank::Client
9
- end
10
- end
11
4
  context 'find' do
12
5
  it 'returns a WorldBank::IncomeLevel' do
13
6
  stub_get('incomeLevels/lmc?format=json').
14
7
  to_return(:status => 200, :body => fixture('income_level_lmc.json'))
15
- @working_class = WorldBank::IncomeLevel.find('lmc')
8
+ @working_class = WorldBank::IncomeLevel.find('lmc').fetch
16
9
  a_get('incomeLevels/lmc?format=json').should have_been_made
17
10
  @working_class.should be_a WorldBank::IncomeLevel
18
11
  end
@@ -20,7 +13,7 @@ describe WorldBank::IncomeLevel do
20
13
  before do
21
14
  stub_get('incomeLevels/lmc?format=json').
22
15
  to_return(:status => 200, :body => fixture('income_level_lmc.json'))
23
- @working_class = WorldBank::IncomeLevel.find('lmc')
16
+ @working_class = WorldBank::IncomeLevel.find('lmc').fetch
24
17
  end
25
18
  it 'an id' do
26
19
  @working_class.id.should eql 'LMC'
@@ -5,7 +5,7 @@ describe WorldBank::Indicator do
5
5
  it 'returns a WorldBank::Indicator' do
6
6
  stub_get('indicators/AG.AGR.TRAC.NO?format=json').
7
7
  to_return(:status => 200, :body => fixture('indicators_tractors.json'))
8
- tractors = WorldBank::Indicator.find('AG.AGR.TRAC.NO')
8
+ tractors = WorldBank::Indicator.find('AG.AGR.TRAC.NO').fetch
9
9
  a_get('indicators/AG.AGR.TRAC.NO?format=json').should have_been_made
10
10
  tractors.should be_a WorldBank::Indicator
11
11
  end
@@ -13,7 +13,7 @@ describe WorldBank::Indicator do
13
13
  before do
14
14
  stub_get('indicators/AG.AGR.TRAC.NO?format=json').
15
15
  to_return(:status => 200, :body => fixture('indicators_tractors.json'))
16
- @tractors = WorldBank::Indicator.find 'AG.AGR.TRAC.NO'
16
+ @tractors = WorldBank::Indicator.find('AG.AGR.TRAC.NO').fetch
17
17
  end
18
18
  it 'an id' do
19
19
  @tractors.id.should eql 'AG.AGR.TRAC.NO'
@@ -1,17 +1,11 @@
1
1
  require 'helper'
2
2
 
3
3
  describe WorldBank::LendingType do
4
- context 'client' do
5
- it 'should return a Client' do
6
- client = WorldBank::LendingType.client
7
- client.should be_a WorldBank::Client
8
- end
9
- end
10
4
  context 'find' do
11
5
  it 'returns a LendingType' do
12
6
  stub_get('lendingTypes/idb?format=json').
13
7
  to_return(:status => 200, :body => fixture('lending_type_idb.json'))
14
- @blend = WorldBank::LendingType.find('idb')
8
+ @blend = WorldBank::LendingType.find('idb').fetch
15
9
  a_get('lendingTypes/idb?format=json').should have_been_made
16
10
  @blend.should be_a WorldBank::LendingType
17
11
  end
@@ -19,7 +13,7 @@ describe WorldBank::LendingType do
19
13
  before do
20
14
  stub_get('lendingTypes/idb?format=json').
21
15
  to_return(:status => 200, :body => fixture('lending_type_idb.json'))
22
- @blend = WorldBank::LendingType.find('idb')
16
+ @blend = WorldBank::LendingType.find('idb').fetch
23
17
  end
24
18
  it 'an id' do
25
19
  @blend.id.should eql 'IDB'
@@ -0,0 +1,45 @@
1
+ require 'helper'
2
+
3
+ describe WorldBank::ParamQuery do
4
+ context 'lending types' do
5
+ it 'accepts a single LendingType as param' do
6
+ stub_get('countries/all?format=json&lendingTypes=IDB')
7
+ lending_type = WorldBank::LendingType.new({"id" => "IDB","value" => "Blend"})
8
+ WorldBank::Country.all.raw.lending_type(lending_type).fetch
9
+ a_get('countries/all?format=json&lendingTypes=IDB').should have_been_made
10
+ end
11
+ end
12
+ context 'income levels' do
13
+ it 'accepts a single IncomeLevels as param' do
14
+ stub_get('countries/all?format=json&incomeLevels=LMC')
15
+ WorldBank::Country.all.raw.income_level('LMC').fetch
16
+ a_get('countries/all?format=json&incomeLevels=LMC').should have_been_made
17
+ end
18
+ end
19
+ context 'sources' do
20
+ it 'accepts a single Source as param' do
21
+ stub_get('countries/all?format=json&sources=1')
22
+ WorldBank::Country.all.raw.source(1).fetch
23
+ a_get('countries/all?format=json&sources=1').should have_been_made
24
+ end
25
+ end
26
+ context 'featured_indicators' do
27
+ it 'adds featured param with value of 1' do
28
+ stub_get('indicators/all?format=json&featured=1')
29
+ WorldBank::Indicator.featured.raw.fetch
30
+ a_get('indicators/all?format=json&featured=1').should have_been_made
31
+ end
32
+ end
33
+ context 'country' do
34
+ it 'adds countries to the params if given ISO-2 code' do
35
+ stub_get('indicators/AB.LND.ARBL.ZS?format=json&countries=br')
36
+ WorldBank::Indicator.find('AB.LND.ARBL.ZS').country('br').raw.fetch
37
+ a_get('indicators/AB.LND.ARBL.ZS?format=json&countries=br').should have_been_made
38
+ end
39
+ it 'adds countries to the params if given human name' do
40
+ stub_get('indicators/AB.LND.ARBL.ZS?format=json&countries=BR')
41
+ WorldBank::Indicator.find('AB.LND.ARBL.ZS').country('brazil').raw.fetch
42
+ a_get('indicators/AB.LND.ARBL.ZS?format=json&countries=BR').should have_been_made
43
+ end
44
+ end
45
+ end
@@ -1,114 +1,74 @@
1
1
  require 'helper'
2
2
 
3
3
  describe WorldBank::Query do
4
- before do
5
- @india = {"id" => "IND",
6
- "iso2Code" => "IN",
7
- "name" => "India",
8
- "region" => {
9
- "id" => "SAS",
10
- "value" => "South Asia"},
11
- "adminregion" => {
12
- "id" => "SAS",
13
- "value" => "South Asia"},
14
- "incomeLevel" => {
15
- "id" => "LMC",
16
- "value" => "Lower middle income"},
17
- "lendingType" => {
18
- "id" => "IDB",
19
- "value" => "Blend"},
20
- "capitalCity" => "New Delhi",
21
- "longitude" => "77.225",
22
- "latitude" => "28.6353"}
23
- @country = WorldBank::Country.new(@india)
24
- @query = WorldBank::Query::Base.new(@country)
25
- @client = @query.instance_variable_get :@client
26
- end
27
- context 'client' do
28
- it 'returns WorldBank::Client' do
29
- @client.should be_a WorldBank::Client
30
- end
31
- end
32
4
  context 'dates' do
5
+ it 'should return self' do
6
+ WorldBank::Source.all.dates('something').should be_a_kind_of WorldBank::Query
7
+ end
33
8
  it 'adds a date string to the params hash' do
34
- @query.dates('2000:2010')
35
- @client.query[:params].should eql({:format => 'json', :date => '2000:2010'})
9
+ stub_get('sources/all?date=2000:2010&format=json')
10
+ WorldBank::Source.all.raw.dates('2000:2010').fetch
11
+ a_get('sources/all?date=2000:2010&format=json').should have_been_made
36
12
  end
37
13
  it 'adds a range of date objects to the params hash' do
38
- @birthday = Date.new(1982, 3, 7)
39
- @q3 = Date.new(2010, 9, 30)
40
- @query.dates(@birthday...@q3)
41
- @client.query[:params].should eql({:format => 'json', :date => '1982M03:2010M09'})
14
+ stub_get('sources/all?date=1982M03:2010M09&format=json')
15
+ birthday = Date.new(1982, 3, 7)
16
+ q3 = Date.new(2010, 9, 30)
17
+ WorldBank::Source.all.raw.dates(birthday...q3).fetch
18
+ a_get('sources/all?date=1982M03:2010M09&format=json').should have_been_made
42
19
  end
43
20
  end
44
21
  context 'format' do
45
22
  it 'adds a format string to the params' do
46
- @query.format('xml')
47
- @client.query[:params].should eql({:format => 'xml'})
48
- end
49
- end
50
- context 'id' do
51
- it 'adds an id string to the params' do
52
- @query.id(9)
53
- @client.query[:params].should eql({:format => 'json', :id => 9})
23
+ stub_get('sources/all?format=xml')
24
+ WorldBank::Source.all.raw.format(:xml).fetch
25
+ a_get('sources/all?format=xml').should have_been_made
54
26
  end
55
27
  end
56
28
  context 'most_recent_values' do
57
29
  it 'adds a MRV to the params' do
58
- @query.most_recent_values(10)
59
- @client.query[:params].should eql({:format => 'json', :MRV => 10})
60
- end
30
+ stub_get('sources/all?MRV=5&format=json')
31
+ WorldBank::Source.all.raw.most_recent_values(5).fetch
32
+ a_get('sources/all?MRV=5&format=json').should have_been_made end
61
33
  end
62
34
  context 'per page' do
63
35
  it 'adds a perPage string to the params' do
64
- @query.per_page(50)
65
- @client.query[:params].should eql({:format => 'json', :perPage => 50})
36
+ stub_get('sources/all?perPage=50&format=json')
37
+ WorldBank::Source.all.raw.per_page(50).fetch
38
+ a_get('sources/all?perPage=50&format=json').should have_been_made
66
39
  end
67
40
  end
68
41
  context 'page' do
69
- it 'adds a page param' do
70
- @query.page(3)
71
- @client.query[:params].should eql({:format => 'json', :page => 3})
42
+ it 'adds a page param when passed a value' do
43
+ stub_get('sources/all?page=3&format=json')
44
+ WorldBank::Source.all.raw.page(3).fetch
45
+ a_get('sources/all?page=3&format=json').should have_been_made
72
46
  end
73
47
  end
74
- context 'language' do
75
- it 'adds one of (en, es, fr, ar) to the dir array' do
76
- @query.language('es')
77
- @client.query[:dirs].should eql(['countries', 'es'])
48
+ context 'next' do
49
+ it 'increments the page count' do
50
+ stub_get('sources/all?page=3&format=json')
51
+ query = WorldBank::Source.all.raw.page(2)
52
+ query.next.fetch
53
+ a_get('sources/all?page=3&format=json').should have_been_made
78
54
  end
79
55
  end
80
- context 'lending types' do
81
- it 'accepts a single LendingType as param' do
82
- @query = WorldBank::Query::Base::Countries.new(@country)
83
- @client = @query.instance_variable_get :@client
84
- @lending_type = WorldBank::LendingType.new({"id" => "IDB","value" => "Blend"})
85
- @query.lending_types(@lending_type)
86
- @client.query[:params].should eql({:format => 'json', :lendingTypes => 'IDB'})
87
- end
88
- it 'converts an array of LendingTypes to a param' do
89
- @query = WorldBank::Query::Base::Countries.new(@country)
90
- @client = @query.instance_variable_get :@client
91
- @lending_type = WorldBank::LendingType.new({"id" => "IDB","value" => "Blend"})
92
- @other_type = @lending_type.dup
93
- @query.lending_types([@other_type, @lending_type])
94
- @client.query[:params].should eql({:format => 'json', :lendingTypes => 'IDB;IDB'})
56
+ context 'cycle' do
57
+ it 'cycles through all of the pages of results' do
58
+ 3.times do |i|
59
+ stub_get("sources/all?page=#{i+1}&format=json").
60
+ to_return(:status => 200, :body => ["#{i+1}"])
61
+ end
62
+ query = WorldBank::Source.all.raw.page(1)
63
+ query.instance_variable_set(:@pages, 3)
64
+ query.cycle.should == ["2", "3"]
95
65
  end
96
66
  end
97
- context 'income levels' do
98
- it 'accepts a single IncomeLevels as param' do
99
- @query = WorldBank::Query::Base::Countries.new(@country)
100
- @client = @query.instance_variable_get :@client
101
- @income_level = WorldBank::IncomeLevel.new({"id" => "LMC","value" => "Lower middle income"})
102
- @query.income_levels(@income_level)
103
- @client.query[:params].should eql({:format => 'json', :incomeLevels => 'LMC'})
104
- end
105
- it 'converts an array of IncomeLevels to a param' do
106
- @query = WorldBank::Query::Base::Countries.new(@country)
107
- @client = @query.instance_variable_get :@client
108
- @income_level = WorldBank::IncomeLevel.new({"id" => "LMC","value" => "Lower middle income"})
109
- @other_type = @income_level.dup
110
- @query.income_levels([@other_type, @income_level])
111
- @client.query[:params].should eql({:format => 'json', :incomeLevels => 'LMC;LMC'})
67
+ context 'language' do
68
+ it 'adds language param to the front of the query path' do
69
+ stub_get('es/sources/all?format=json')
70
+ WorldBank::Source.all.raw.language(:spanish).fetch
71
+ a_get('es/sources/all?format=json').should have_been_made
112
72
  end
113
73
  end
114
74
  end