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.
@@ -0,0 +1,6 @@
1
+ 0.0.1
2
+ Initial release
3
+ 0.9.0
4
+ Replaced old query builder with a new Query object
5
+ Factored querying Data into its own class
6
+ Better test coverage
@@ -1,9 +1,11 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '/world_bank/client'))
2
2
 
3
3
  module WorldBank
4
-
5
- def self.client(options={})
6
- WorldBank::Client.new(options)
4
+
5
+ def self.client(query={}, raw=false)
6
+ defaults = {:params => {:format => :json}, :dirs => []}
7
+ defaults.merge!(query)
8
+ WorldBank::Client.new(defaults, raw)
7
9
  end
8
10
 
9
11
  # Delegate to WorldBank::Client.new
@@ -15,5 +17,5 @@ module WorldBank
15
17
  def self.respond_to?(method, include_private=false)
16
18
  client.respond_to?(method, include_private) || super(method, include_private)
17
19
  end
18
-
20
+
19
21
  end
@@ -1,52 +1,26 @@
1
1
  require 'faraday_middleware'
2
- require File.expand_path(File.join(File.dirname(__FILE__), '/source'))
3
- require File.expand_path(File.join(File.dirname(__FILE__), '/income_level'))
4
- require File.expand_path(File.join(File.dirname(__FILE__), '/lending_type'))
5
- require File.expand_path(File.join(File.dirname(__FILE__), '/country'))
6
- require File.expand_path(File.join(File.dirname(__FILE__), '/indicator'))
7
- require File.expand_path(File.join(File.dirname(__FILE__), '/topic'))
2
+ require File.expand_path(File.join(File.dirname(__FILE__), 'source'))
3
+ require File.expand_path(File.join(File.dirname(__FILE__), 'income_level'))
4
+ require File.expand_path(File.join(File.dirname(__FILE__), 'lending_type'))
5
+ require File.expand_path(File.join(File.dirname(__FILE__), 'country'))
6
+ require File.expand_path(File.join(File.dirname(__FILE__), 'indicator'))
7
+ require File.expand_path(File.join(File.dirname(__FILE__), 'topic'))
8
8
  require File.expand_path(File.join(File.dirname(__FILE__), 'region'))
9
9
  require File.expand_path(File.join(File.dirname(__FILE__), 'query'))
10
+ require File.expand_path(File.join(File.dirname(__FILE__), 'data'))
11
+ require File.expand_path(File.join(File.dirname(__FILE__), 'param_query'))
12
+ require File.expand_path(File.join(File.dirname(__FILE__), 'data_query'))
10
13
 
11
14
  module WorldBank
12
15
  class Client
13
16
 
14
17
  attr_accessor :query
15
18
 
16
- def initialize(options={})
17
- @format = options[:format] || 'json'
18
- @query = {:params => {}, :dirs => []}
19
- @query[:params][:format] = @format
19
+ def initialize(query, raw)
20
+ @query = query
21
+ @raw = raw
20
22
  end
21
23
 
22
- def sources
23
- WorldBank::Source.all(self)
24
- end
25
-
26
- def income_levels
27
- WorldBank::IncomeLevel.all(self)
28
- end
29
-
30
- def lending_types
31
- WorldBank::LendingType.all(self)
32
- end
33
-
34
- def countries
35
- WorldBank::Country.all(self)
36
- end
37
-
38
- def indicators
39
- WorldBank::Indicator.all(self)
40
- end
41
-
42
- def regions
43
- WorldBank::Region.all(self)
44
- end
45
-
46
- def topics
47
- WorldBank::Topic.all(self)
48
- end
49
-
50
24
  def get_query
51
25
  @path = @query[:dirs].join('/')
52
26
  @path += '?'
@@ -57,32 +31,31 @@ module WorldBank
57
31
  @path += params.join('&')
58
32
  get(@path)
59
33
  end
60
-
34
+
61
35
  def get(path, headers={})
62
- response = connection.get do |request|
63
- request.url(path, headers)
64
- end
36
+ response = connection.get do |request|
37
+ request.url(path, headers)
38
+ end
65
39
  response.body
66
- end
40
+ end
67
41
 
68
42
  private
69
-
43
+
70
44
  def connection
71
45
  Faraday.new(:url => 'http://api.worldbank.org/') do |connection|
72
46
  connection.use Faraday::Request::UrlEncoded
73
47
  connection.use Faraday::Response::RaiseError
74
48
  connection.use Faraday::Response::Mashify
75
- case @format.to_s.downcase
76
- when 'json'
77
- connection.use Faraday::Response::ParseJson
78
- when 'jsonp'
79
- connection.use Faraday::Response::ParseJson
80
- when 'xml'
81
- connection.use Faraday::Response::ParseXml
82
- when 'raw'
83
- end
49
+ unless @raw
50
+ case @query[:params][:format].to_s.downcase
51
+ when 'json'
52
+ connection.use Faraday::Response::ParseJson
53
+ when 'xml'
54
+ connection.use Faraday::Response::ParseXml
55
+ end
56
+ end
84
57
  connection.adapter(Faraday.default_adapter)
85
58
  end
86
- end
59
+ end
87
60
  end
88
61
  end
@@ -1,24 +1,321 @@
1
1
  module WorldBank
2
2
 
3
3
  class Country
4
-
4
+
5
+ COUNTRY_ALIASES = [
6
+ ['AW', 'ABW', 'aruba'],
7
+ ['AF', 'AFG', 'afganistan'],
8
+ ['AO', 'AGO', 'angola'],
9
+ ['AI', 'AIA', 'anguilla'],
10
+ ['AX', 'ALA', 'aland_islands'],
11
+ ['AL', 'ALB', 'albania'],
12
+ ['AD', 'AND', 'andorra'],
13
+ ['AE', 'ARE', 'united_arab_emirates'],
14
+ ['AR', 'ARG', 'argentina'],
15
+ ['AM', 'ARM', 'armenia'],
16
+ ['AS', 'ASM', 'american_samoa'],
17
+ ['AQ', 'ATA', 'antarctica'],
18
+ ['TF', 'ATF', 'french_southern_territories'],
19
+ ['AG', 'ATG', 'antigua_and_barbuda'],
20
+ ['AU', 'AUS', 'australia'],
21
+ ['AT', 'AUT', 'austria'],
22
+ ['AZ', 'AZE', 'azerbaijan'],
23
+ ['BI', 'BDI', 'burundi'],
24
+ ['BE', 'BEL', 'belgium'],
25
+ ['BJ', 'BEN', 'benin'],
26
+ ['BQ', 'BES', 'bonaire_saint_eustatius_and_saba'],
27
+ ['BF', 'BFA', 'burkina_faso'],
28
+ ['BD', 'BGD', 'bangladesh'],
29
+ ['BG', 'BGR', 'bulgaria'],
30
+ ['BH', 'BHR', 'bahrain'],
31
+ ['BS', 'BHS', 'bahamas'],
32
+ ['BA', 'BIH', 'bosnia_and_herzegovina'],
33
+ ['BL', 'BLM', 'saint_barthelemy'],
34
+ ['BY', 'BLR', 'belarus'],
35
+ ['BZ', 'BLZ', 'belize'],
36
+ ['BM', 'BMU', 'bermuda'],
37
+ ['BO', 'BOL', 'plurinational_state_of_bolivia'],
38
+ ['BR', 'BRA', 'brazil'],
39
+ ['BB', 'BRB', 'barbados'],
40
+ ['BN', 'BRN', 'brunei_darussalam'],
41
+ ['BT', 'BTN', 'bhutan'],
42
+ ['BV', 'BVT', 'bouvet_island'],
43
+ ['BW', 'BWA', 'botswana'],
44
+ ['CF', 'CAF', 'central_african_republic'],
45
+ ['CA', 'CAN', 'canada'],
46
+ ['CC', 'CCK', 'cocos_keeling_islands'],
47
+ ['CH', 'CHE', 'switzerland'],
48
+ ['CL', 'CHL', 'chile'],
49
+ ['CN', 'CHN', 'china'],
50
+ ['CI', 'CIV', 'cote_divoire'],
51
+ ['CM', 'CMR', 'cameroon'],
52
+ ['CD', 'COD', 'democratic_republic_of_the_congo'],
53
+ ['CG', 'COG', 'congo'],
54
+ ['CK', 'COK', 'cook_islands'],
55
+ ['CO', 'COL', 'colombia'],
56
+ ['KM', 'COM', 'comoros'],
57
+ ['CV', 'CPV', 'cape_verde'],
58
+ ['CR', 'CRI', 'costa_rica'],
59
+ ['CU', 'CUB', 'cuba'],
60
+ ['CW', 'CUW', 'curacao'],
61
+ ['CX', 'CXR', 'christmas_island'],
62
+ ['KY', 'CYM', 'cayman_islands'],
63
+ ['CY', 'CYP', 'cyprus'],
64
+ ['CZ', 'CZE', 'czech_republic'],
65
+ ['DE', 'DEU', 'germany'],
66
+ ['DJ', 'DJI', 'djibouti'],
67
+ ['DM', 'DMA', 'dominca'],
68
+ ['DK', 'DNK', 'denmark'],
69
+ ['DO', 'DOM', 'dominican_republic'],
70
+ ['DZ', 'DZA', 'algeria'],
71
+ ['EC', 'ECU', 'ecuador'],
72
+ ['EG', 'EGY', 'egypt'],
73
+ ['ER', 'ERI', 'eritrea'],
74
+ ['EH', 'ESH', 'western_sahara'],
75
+ ['ES', 'ESP', 'spain'],
76
+ ['EE', 'EST', 'estonia'],
77
+ ['ET', 'ETH', 'ethiopia'],
78
+ ['FI', 'FIN', 'finland'],
79
+ ['FJ', 'FJI', 'fiji'],
80
+ ['FK', 'FLK', 'falkland_islands_malvinas'],
81
+ ['FR', 'FRA', 'france'],
82
+ ['FO', 'FRO', 'faroe_islands'],
83
+ ['FM', 'FSM', 'federated_states_of_micronesia'],
84
+ ['GA', 'GAB', 'gabon'],
85
+ ['GB', 'GBR', 'united_kingdom'],
86
+ ['GE', 'GEO', 'georgia'],
87
+ ['GG', 'GGY', 'guernsey'],
88
+ ['GH', 'GHA', 'ghana'],
89
+ ['GI', 'GIB', 'gibraltar'],
90
+ ['GN', 'GIN', 'guinea'],
91
+ ['GP', 'GLP', 'guadeloupe'],
92
+ ['GM', 'GMB', 'gambia'],
93
+ ['GW', 'GNB', 'guinea_bissau'],
94
+ ['GQ', 'GNQ', 'equatorial_guinea'],
95
+ ['GR', 'GRC', 'greece'],
96
+ ['GD', 'GRD', 'grenada'],
97
+ ['GL', 'GRL', 'greenland'],
98
+ ['GT', 'GTM', 'guatemala'],
99
+ ['GF', 'GUF', 'french_guiana'],
100
+ ['GU', 'GUM', 'guam'],
101
+ ['GY', 'GUY', 'guyana'],
102
+ ['HK', 'HKG', 'hong_kong'],
103
+ ['HM', 'HMD', 'heard_island_mcdonald_islands'],
104
+ ['HN', 'HND', 'honduras'],
105
+ ['HR', 'HRV', 'croatia'],
106
+ ['HT', 'HTI', 'haiti'],
107
+ ['HU', 'HUN', 'hungary'],
108
+ ['ID', 'IDN', 'indonesia'],
109
+ ['IM', 'IMN', 'isle_of_man'],
110
+ ['IN', 'IND', 'india'],
111
+ ['IO', 'IOT', 'british_indian_ocean_territory'],
112
+ ['IE', 'IRL', 'ireland'],
113
+ ['IR', 'IRN', 'islamic_republic_of_iran'],
114
+ ['IQ', 'IRQ', 'iraq'],
115
+ ['IS', 'ISL', 'iceland'],
116
+ ['IL', 'ISR', 'israel'],
117
+ ['IT', 'ITA', 'italy'],
118
+ ['JM', 'JAM', 'jamaica'],
119
+ ['JE', 'JEY', 'jersey'],
120
+ ['JO', 'JOR', 'jordan'],
121
+ ['JP', 'JPN', 'japan'],
122
+ ['KZ', 'KAZ', 'kazakhstan'],
123
+ ['KE', 'KEN', 'kenya'],
124
+ ['KG', 'KGZ', 'kyrgyzstan'],
125
+ ['KH', 'KHM', 'cambodia'],
126
+ ['KI', 'KIR', 'kiribati'],
127
+ ['KN', 'KNA', 'saint_kitts_and_nevis'],
128
+ ['KR', 'KOR', 'republic_of_korea'],
129
+ ['KW', 'KWT', 'kuwait'],
130
+ ['LA', 'LAO', 'lao_peoples_democratic_republic'],
131
+ ['LB', 'LBN', 'lebanon'],
132
+ ['LR', 'LBR', 'liberia'],
133
+ ['LY', 'LBY', 'libyan_arab_jamahiriya'],
134
+ ['LC', 'LCA', 'saint_lucia'],
135
+ ['LI', 'LIE', 'liechtenstein'],
136
+ ['LK', 'LKA', 'sri_lanka'],
137
+ ['LS', 'LSO', 'lesotho'],
138
+ ['LT', 'LTU', 'lithuania'],
139
+ ['LU', 'LUX', 'luxembourg'],
140
+ ['LV', 'LVA', 'latvia'],
141
+ ['MO', 'MAC', 'macao'],
142
+ ['MF', 'MAF', 'saint_martin'],
143
+ ['MA', 'MAR', 'morocco'],
144
+ ['MC', 'MCO', 'monaco'],
145
+ ['MD', 'MDA', 'republic_of_moldova'],
146
+ ['MG', 'MDG', 'madagascar'],
147
+ ['MV', 'MDV', 'maldives'],
148
+ ['MX', 'MEX', 'mexico'],
149
+ ['MH', 'MHL', 'marshall_islands'],
150
+ ['MK', 'MKD', 'the_former_yugoslav_republic_of_macedonia'],
151
+ ['ML', 'MLI', 'mali'],
152
+ ['MT', 'MLT', 'malta'],
153
+ ['MM', 'MMR', 'myanmar'],
154
+ ['ME', 'MNE', 'montenegro'],
155
+ ['MN', 'MNG', 'mongolia'],
156
+ ['MP', 'MNP', 'northern_mariana_islands'],
157
+ ['MZ', 'MOZ', 'mozambique'],
158
+ ['MR', 'MRT', 'mauritania'],
159
+ ['MS', 'MSR', 'montserrat'],
160
+ ['MQ', 'MTQ', 'martinique'],
161
+ ['MU', 'MUS', 'mauritius'],
162
+ ['MW', 'MWI', 'malawi'],
163
+ ['MY', 'MYS', 'malaysia'],
164
+ ['YT', 'MYT', 'mayotte'],
165
+ ['NA', 'NAM', 'namibia'],
166
+ ['NC', 'NCL', 'new_caledonia'],
167
+ ['NE', 'NER', 'niger'],
168
+ ['NF', 'NFK', 'norfolk_island'],
169
+ ['NG', 'NGA', 'nigeria'],
170
+ ['NI', 'NIC', 'nicaragua'],
171
+ ['NU', 'NIU', 'niue'],
172
+ ['NL', 'NLD', 'netherlands'],
173
+ ['NO', 'NOR', 'norway'],
174
+ ['NP', 'NPL', 'nepal'],
175
+ ['NR', 'NRU', 'nauru'],
176
+ ['NZ', 'NZL', 'new_zealand'],
177
+ ['OM', 'OMN', 'oman'],
178
+ ['PK', 'PAK', 'pakistan'],
179
+ ['PA', 'PAN', 'panama'],
180
+ ['PN', 'PCN', 'pitcairn'],
181
+ ['PE', 'PER', 'peru'],
182
+ ['PH', 'PHL', 'philippines'],
183
+ ['PW', 'PLW', 'palau'],
184
+ ['PG', 'PNG', 'papua_new_guinea'],
185
+ ['PL', 'POL', 'poland'],
186
+ ['PR', 'PRI', 'puerto_rico'],
187
+ ['KP', 'PRK', 'democratic_peoples_republic_of_korea'],
188
+ ['PT', 'PRT', 'portugal'],
189
+ ['PY', 'PRY', 'paraguay'],
190
+ ['PS', 'PSE', 'occupied_palestinian_territory'],
191
+ ['PF', 'PYF', 'french_polynesia'],
192
+ ['QA', 'QAT', 'qatar'],
193
+ ['RE', 'REU', 'reunion'],
194
+ ['RO', 'ROU', 'romania'],
195
+ ['RU', 'RUS', 'russian_federation'],
196
+ ['RW', 'RWA', 'rwanda'],
197
+ ['SA', 'SAU', 'saudi_arabia'],
198
+ ['SD', 'SDN', 'sudan'],
199
+ ['SN', 'SEN', 'senegal'],
200
+ ['SG', 'SGP', 'singapore'],
201
+ ['GS', 'SGS', 'south_georgia_and_the_south_sandwich_islands'],
202
+ ['SH', 'SHN', 'saint_helena_ascension_and_tristan_da_cunha'],
203
+ ['SJ', 'SJM', 'svalbard_and_jan_mayen'],
204
+ ['SB', 'SLB', 'soloman_islands'],
205
+ ['SL', 'SLE', 'sierra_leone'],
206
+ ['SV', 'SLV', 'el_salvador'],
207
+ ['SM', 'SMR', 'san_marino'],
208
+ ['SO', 'SOM', 'somalia'],
209
+ ['PM', 'SPM', 'saint_pierre_and_miquelon'],
210
+ ['RS', 'SRB', 'serbia'],
211
+ ['ST', 'STP', 'sao_tome_and_principe'],
212
+ ['SR', 'SUR', 'suriname'],
213
+ ['SK', 'SVK', 'slovakia'],
214
+ ['SI', 'SVN', 'slovenia'],
215
+ ['SE', 'SWE', 'sweden'],
216
+ ['SZ', 'SWZ', 'swaziland'],
217
+ ['SX', 'SXM', 'sint_maarten'],
218
+ ['SC', 'SYC', 'seychelles'],
219
+ ['SY', 'SYR', 'syrian_arab_republic'],
220
+ ['TC', 'TCA', 'turks_caicos_islands'],
221
+ ['TD', 'TCD', 'chad'],
222
+ ['TG', 'TGO', 'togo'],
223
+ ['TH', 'THA', 'thailand'],
224
+ ['TJ', 'TJK', 'tajikistan'],
225
+ ['TK', 'TKL', 'tokelau'],
226
+ ['TM', 'TKM', 'turkmenistan'],
227
+ ['TL', 'TLS', 'timor_leste'],
228
+ ['TO', 'TON', 'tonga'],
229
+ ['TT', 'TTO', 'trinidad_and_tobago'],
230
+ ['TN', 'TUN', 'tunisia'],
231
+ ['TR', 'TUR', 'turkey'],
232
+ ['TU', 'TUV', 'tuvalu'],
233
+ ['TW', 'TWN', 'taiwan_provice_of_china'],
234
+ ['TZ', 'TZA', 'united_republic_of_tanzania'],
235
+ ['UG', 'UGA', 'uganda'],
236
+ ['UA', 'UKR', 'ukraine'],
237
+ ['UM', 'UMI', 'united_states_minor_outlying_islands'],
238
+ ['UY', 'URY', 'uruguay'],
239
+ ['US', 'USA', 'united_states_of_america'],
240
+ ['UZ', 'UZB', 'uzbekistan'],
241
+ ['VC', 'VCT', 'saint_vincent_and_the_grenadines'],
242
+ ['VE', 'VEN', 'bolivarian_republic_of_venezuela'],
243
+ ['VG', 'VGB', 'british_virgin_islands'],
244
+ ['VI', 'VIR', 'us_virgin_islands'],
245
+ ['VN', 'VNM', 'viet_nam'],
246
+ ['VU', 'VUT', 'vanuatu'],
247
+ ['WF', 'WLF', 'wallis_and_futuna'],
248
+ ['WS', 'WSM', 'samoa'],
249
+ ['YE', 'YEM', 'yemen'],
250
+ ['ZA', 'ZAF', 'south_africa'],
251
+ ['ZM', 'ZMB', 'zambia'],
252
+ ['ZW', 'ZWE', 'zimbabwe']
253
+ ]
254
+
5
255
  attr_reader :raw, :name, :iso2_code, :iso3_code, :region, :income_level, :lending_type, :capital, :type
6
256
 
7
- def self.client
8
- @client ||= WorldBank::Client.new
257
+ def self.country_aliases
258
+ COUNTRY_ALIASES
9
259
  end
10
260
 
11
- def self.all(client)
12
- client.query[:dirs] = ['countries']
13
- client.get_query
261
+ def self.format(arg)
262
+ find('all').format(arg)
263
+ end
264
+
265
+ def self.id(arg)
266
+ find('all').id(arg)
267
+ end
268
+
269
+ def self.most_recent_values(arg)
270
+ find('all').most_recent_values(arg)
271
+ end
272
+
273
+ def self.page(arg)
274
+ find('all').page(arg)
275
+ end
276
+
277
+ def self.per_page
278
+ find('all').per_page(arg)
279
+ end
280
+
281
+ def self.language(arg)
282
+ find('all').language(arg)
283
+ end
284
+
285
+ def self.income_level(arg)
286
+ find('all').income_level(arg)
287
+ end
288
+
289
+ def self.lending_type(arg)
290
+ find('all').lending_type(arg)
291
+ end
292
+
293
+ def self.region(arg)
294
+ find('all').region(arg)
295
+ end
296
+
297
+ def self.country
298
+ find('all').country(arg)
299
+ end
300
+
301
+ def self.indicators(arg)
302
+ find('all').indicator(arg)
303
+ end
304
+
305
+ def self.fetch(arg)
306
+ find(arg).fetch
307
+ end
308
+
309
+ def self.all
310
+ find('all')
14
311
  end
15
312
 
16
313
  def self.find(id)
17
- client.query[:dirs] = ['countries', id.to_s]
18
- result = client.get_query
19
- new(result[1][0])
314
+ id = normalize_id id
315
+ id = ensure_id id unless id =~ /all/
316
+ WorldBank::ParamQuery.new('countries', id, self)
20
317
  end
21
-
318
+
22
319
  def initialize(values={})
23
320
  @raw = values
24
321
  @name = values['name']
@@ -30,6 +327,35 @@ module WorldBank
30
327
  @capital = values['capitalCity']
31
328
  @type = 'countries'
32
329
  end
33
- end
34
330
 
331
+ private
332
+
333
+ def self.ensure_id(id)
334
+ @id = id
335
+ if @id.length > 3
336
+ @matching = COUNTRIES.select do |country|
337
+ country[2] =~ Regexp.new(@id)
338
+ end
339
+ if @matching.length > 1
340
+ raise ArgumentError,
341
+ "More than one country code matched '#{@id}'. Perhaps you meant one of #{@matching.join(', ')}?",
342
+ caller
343
+ elsif @matching.length == 0
344
+ raise ArgumentError,
345
+ "No countries matched '#{@id}', please try again.",
346
+ caller
347
+ else
348
+ @id = @matching[0][0]
349
+ end
350
+ end
351
+ @id
352
+ end
353
+
354
+ def self.normalize_id(id)
355
+ id.gsub!(/[ -]/, '_')
356
+ id.downcase!
357
+ id
358
+ end
359
+ end
35
360
  end
361
+