everypolitician 0.4.0 → 0.5.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: 5fb2e4b9951d0fcb07375a0eeb7e62654d69218a
4
- data.tar.gz: 97535464a6f92d4ddaa1d94144aa48a44005a6bc
3
+ metadata.gz: 2f5019b4f32557bebbe011a84ef324b25faedfa2
4
+ data.tar.gz: 721813c2e6d40c2714c742520ccb7e569174303c
5
5
  SHA512:
6
- metadata.gz: 3d812c5eedb13bdfe7f0d2cc68be4d1da742f724aa6c694b1ac82eb70b366cf50eef7f13bc2f850a6609bc66c49697157d0f50018d284600a30e28238506505e
7
- data.tar.gz: b2cb5cb815eb29f841ed391cd2ce889aa9d259c47867bb748f1b8490c1564a36b8986ea1cc5b940642397c9c2cb3486e33c5fb1d81b33622885de429d91e8006
6
+ metadata.gz: 7f648d3407c0ffd02073518f016a2c5f5964a75d98fbcf18aff88dc39344ef6f929fc7c4be4dbd4c58d43511b0e1e78d674b2f830bba2df22b5f8c397526b2a7
7
+ data.tar.gz: ee7abeb616c7537c2b4111953051900ce4138369bd403b0f350eb5f1b844190f65f2bd0a3218c8f887cbceb375aff3dd801e7214ae3d65784dad8ed5b457ca8c
data/CHANGELOG.md CHANGED
@@ -3,6 +3,19 @@
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.5.0] - 2016-01-27
7
+
8
+ ### Added
9
+
10
+ - `Everypolitician.countries` method for returning an array of all countries.
11
+ - Find countries and legislatures by arbitrary attributes, e.g. `Everypolitician.country(code: 'AU')`
12
+
13
+ ### Changed
14
+
15
+ - We no longer use ruby metaprogramming to define the methods on `Country` and `Legislature`, they are now explicitly defined as properties and methods.
16
+ - `Legislature#popolo_url` now uses the `Legislature#sha` method rather than `master` in the raw url.
17
+ - `countries.json` is only loaded once, when it's first accessed. If you want to force a refetch then you can call `Everypolitician.countries = nil`.
18
+
6
19
  ## [0.4.0] - 2016-01-26
7
20
 
8
21
  ### Changed
@@ -33,3 +46,4 @@ This project adheres to [Semantic Versioning](http://semver.org/).
33
46
  [0.2.0]: https://github.com/everypolitician/everypolitician-ruby/compare/v0.1.0...v0.2.0
34
47
  [0.3.0]: https://github.com/everypolitician/everypolitician-ruby/compare/v0.2.0...v0.3.0
35
48
  [0.4.0]: https://github.com/everypolitician/everypolitician-ruby/compare/v0.3.0...v0.4.0
49
+ [0.5.0]: https://github.com/everypolitician/everypolitician-ruby/compare/v0.4.0...v0.5.0
@@ -1,3 +1,3 @@
1
1
  module Everypolitician
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -8,6 +8,7 @@ module Everypolitician
8
8
 
9
9
  class << self
10
10
  attr_writer :countries_json
11
+ attr_writer :countries
11
12
  end
12
13
 
13
14
  def self.countries_json
@@ -15,6 +16,14 @@ module Everypolitician
15
16
  'everypolitician/everypolitician-data/master/countries.json'
16
17
  end
17
18
 
19
+ def self.countries
20
+ @countries ||= begin
21
+ JSON.parse(open(countries_json).read, symbolize_names: true).map do |c|
22
+ Country.new(c)
23
+ end
24
+ end
25
+ end
26
+
18
27
  def self.country(slug)
19
28
  Country.find(slug)
20
29
  end
@@ -29,69 +38,77 @@ module Everypolitician
29
38
  [country, legislature]
30
39
  end
31
40
 
32
- class Entity
33
- def initialize(data)
34
- process_data(data).each do |key, value|
35
- define_singleton_method(key) do
36
- value
37
- end
38
- end
41
+ class Country
42
+ attr_reader :name
43
+ attr_reader :code
44
+ attr_reader :slug
45
+ attr_reader :raw_data
46
+
47
+ def self.find(query)
48
+ query = { slug: query } if query.is_a?(String)
49
+ country = Everypolitician.countries.find { |c| query.all? { |k, v| c[k] == v } }
50
+ fail Error, "Couldn't find country for query: #{query}" if country.nil?
51
+ new(country)
39
52
  end
40
53
 
41
- # Override in subclasses to change structure of data
42
- def process_data(data)
43
- data
54
+ def initialize(country_data)
55
+ @name = country_data[:name]
56
+ @code = country_data[:code]
57
+ @slug = country_data[:slug]
58
+ @raw_data = country_data
44
59
  end
45
- end
46
60
 
47
- class Country < Entity
48
- def self.find(slug)
49
- country = CountriesJson.new.find { |c| c[:slug] == slug }
50
- fail Error, "Unknown country slug: #{slug}" if country.nil?
51
- new(country)
61
+ def [](key)
62
+ raw_data[key]
63
+ end
64
+
65
+ def legislatures
66
+ @legislatures ||= @raw_data[:legislatures].map { |l| Legislature.new(l) }
52
67
  end
53
68
 
54
- def legislature(slug)
55
- legislature = legislatures.find { |l| l[:slug] == slug }
69
+ def legislature(query)
70
+ query = { slug: query } if query.is_a?(String)
71
+ legislature = legislatures.find { |l| query.all? { |k, v| l.__send__(k) == v } }
56
72
  fail Error, "Unknown legislature slug: #{slug}" if legislature.nil?
57
- Legislature.new(legislature)
73
+ legislature
58
74
  end
59
75
  end
60
76
 
61
- class Legislature < Entity
77
+ class Legislature
78
+ attr_reader :name
79
+ attr_reader :slug
80
+ attr_reader :lastmod
81
+ attr_reader :person_count
82
+ attr_reader :sha
83
+ attr_reader :raw_data
84
+
62
85
  def self.find(country_slug, legislature_slug)
63
86
  Country.find(country_slug).legislature(legislature_slug)
64
87
  end
65
88
 
66
- def popolo
67
- @popolo ||= Everypolitician::Popolo.parse(open(popolo_url).read)
68
- end
69
-
70
- def process_data(data)
71
- data[:popolo_url] = 'https://raw.githubusercontent.com/everypolitician' \
72
- "/everypolitician-data/master/#{data.delete(:popolo)}"
73
- data
89
+ def initialize(legislature_data)
90
+ @name = legislature_data[:name]
91
+ @slug = legislature_data[:slug]
92
+ @lastmod = legislature_data[:lastmod]
93
+ @person_count = legislature_data[:person_count]
94
+ @sha = legislature_data[:sha]
95
+ @raw_data = legislature_data
74
96
  end
75
- end
76
-
77
- class CountriesJson
78
- include Enumerable
79
97
 
80
- def countries
81
- @countries ||= JSON.parse(raw_json_string, symbolize_names: true)
98
+ def popolo
99
+ @popolo ||= Everypolitician::Popolo.parse(open(popolo_url).read)
82
100
  end
83
101
 
84
- def each(&block)
85
- countries.each(&block)
102
+ def popolo_url
103
+ @popolo_url ||= 'https://raw.githubusercontent.com/everypolitician' \
104
+ "/everypolitician-data/#{sha}/#{raw_data[:popolo]}"
86
105
  end
87
106
 
88
- private
89
-
90
- def raw_json_string
91
- @json ||= open(Everypolitician.countries_json).read
107
+ def legislative_periods
108
+ @legislative_periods ||= raw_data[:legislative_periods]
92
109
  end
93
110
  end
94
111
  end
95
112
 
96
113
  # Alternative constant name which is how it's usually capitalized in public copy.
97
- EveryPolitician = Everypolitician
114
+ EveryPolitician ||= Everypolitician
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: everypolitician
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.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-01-26 00:00:00.000000000 Z
11
+ date: 2016-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: everypolitician-popolo