everypolitician-popolo 0.2.0 → 0.3.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: 819ab96e2d0f30e3bf74ba4778a571516a159a36
4
- data.tar.gz: db95c96de4e2645f3610aefa37121649599f509f
3
+ metadata.gz: 75d39a544d53fcdcc9692ac0825e03234a7f1ac9
4
+ data.tar.gz: 4dc22302c3543d088cbed60b375b0cc196026881
5
5
  SHA512:
6
- metadata.gz: 111f78af9e392de63fbc5c4cda75ed9752653a2a8472b0980e60956f3cd72f850d8f63769d9e0f6bab719644f9bc3454d3a1d2c0a3e581294038e12b3d9c85ce
7
- data.tar.gz: 64c9211c16d8f2f19b49266b34820db29c80ef634e2ebf320efa96947014e82eecf3ea5e2ecde1cdc4da0c7b65e710c3b164b54d3df6d5d120f52345fbd9ddeb
6
+ metadata.gz: 69ad80079a75686b97663ea202642ecc5a0bf52814170e641bee38eb68c3c4fac1744330ebcbad55add964b78b2741dc234fa587beac874cb35cd8d8d1c64a56
7
+ data.tar.gz: caad3f3004bb899da9d97674c7064f383eb0e5384afc5edf4a63fd7ce583f7093a7d05a53d08ef12e7791848e920dd31c5cc0495a97925ce6ab07eda98d46a36
data/CHANGELOG.md CHANGED
@@ -3,9 +3,21 @@
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.3.0] - 2016-05-04
7
+
8
+ ### Fixed
9
+
10
+ - Return an empty array when no data is available for a collection (Thanks @henare)
11
+
12
+ ### Added
13
+
14
+ - Support for Posts, which EveryPolitician has recently added support for.
15
+ - `Collection#find_by` and `Collection#where` methods. See the README for more details on how to use these.
16
+ - Explicit methods so that calling a known property on a document returns `nil` rather than blowing up if it's missing.
17
+
6
18
  ## [0.2.0] - 2016-03-11
7
19
 
8
- ## Added
20
+ ### Added
9
21
 
10
22
  - Support for all Popolo collections that EveryPolitician uses - People, Organizations, Areas, Memberships and Events. Thanks to @equivalentideas and @henare for this contribution!
11
23
 
@@ -14,3 +26,4 @@ This project adheres to [Semantic Versioning](http://semver.org/).
14
26
  - Initial release
15
27
 
16
28
  [0.2.0]: https://github.com/everypolitician/everypolitician-popolo/compare/v0.1.0...v0.2.0
29
+ [0.3.0]: https://github.com/everypolitician/everypolitician-popolo/compare/v0.2.0...v0.3.0
data/README.md CHANGED
@@ -50,6 +50,28 @@ person.image # => "http://www.lagtinget.ax/files/aaltonen_carina.jpg"
50
50
  person.wikidata # => "Q4934081"
51
51
  ```
52
52
 
53
+ You can also find individual records or collections based on their attributes:
54
+
55
+ ```ruby
56
+ popolo.persons.find_by(name: "Aaltonen Carina", wikidata: "Q4934081")
57
+ # => #<Everypolitician::Popolo::Person:0x0000000237dfc8
58
+ # @document={:id=>"0c705344-23aa-4fa2-9391-af41c1c775b7",
59
+ # :identifiers=>[{:identifier=>"Q4934081", :scheme=>"wikidata"}],
60
+ # :name=>"Aaltonen Carina"}>
61
+
62
+ popolo.organizations.where(classification: "party")
63
+ # => [
64
+ # <Everypolitician::Popolo::Organization:0x000000035779e0
65
+ # @document={:classification=>"party",
66
+ # :id=>"123",
67
+ # :name=>"Sunripe Tomato Party"}>,
68
+ # <Everypolitician::Popolo::Organization:0x000000035779e1
69
+ # @document={:classification=>"party",
70
+ # :id=>"456",
71
+ # :name=>"The Greens"}>
72
+ # ]
73
+ ```
74
+
53
75
  ## Development
54
76
 
55
77
  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.
@@ -1,9 +1,11 @@
1
1
  require 'everypolitician/popolo/version'
2
2
  require 'everypolitician/popolo/collection'
3
+ require 'everypolitician/popolo/entity'
3
4
  require 'everypolitician/popolo/person'
4
5
  require 'everypolitician/popolo/organization'
5
6
  require 'everypolitician/popolo/area'
6
7
  require 'everypolitician/popolo/event'
8
+ require 'everypolitician/popolo/post'
7
9
  require 'everypolitician/popolo/membership'
8
10
  require 'json'
9
11
 
@@ -43,6 +45,10 @@ module Everypolitician
43
45
  Events.new(popolo[:events])
44
46
  end
45
47
 
48
+ def posts
49
+ Posts.new(popolo[:posts])
50
+ end
51
+
46
52
  def memberships
47
53
  Memberships.new(popolo[:memberships])
48
54
  end
@@ -1,18 +1,6 @@
1
1
  module Everypolitician
2
2
  module Popolo
3
- class Areas < Collection
4
- def initialize(documents)
5
- @documents = documents.map { |p| Area.new(p) }
6
- end
7
- end
8
-
9
- class Area
10
- def initialize(document)
11
- @document = document
12
- document.each do |key, value|
13
- define_singleton_method(key) { value }
14
- end
15
- end
16
- end
3
+ class Areas < Collection; end
4
+ class Area < Entity; end
17
5
  end
18
6
  end
@@ -5,6 +5,10 @@ module Everypolitician
5
5
 
6
6
  attr_reader :documents
7
7
 
8
+ def initialize(documents)
9
+ @documents = documents ? documents.map { |p| klass.new(p) } : []
10
+ end
11
+
8
12
  def each(&block)
9
13
  documents.each(&block)
10
14
  end
@@ -13,6 +17,39 @@ module Everypolitician
13
17
  other_ids = Set.new(other.documents.map(&:id))
14
18
  documents.reject { |d| other_ids.include?(d.id) }
15
19
  end
20
+
21
+ def find_by(attributes = {})
22
+ where(attributes).first
23
+ end
24
+
25
+ def where(attributes = {})
26
+ find_all do |object|
27
+ attributes.all? { |k, v| object.send(k) == v }
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ # TODO: This feels pretty nasty, is there a better way of working out the
34
+ # class name?
35
+ def klass
36
+ case self.class.to_s.split("::").last
37
+ when "People"
38
+ Person
39
+ when "Organizations"
40
+ Organization
41
+ when "Memberships"
42
+ Membership
43
+ when "Events"
44
+ Event
45
+ when "Posts"
46
+ Post
47
+ when "Areas"
48
+ Area
49
+ else
50
+ raise "Unknown class: #{self.class.to_s}"
51
+ end
52
+ end
16
53
  end
17
54
  end
18
55
  end
@@ -0,0 +1,35 @@
1
+ module Everypolitician
2
+ module Popolo
3
+ class Entity
4
+
5
+ attr_accessor :id
6
+ attr_reader :document
7
+
8
+ def initialize(document)
9
+ @document = document
10
+
11
+ document.each do |key, value|
12
+ if respond_to?("#{key}=")
13
+ __send__("#{key}=", value)
14
+ else
15
+ define_singleton_method(key) { value }
16
+ end
17
+ end
18
+ end
19
+
20
+ def [](key)
21
+ document[key]
22
+ end
23
+
24
+ def key?(key)
25
+ document.key?(key)
26
+ end
27
+
28
+ def ==(other)
29
+ id == other.id
30
+ end
31
+ alias eql? ==
32
+
33
+ end
34
+ end
35
+ end
@@ -1,18 +1,6 @@
1
1
  module Everypolitician
2
2
  module Popolo
3
- class Events < Collection
4
- def initialize(documents)
5
- @documents = documents.map { |p| Event.new(p) }
6
- end
7
- end
8
-
9
- class Event
10
- def initialize(document)
11
- @document = document
12
- document.each do |key, value|
13
- define_singleton_method(key) { value }
14
- end
15
- end
16
- end
3
+ class Events < Collection; end
4
+ class Event < Entity; end
17
5
  end
18
6
  end
@@ -1,26 +1,9 @@
1
1
  module Everypolitician
2
2
  module Popolo
3
- class Memberships < Collection
4
- def initialize(documents)
5
- @documents = documents.map { |p| Membership.new(p) }
6
- end
7
- end
8
-
9
- class Membership
10
- def initialize(document)
11
- @document = document
12
- document.each do |key, value|
13
- define_singleton_method(key) { value }
14
- end
15
- end
16
-
17
- def start_date
18
- @document[:start_date]
19
- end
3
+ class Memberships < Collection; end
20
4
 
21
- def end_date
22
- @document[:end_date]
23
- end
5
+ class Membership < Entity
6
+ attr_accessor :person_id, :on_behalf_of_id, :organization_id, :area_id, :role, :start_date, :end_date
24
7
  end
25
8
  end
26
9
  end
@@ -1,25 +1,6 @@
1
1
  module Everypolitician
2
2
  module Popolo
3
- class Organizations < Collection
4
- def initialize(documents)
5
- @documents = documents.map { |p| Organization.new(p) }
6
- end
7
- end
8
-
9
- class Organization
10
- attr_reader :document
11
-
12
- def initialize(document)
13
- @document = document
14
- document.each do |key, value|
15
- define_singleton_method(key) { value }
16
- end
17
- end
18
-
19
- def ==(other)
20
- id == other.id
21
- end
22
- alias eql? ==
23
- end
3
+ class Organizations < Collection; end
4
+ class Organization < Entity; end
24
5
  end
25
6
  end
@@ -1,50 +1,39 @@
1
1
  module Everypolitician
2
2
  module Popolo
3
- class People < Collection
4
- def initialize(documents)
5
- @documents = documents.map { |p| Person.new(p) }
6
- end
7
- end
3
+ class People < Collection; end
8
4
 
9
- class Person
5
+ class Person < Entity
10
6
  class Error < StandardError; end
11
7
 
12
- attr_reader :document
8
+ attr_accessor :name, :email, :image, :gender, :birth_date, :death_date
13
9
 
14
- def initialize(document)
15
- @document = document
16
- document.each do |key, value|
17
- define_singleton_method(key) { value }
18
- end
10
+ def links
11
+ document.fetch(:links, [])
19
12
  end
20
13
 
21
- def [](key)
22
- document[key]
14
+ def identifiers
15
+ document.fetch(:identifiers, [])
23
16
  end
24
17
 
25
- def key?(key)
26
- document.key?(key)
18
+ def contact_details
19
+ document.fetch(:contact_details, [])
27
20
  end
28
21
 
29
- def ==(other)
30
- id == other.id
31
- end
32
- alias eql? ==
33
22
 
34
- def links
35
- document.fetch(:links, [])
23
+ def identifier(scheme)
24
+ identifiers.find(->{{}}) { |i| i[:scheme] == scheme }[:identifier]
36
25
  end
37
26
 
38
- def identifiers
39
- document.fetch(:identifiers, [])
27
+ def contact(type)
28
+ contact_details.find(->{{}}) { |i| i[:type] == type }[:value]
40
29
  end
41
30
 
42
- def identifier(scheme)
43
- identifiers.find(->{{}}) { |i| i[:scheme] == scheme }[:identifier]
31
+ def phone
32
+ contact('phone')
44
33
  end
45
34
 
46
- def email
47
- self[:email]
35
+ def fax
36
+ contact('fax')
48
37
  end
49
38
 
50
39
  def twitter
@@ -69,6 +58,10 @@ module Everypolitician
69
58
  identifier('wikidata')
70
59
  end
71
60
 
61
+ def sort_name
62
+ name
63
+ end
64
+
72
65
  def name_at(date)
73
66
  return name unless key?(:other_names)
74
67
  historic = other_names.find_all { |n| n.key?(:end_date) }
@@ -80,18 +73,6 @@ module Everypolitician
80
73
  fail Error, "Too many names at #{date}: #{at_date}" if at_date.count > 1
81
74
  at_date.first[:name]
82
75
  end
83
-
84
- def sort_name
85
- name
86
- end
87
-
88
- def image
89
- self[:image]
90
- end
91
-
92
- def gender
93
- self[:gender]
94
- end
95
76
  end
96
77
  end
97
78
  end
@@ -0,0 +1,6 @@
1
+ module Everypolitician
2
+ module Popolo
3
+ class Posts < Collection; end
4
+ class Post < Entity; end
5
+ end
6
+ end
@@ -1,5 +1,5 @@
1
1
  module Everypolitician
2
2
  module Popolo
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: everypolitician-popolo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.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-03-11 00:00:00.000000000 Z
11
+ date: 2016-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,10 +86,12 @@ files:
86
86
  - lib/everypolitician/popolo.rb
87
87
  - lib/everypolitician/popolo/area.rb
88
88
  - lib/everypolitician/popolo/collection.rb
89
+ - lib/everypolitician/popolo/entity.rb
89
90
  - lib/everypolitician/popolo/event.rb
90
91
  - lib/everypolitician/popolo/membership.rb
91
92
  - lib/everypolitician/popolo/organization.rb
92
93
  - lib/everypolitician/popolo/person.rb
94
+ - lib/everypolitician/popolo/post.rb
93
95
  - lib/everypolitician/popolo/version.rb
94
96
  homepage: https://github.com/everypolitician/everypolitician-popolo
95
97
  licenses: