everypolitician 0.17.0 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53e74c64bd7576aa98501922b02abfdf58ce5996
4
- data.tar.gz: d57337142f105a4e5b01959ec6b93510200ad9f5
3
+ metadata.gz: 3a2f42e4251751b073c51cc076f48eb031e8982a
4
+ data.tar.gz: f6a9f626ec4561e709db1e42f29d7e5b711d464f
5
5
  SHA512:
6
- metadata.gz: 611b9052bc7dbee75d6514d6c665c3440ebbfbe774da31fe6fbdec5b45500c06ac1add3f3dba46bb85f7aa780e10a918d2ba0eb9555559cef287dd4931df1c26
7
- data.tar.gz: 42eacfc02b5268f974d3c40769335ec8386bded77a7917592739f291f0521ee270b0c0316380c5a7a22d272222571ee10f88c89b75382856e017ebb87cebbb18
6
+ metadata.gz: 32661d8fc404cd8cc07e00738cff3f76eb28ead39d0f30dfad21f1b3b481459a70b5c5dc5c3fd41bddc94899e8fb94067bd1da4ea689ab41fefe5cbc73b7b572
7
+ data.tar.gz: 1d362621148d17b0ac2a277ea307df2233f0ecbad57d72a27a85e91e28dc180cb4d78b35e018a54a9a60f97377e384e30d06d906d0ae813c926dbdae0001a9e2
@@ -3,8 +3,28 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project adheres to [Semantic Versioning](http://semver.org/).
5
5
 
6
+ ## [0.20.0] - 2016-10-28
7
+
8
+ ### Added
9
+
10
+ - Added `all_legislatures` to Index class. Returns an array of all the
11
+ Legislatures for all countries.
12
+ - Added `latest_term` to Legislature class. This returns an
13
+ Everpolitician::Popolo::LegislativePeriod object with data from the
14
+ latest term. Note that this may involve a **substantial download**
15
+ depending on the legislature.
16
+
17
+ ## [0.19.0] - 2016-10-19
18
+
6
19
  ## Unreleased
7
20
 
21
+ ## [0.18.0] - 2016-08-25
22
+
23
+ ### Changed
24
+
25
+ - The method to get the URL of the names file is now `Legislature.names_url`,
26
+ not `Legislature.csv_url`
27
+
8
28
  ## [0.17.0] - 2016-08-23
9
29
 
10
30
  ### Changed
@@ -156,3 +176,6 @@ a string with the time in seconds.
156
176
  [0.15.0]: https://github.com/everypolitician/everypolitician-ruby/compare/v0.14.0...v0.15.0
157
177
  [0.16.0]: https://https://github.com/everypolitician/everypolitician-ruby/compare/v0.15.0...v0.16.0
158
178
  [0.17.0]: https://https://github.com/everypolitician/everypolitician-ruby/compare/v0.16.0...v0.17.0
179
+ [0.18.0]: https://https://github.com/everypolitician/everypolitician-ruby/compare/v0.17.0...v0.18.0
180
+ [0.19.0]: https://https://github.com/everypolitician/everypolitician-ruby/compare/v0.18.0...v0.19.0
181
+ [0.20.0]: https://https://github.com/everypolitician/everypolitician-ruby/compare/v0.19.0...v0.20.0
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Everypolitician [![Build Status](https://travis-ci.org/everypolitician/everypolitician-ruby.svg?branch=v0.1.0)](https://travis-ci.org/everypolitician/everypolitician-ruby) [![Gem Version](https://badge.fury.io/rb/everypolitician.svg)](https://badge.fury.io/rb/everypolitician)
2
2
 
3
- Interface with EveryPolitician data from your Ruby application.
3
+ Interface with [EveryPolitician](http://everypolitician.org) data from your Ruby application.
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,7 +20,9 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- Use as shown below, passing the `:slug` of the country to the `country` method, and the `:slug` of the legislature to the `legislature` method. Examples:
23
+ Pass the `:slug` of the country to the `country` method, and the `:slug` of the legislature to the `legislature` method.
24
+
25
+ Examples:
24
26
 
25
27
  ```ruby
26
28
  require 'everypolitician'
@@ -43,18 +45,46 @@ algeria = Everypolitician::Index.new.country('Algeria')
43
45
  national_assembly = algeria.legislature('Majlis')
44
46
 
45
47
  # Iterate though all known countries
46
- Everypolitician::Index.new.countries do |country|
48
+ Everypolitician::Index.new.countries.each do |country|
47
49
  puts "#{country.name} has #{country.legislatures.size} legislature(s)"
48
50
  end
51
+
52
+ # Iterate through all known legislatures
53
+ Everypolitician::Index.new.all_legislatures.each do |legislature|
54
+ puts "#{legislature.name} in #{legislature.country.name} has #{legislature.person_count} member(s)"
55
+ end
56
+
57
+ # Fetch Everypolitician::Popolo::LegislativePeriod for the latest term
58
+ # Note that this may involve a **substantial download**
59
+ puts 'Members of the Assembly:'
60
+ assembly = Everypolitician::Index.new.country('Albania').lower_house
61
+ assembly.latest_term.memberships.map(&:person).uniq(&:id).each do |person|
62
+ puts person.name
63
+ end
49
64
  ```
50
65
 
51
- If you want to point at a different version of `countries.json` you can supply an
52
- `index_url` option to `Everypolitician::Index.new`.
66
+ By default, the gem connects to EveryPolitician's data on GitHub over HTTPS and
67
+ returns the most recent data. Specifically it uses the current index file,
68
+ called `countries.json`, which itself contains links to specific versions of
69
+ data files.
70
+
71
+ If you want to point at a different `countries.json`, you can override this default behavour by supplying an
72
+ `index_url` option to `Everypolitician::Index.new` like this:
53
73
 
54
74
  ```ruby
55
75
  Everypolitician::Index.new(index_url: 'https://cdn.rawgit.com/everypolitician/everypolitician-data/080cb46/countries.json')
56
76
  ```
57
77
 
78
+ The example above is using a specific commit (indicated by the hash `080cb46`).
79
+ If you want to use a local copy of `countries.json` you can specify a local file path as the `index_url` instead of a fully-qualified URL.
80
+
81
+ For more about `countries.json`, see [this description](http://docs.everypolitician.org/repo_structure.html).
82
+
83
+ Remember that EveryPolitician data is frequently updated — see this information
84
+ about [using EveryPolitician data](http://docs.everypolitician.org/use_the_data.html).
85
+
86
+ More information on [the EveryPolitician site](http://docs.everypolitician.org/).
87
+
58
88
  ## Development
59
89
 
60
90
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/Rakefile CHANGED
@@ -3,6 +3,7 @@ require 'rake/testtask'
3
3
  require 'rubocop/rake_task'
4
4
 
5
5
  Rake::TestTask.new(:test) do |t|
6
+ t.warning = true
6
7
  t.libs << 'test'
7
8
  t.libs << 'lib'
8
9
  t.test_files = FileList['test/**/*_test.rb']
@@ -18,11 +18,13 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_dependency 'everypolitician-popolo'
21
+ spec.add_dependency 'everypolitician-popolo', '>= 0.8.0'
22
+ spec.add_dependency 'require_all'
22
23
 
23
24
  spec.add_development_dependency 'bundler', '~> 1.10'
24
25
  spec.add_development_dependency 'rake', '~> 10.0'
25
26
  spec.add_development_dependency 'minitest', '~> 5.9.0'
27
+ spec.add_development_dependency 'minitest-around', '~> 0.4.0'
26
28
  spec.add_development_dependency 'pry', '~> 0.10.4'
27
29
  spec.add_development_dependency 'rubocop', '~> 0.42.0'
28
30
  spec.add_development_dependency 'vcr', '~> 3.0.3'
@@ -1,8 +1,9 @@
1
1
  require 'everypolitician/version'
2
2
  require 'json'
3
3
  require 'open-uri'
4
- require 'everypolitician/popolo'
5
- require 'csv'
4
+ require 'require_all'
5
+
6
+ require_rel 'everypolitician'
6
7
 
7
8
  module Everypolitician
8
9
  class << self
@@ -36,178 +37,4 @@ module Everypolitician
36
37
  legislature = country.legislature(legislature_slug)
37
38
  [country, legislature]
38
39
  end
39
-
40
- class Index
41
- DEFAULT_INDEX_URL = 'https://raw.githubusercontent.com/' \
42
- 'everypolitician/everypolitician-data/master/countries.json'.freeze
43
-
44
- attr_reader :index_url
45
-
46
- def initialize(index_url: DEFAULT_INDEX_URL)
47
- @index_url = index_url
48
- end
49
-
50
- def country(slug)
51
- country_index[slug.to_s.downcase]
52
- end
53
-
54
- def countries
55
- @countries ||= begin
56
- JSON.parse(open(index_url).read, symbolize_names: true).map do |c|
57
- Country.new(c)
58
- end
59
- end
60
- end
61
-
62
- private
63
-
64
- def country_index
65
- @country_index ||= Hash[countries.map { |c| [c.slug.downcase, c] }]
66
- end
67
- end
68
-
69
- class Country
70
- attr_reader :name
71
- attr_reader :code
72
- attr_reader :slug
73
- attr_reader :raw_data
74
-
75
- def self.find(query)
76
- query = { slug: query } if query.is_a?(String)
77
- country = Everypolitician.countries.find do |c|
78
- query.all? { |k, v| c[k].to_s.downcase == v.to_s.downcase }
79
- end
80
- return if country.nil?
81
- new(country)
82
- end
83
-
84
- def initialize(country_data)
85
- @name = country_data[:name]
86
- @code = country_data[:code]
87
- @slug = country_data[:slug]
88
- @raw_data = country_data
89
- end
90
-
91
- def [](key)
92
- raw_data[key]
93
- end
94
-
95
- def legislatures
96
- @legislatures ||= @raw_data[:legislatures].map { |l| Legislature.new(l, self) }
97
- end
98
-
99
- def legislature(query)
100
- query = { slug: query } if query.is_a?(String)
101
- legislatures.find do |l|
102
- query.all? { |k, v| l.__send__(k).to_s.downcase == v.to_s.downcase }
103
- end
104
- end
105
- end
106
-
107
- class Legislature
108
- attr_reader :name
109
- attr_reader :slug
110
- attr_reader :lastmod_str
111
- attr_reader :person_count
112
- attr_reader :sha
113
- attr_reader :country
114
- attr_reader :raw_data
115
- attr_reader :statement_count
116
- attr_reader :popolo_url
117
-
118
- def self.find(country_slug, legislature_slug)
119
- country = Country.find(country_slug)
120
- country && country.legislature(legislature_slug)
121
- end
122
-
123
- def initialize(legislature_data, country)
124
- @name = legislature_data[:name]
125
- @slug = legislature_data[:slug]
126
- @lastmod_str = legislature_data[:lastmod]
127
- @person_count = legislature_data[:person_count]
128
- @sha = legislature_data[:sha]
129
- @statement_count = legislature_data[:statement_count]
130
- @popolo_url = legislature_data[:popolo_url]
131
- @raw_data = legislature_data
132
- @country = country
133
- end
134
-
135
- def [](key)
136
- raw_data[key]
137
- end
138
-
139
- def popolo
140
- @popolo ||= Everypolitician::Popolo.parse(open(popolo_url).read)
141
- end
142
-
143
- def legislative_periods
144
- @legislative_periods ||= raw_data[:legislative_periods].map do |lp|
145
- LegislativePeriod.new(lp, self, country)
146
- end
147
- end
148
-
149
- def directory
150
- @directory = raw_data[:sources_directory].split('/')[1, 2].join('/')
151
- end
152
-
153
- def self.from_sources_dir(dir)
154
- @index_by_sources ||= EveryPolitician.countries.map(&:legislatures).flatten.group_by(&:directory)
155
- @index_by_sources[dir][0]
156
- end
157
-
158
- def lastmod
159
- Time.at(lastmod_str.to_i).gmtime
160
- end
161
-
162
- def csv_url
163
- @csv_url ||= 'https://cdn.rawgit.com/everypolitician' \
164
- "/everypolitician-data/#{@sha}/#{raw_data[:names]}"
165
- end
166
- end
167
-
168
- class LegislativePeriod
169
- attr_reader :id
170
- attr_reader :name
171
- attr_reader :slug
172
- attr_reader :legislature
173
- attr_reader :country
174
- attr_reader :raw_data
175
- attr_reader :csv_url
176
-
177
- def initialize(legislative_period_data, legislature, country)
178
- @id = legislative_period_data[:id]
179
- @name = legislative_period_data[:name]
180
- @slug = legislative_period_data[:slug]
181
- @csv_url = legislative_period_data[:csv_url]
182
- @legislature = legislature
183
- @country = country
184
- @raw_data = legislative_period_data
185
- end
186
-
187
- def start_date
188
- @start_date ||= parse_partial_date(raw_data[:start_date])
189
- end
190
-
191
- def end_date
192
- @end_date ||= parse_partial_date(raw_data[:end_date])
193
- end
194
-
195
- def csv
196
- CSV.parse(open(csv_url).read, headers: true, header_converters: :symbol, converters: nil)
197
- end
198
-
199
- def [](key)
200
- raw_data[key]
201
- end
202
-
203
- private
204
-
205
- def parse_partial_date(date)
206
- return if date.to_s.empty?
207
- Date.new(*date.split('-').map(&:to_i))
208
- end
209
- end
210
40
  end
211
-
212
- # Alternative constant name which is how it's usually capitalized in public copy.
213
- EveryPolitician ||= Everypolitician
@@ -0,0 +1,55 @@
1
+ module Everypolitician
2
+ class Country
3
+ attr_reader :name
4
+ attr_reader :code
5
+ attr_reader :slug
6
+ attr_reader :raw_data
7
+
8
+ def self.find(query)
9
+ query = { slug: query } if query.is_a?(String)
10
+ country = Everypolitician.countries.find do |c|
11
+ query.all? { |k, v| c[k].to_s.downcase == v.to_s.downcase }
12
+ end
13
+ return if country.nil?
14
+ new(country)
15
+ end
16
+
17
+ def initialize(country_data)
18
+ @name = country_data[:name]
19
+ @code = country_data[:code]
20
+ @slug = country_data[:slug]
21
+ @raw_data = country_data
22
+ end
23
+
24
+ def [](key)
25
+ raw_data[key]
26
+ end
27
+
28
+ def legislatures
29
+ @legislatures ||= @raw_data[:legislatures].map { |l| Legislature.new(l, self) }
30
+ end
31
+
32
+ def legislature(query)
33
+ query = { slug: query } if query.is_a?(String)
34
+ legislatures.find do |l|
35
+ query.all? { |k, v| l.__send__(k).to_s.downcase == v.to_s.downcase }
36
+ end
37
+ end
38
+
39
+ def upper_house
40
+ @upper_house ||= most_recent('upper house')
41
+ end
42
+
43
+ def lower_house
44
+ @lower_house ||= most_recent('lower house')
45
+ end
46
+
47
+ private
48
+
49
+ def most_recent(type)
50
+ houses = legislatures.select { |l| l.type == type || l.type == 'unicameral legislature' }
51
+ return houses.first if houses.count == 1
52
+ houses.max_by { |h| h.legislative_periods.map(&:start_date).max }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,37 @@
1
+ require 'json'
2
+ require 'open-uri'
3
+
4
+ module Everypolitician
5
+ class Index
6
+ DEFAULT_INDEX_URL = 'https://raw.githubusercontent.com/' \
7
+ 'everypolitician/everypolitician-data/master/countries.json'.freeze
8
+
9
+ attr_reader :index_url
10
+
11
+ def initialize(index_url: DEFAULT_INDEX_URL)
12
+ @index_url = index_url
13
+ end
14
+
15
+ def country(slug)
16
+ country_index[slug.to_s.downcase]
17
+ end
18
+
19
+ def countries
20
+ @countries ||= begin
21
+ JSON.parse(open(index_url).read, symbolize_names: true).map do |c|
22
+ Country.new(c)
23
+ end
24
+ end
25
+ end
26
+
27
+ def all_legislatures
28
+ @all_legislatures ||= countries.flat_map(&:legislatures)
29
+ end
30
+
31
+ private
32
+
33
+ def country_index
34
+ @country_index ||= Hash[countries.map { |c| [c.slug.downcase, c] }]
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,47 @@
1
+ require 'open-uri'
2
+ require 'csv'
3
+
4
+ module Everypolitician
5
+ class LegislativePeriod
6
+ attr_reader :id
7
+ attr_reader :name
8
+ attr_reader :slug
9
+ attr_reader :legislature
10
+ attr_reader :country
11
+ attr_reader :raw_data
12
+ attr_reader :csv_url
13
+
14
+ def initialize(legislative_period_data, legislature, country)
15
+ @id = legislative_period_data[:id]
16
+ @name = legislative_period_data[:name]
17
+ @slug = legislative_period_data[:slug]
18
+ @csv_url = legislative_period_data[:csv_url]
19
+ @legislature = legislature
20
+ @country = country
21
+ @raw_data = legislative_period_data
22
+ end
23
+
24
+ def start_date
25
+ @start_date ||= parse_partial_date(raw_data[:start_date])
26
+ end
27
+
28
+ def end_date
29
+ @end_date ||= parse_partial_date(raw_data[:end_date])
30
+ end
31
+
32
+ def csv
33
+ CSV.parse(open(csv_url).read, headers: true, header_converters: :symbol, converters: nil)
34
+ end
35
+
36
+ def [](key)
37
+ raw_data[key]
38
+ end
39
+
40
+ private
41
+
42
+ def parse_partial_date(date)
43
+ return if date.to_s.empty?
44
+ Date.new(*date.split('-').map(&:to_i))
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,71 @@
1
+ require 'open-uri'
2
+ require 'everypolitician/popolo'
3
+
4
+ module Everypolitician
5
+ class Legislature
6
+ attr_reader :name
7
+ attr_reader :slug
8
+ attr_reader :lastmod_str
9
+ attr_reader :person_count
10
+ attr_reader :sha
11
+ attr_reader :country
12
+ attr_reader :raw_data
13
+ attr_reader :statement_count
14
+ attr_reader :popolo_url
15
+ attr_reader :type
16
+
17
+ def self.find(country_slug, legislature_slug)
18
+ country = Country.find(country_slug)
19
+ country && country.legislature(legislature_slug)
20
+ end
21
+
22
+ def initialize(legislature_data, country)
23
+ @name = legislature_data[:name]
24
+ @slug = legislature_data[:slug]
25
+ @lastmod_str = legislature_data[:lastmod]
26
+ @person_count = legislature_data[:person_count]
27
+ @sha = legislature_data[:sha]
28
+ @statement_count = legislature_data[:statement_count]
29
+ @popolo_url = legislature_data[:popolo_url]
30
+ @raw_data = legislature_data
31
+ @country = country
32
+ @type = legislature_data[:type]
33
+ end
34
+
35
+ def [](key)
36
+ raw_data[key]
37
+ end
38
+
39
+ def popolo
40
+ @popolo ||= Everypolitician::Popolo.parse(open(popolo_url).read)
41
+ end
42
+
43
+ def legislative_periods
44
+ @legislative_periods ||= raw_data[:legislative_periods].map do |lp|
45
+ LegislativePeriod.new(lp, self, country)
46
+ end
47
+ end
48
+
49
+ def directory
50
+ @directory = raw_data[:sources_directory].split('/')[1, 2].join('/')
51
+ end
52
+
53
+ def latest_term
54
+ @latest_term ||= popolo.latest_term
55
+ end
56
+
57
+ def self.from_sources_dir(dir)
58
+ @index_by_sources ||= EveryPolitician.countries.map(&:legislatures).flatten.group_by(&:directory)
59
+ @index_by_sources[dir][0]
60
+ end
61
+
62
+ def lastmod
63
+ Time.at(lastmod_str.to_i).gmtime
64
+ end
65
+
66
+ def names_url
67
+ 'https://cdn.rawgit.com/everypolitician/everypolitician-data/%s/%s' %
68
+ [sha, raw_data[:names]]
69
+ end
70
+ end
71
+ end
@@ -1,3 +1,3 @@
1
1
  module Everypolitician
2
- VERSION = '0.17.0'.freeze
2
+ VERSION = '0.20.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,17 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: everypolitician
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.0
4
+ version: 0.20.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Mytton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-24 00:00:00.000000000 Z
11
+ date: 2016-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: everypolitician-popolo
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: require_all
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - ">="
@@ -66,6 +80,20 @@ dependencies:
66
80
  - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: 5.9.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-around
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.4.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.4.0
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: pry
71
99
  requirement: !ruby/object:Gem::Requirement
@@ -142,6 +170,10 @@ files:
142
170
  - bin/setup
143
171
  - everypolitician.gemspec
144
172
  - lib/everypolitician.rb
173
+ - lib/everypolitician/country.rb
174
+ - lib/everypolitician/index.rb
175
+ - lib/everypolitician/legislative_period.rb
176
+ - lib/everypolitician/legislature.rb
145
177
  - lib/everypolitician/version.rb
146
178
  homepage: https://github.com/everypolitician/everypolitician-ruby
147
179
  licenses: