wbapi 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/lib/wbapi.rb +79 -0
  4. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6d5365acd4ede3110188539a0c9664469b1afe46
4
+ data.tar.gz: 488bb27957b9a9d7fbe080cd61979711f6963d1d
5
+ SHA512:
6
+ metadata.gz: a86432bf25addbcdcc586e9b3313a1f631d3e05d3e34c548dababfad6d7d0600f418aea443d117d322e9461d134026df4ea0c261aa85818032cbaf8a541c70cd
7
+ data.tar.gz: 47ccb1ca910a4723656149eca922aea8a0e83e72bbbd6b7acb0900bc7f9fcc57f658cfdd9a9078924be97f78ee7fabf10eda4365ab4fed985ca10263019f3f7e
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 John Hager
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/lib/wbapi.rb ADDED
@@ -0,0 +1,79 @@
1
+ require 'open-uri'
2
+ require 'json'
3
+ require 'csv'
4
+
5
+ class WBApi
6
+
7
+ class << self
8
+ # takes hash of options, returns json of request
9
+ def request(indicator = nil, options = {})
10
+ #puts "options is: \n #{options}"
11
+ uri = build_uri(indicator, options.merge({format: 'json'}))
12
+ puts "uri is: #{uri}"
13
+ JSON.parse(open(uri).read)
14
+ end
15
+
16
+ def csv!(request)
17
+ results = request[1]
18
+ a_row = results.first
19
+ indicator = a_row['id'] ?
20
+ "indicators" : a_row["indicator"]["id"]
21
+ columns = a_row.each_with_object([]) do
22
+ |(key, value), columns|
23
+ if value.is_a?(Hash)
24
+ value.keys.each {|sub_key|
25
+ columns << "#{key} #{sub_key}"
26
+ }
27
+ else
28
+ columns << key
29
+ end
30
+ end
31
+ name = "#{Time.now.to_i}_#{indicator}.csv"
32
+
33
+ CSV.open(name, 'wb') do |csv|
34
+ csv << [indicator]
35
+ csv << columns
36
+
37
+ results.each do |res|
38
+ csv << columns.each_with_object([]) do |column, values|
39
+ column = column.split(' ')
40
+ values << column.inject(res) {|res, key|
41
+ res[key]
42
+ }
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ private
49
+ def build_query(options)
50
+ #puts "options is: \n #{options}"
51
+ options.each_with_object('?') {|(key, value), query|
52
+ query << "#{key}=#{value}&"
53
+ }[0..-2]
54
+ end
55
+
56
+ def build_uri_base(indicator, given_country)
57
+ if indicator
58
+ indicator = "indicators/#{indicator}"
59
+ country = "countries/"
60
+ country << (given_country ? given_country : "all") << "/"
61
+ elsif given_country
62
+ indicator, country = nil, given_country
63
+ else
64
+ indicator = "indicators"
65
+ country = nil
66
+ end
67
+ uri = "http://api.worldbank.org/#{country}#{indicator}"
68
+ end
69
+
70
+ def build_uri(indicator, options)
71
+ #puts "options is: \n #{options}"
72
+ uri = build_uri_base(indicator, options.delete(:country))
73
+ query = build_query(options)
74
+ #puts "query is: \n #{query}"
75
+ uri += query.empty? ? '' : query
76
+ end
77
+ end
78
+ end
79
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wbapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - jphager2
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Useful interface to World Bank API
14
+ email: jphager2@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - lib/wbapi.rb
21
+ homepage: https://github.com/jphager2/wbapi
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.2.2
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Useful interface to World Bank API
45
+ test_files: []