Skip to content

Commit

Permalink
Merge pull request #2 from tmtmtmtm/explicit-datafile
Browse files Browse the repository at this point in the history
Explicit Datafile
  • Loading branch information
tmtmtmtm committed Apr 6, 2015
2 parents 16ebee4 + 741f154 commit fb00615
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 101 deletions.
21 changes: 12 additions & 9 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,38 @@
end

get '/terms.html' do
@terms = terms
@terms = Popolo::Data.new('eduskunta').terms
haml :terms
end

get '/people.html' do
@people = persons
@people = Popolo::Data.new('eduskunta').persons
haml :people
end

get '/parties.html' do
@parties = parties
@parties = Popolo::Data.new('eduskunta').parties
haml :parties
end

get '/term/:id' do |id|
@term = term_from_id(id) or pass
@memberships = term_memberships(@term)
pd = Popolo::Data.new('eduskunta')
@term = pd.term_from_id(id) or pass
@memberships = pd.term_memberships(@term)
haml :term
end

get '/person/:id' do |id|
@person = person_from_id(id) or pass
@memberships = person_memberships(@person)
pd = Popolo::Data.new('eduskunta')
@person = pd.person_from_id(id) or pass
@memberships = pd.person_memberships(@person)
haml :person
end

get '/party/:id' do |id|
@party = party_from_id(id) or pass
@memberships = party_memberships(@party['id'])
pd = Popolo::Data.new('eduskunta')
@party = pd.party_from_id(id) or pass
@memberships = pd.party_memberships(@party['id'])
haml :party
end

Expand Down
77 changes: 41 additions & 36 deletions lib/popolo_helper.rb
Original file line number Diff line number Diff line change
@@ -1,63 +1,50 @@
module Popolo
module Helper

require 'date'
require 'date'

class Data

def popit_data
@_data ||= json_file('eduskunta')
def initialize(file)
@_file = file
end

def json_file(file)
JSON.parse(File.read("data/#{file}.json"))
def json
@_data ||= JSON.parse(File.read("data/#{@_file}.json"))
end

def persons
popit_data['persons']
json['persons']
end

def organizations
popit_data['organizations']
json['organizations']
end

def parties
popit_data['organizations'].find_all { |o| o['classification'] == 'party' }
def memberships
json['memberships']
end

def legislature
# TODO cope with more than one!
popit_data['organizations'].find { |o| o['classification'] == 'legislature' }
end

def terms
legislature['terms']
end

def memberships
popit_data['memberships']
json['organizations'].find { |o| o['classification'] == 'legislature' }
end

def party_from_id(id)
p = organizations.detect { |r| r['id'] == id } || organizations.detect { |r| r['id'].end_with? "/#{id}" }
def parties
json['organizations'].find_all { |o| o['classification'] == 'party' }
end

def person_memberships(p)
memberships.find_all { |m| m['person_id'] == p['id'] }.map { |m|
m['organization'] ||= party_from_id(m['organization_id'])
m['on_behalf_of'] ||= party_from_id(m['on_behalf_of_id'])
m
}
def terms
legislature['terms']
end

def legislative_memberships
# TODO expand!
memberships.find_all { |m| m['organization_id'] == 'legislature' }
end

def party_memberships(id)
legislative_memberships.find_all { |m| m['on_behalf_of_id'] == id }.map { |m|
m['person'] ||= person_from_id(m['person_id'])
m
}

def term_from_id(id)
terms.detect { |t| t['id'] == id } || terms.detect { |t| t['id'].end_with? "/#{id}" }
end

def term_memberships(t)
Expand All @@ -75,11 +62,29 @@ def person_from_id(id)
persons.detect { |r| r['id'] == id } || persons.detect { |r| r['id'].end_with? "/#{id}" }
end

def term_from_id(id)
terms.detect { |t| t['id'] == id } || terms.detect { |t| t['id'].end_with? "/#{id}" }
def person_memberships(p)
memberships.find_all { |m| m['person_id'] == p['id'] }.map { |m|
m['organization'] ||= party_from_id(m['organization_id'])
m['on_behalf_of'] ||= party_from_id(m['on_behalf_of_id'])
m
}
end

def party_from_id(id)
p = organizations.detect { |r| r['id'] == id } || organizations.detect { |r| r['id'].end_with? "/#{id}" }
end

def party_memberships(id)
legislative_memberships.find_all { |m| m['on_behalf_of_id'] == id }.map { |m|
m['person'] ||= person_from_id(m['person_id'])
m
}
end

end

module Helper

end
end



117 changes: 61 additions & 56 deletions t/helpers/eduskunta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,90 +4,95 @@

include Popolo::Helper

describe "party" do
describe "Eduskunta" do

subject { party_from_id('kok') }
subject { Popolo::Data.new('eduskunta') }

describe "party" do

let(:party) { subject.party_from_id('kok') }

it "should get the correct party" do
party['name'].must_equal 'National Coalition Party'
end

it "should get the correct party" do
subject['name'].must_equal 'National Coalition Party'
end

end
describe "person" do

describe "person" do
let(:person) { subject.person_from_id('1031') }
let(:mems) { subject.person_memberships(person) }

subject { person_from_id('1031') }
let(:mems) { person_memberships(subject) }
it "should get the correct person" do
person['name'].must_equal 'Stubb Alexander'
end

it "should get the correct person" do
subject['name'].must_equal 'Stubb Alexander'
end
it "should have a membership" do
mems.count.must_equal 1
end

it "should have a membership" do
mems.count.must_equal 1
end
it "should be in the legislature" do
mems.first['organization_id'].must_equal 'legislature'
end

it "should be in the legislature" do
mems.first['organization_id'].must_equal 'legislature'
end
it "should have an expanded Organization" do
mems.first['organization']['name'].must_equal 'Eduskunta'
end

it "should have an expanded Organization" do
mems.first['organization']['name'].must_equal 'Eduskunta'
end
it "should have an expanded Party" do
mems.first['on_behalf_of']['name'].must_equal 'National Coalition Party'
end

it "should have an expanded Party" do
mems.first['on_behalf_of']['name'].must_equal 'National Coalition Party'
end

end
describe "term" do

describe "term" do
let(:term) { subject.term_from_id('34') }
let(:mems) { subject.term_memberships(term) }

subject { term_from_id('34') }
let(:mems) { term_memberships(subject) }
it "should get the correct term" do
term['name'].must_equal 'Eduskunta 34 (2003)'
end

it "should get the correct term" do
subject['name'].must_equal 'Eduskunta 34 (2003)'
end
it "should have 214 memberships" do
mems.count.must_equal 214
end

it "should have 214 memberships" do
mems.count.must_equal 214
end
it "should have someone who joined mid-term" do
mems.map { |m| m['person']['name'] }.must_include 'Donner Jörn'
end

it "should have someone who joined mid-term" do
mems.map { |m| m['person']['name'] }.must_include 'Donner Jörn'
end
it "should have someone who left mid-term" do
mems.map { |m| m['person']['name'] }.must_include 'Laisaari Sinikka'
end

it "should have someone who left mid-term" do
mems.map { |m| m['person']['name'] }.must_include 'Laisaari Sinikka'
end

end
describe "old term" do

describe "old term" do
let(:term) { subject.term_from_id('27') }
let(:mems) { subject.term_memberships(term) }

subject { term_from_id('27') }
let(:mems) { term_memberships(subject) }
it "should get the correct term" do
term['name'].must_equal 'Eduskunta 27 (1975 II)'
end

it "should get the correct term" do
subject['name'].must_equal 'Eduskunta 27 (1975 II)'
end
it "should have 8 memberships" do
mems.count.must_equal 8
end

it "should have 8 memberships" do
mems.count.must_equal 8
end
it "should have someone who joined before" do
mems.map { |m| m['person']['name'] }.must_include 'Tuomioja Erkki'
end

it "should have someone who joined before" do
mems.map { |m| m['person']['name'] }.must_include 'Tuomioja Erkki'
end
it "should have someone who joined at that term" do
mems.map { |m| m['person']['name'] }.must_include 'Stenius-Kaukonen Marjatta'
end

it "should have someone who joined at that term" do
mems.map { |m| m['person']['name'] }.must_include 'Stenius-Kaukonen Marjatta'
end
it "should have someone still serving" do
mems.map { |m| m['person']['name'] }.must_include 'Kanerva Ilkka'
end

it "should have someone still serving" do
mems.map { |m| m['person']['name'] }.must_include 'Kanerva Ilkka'
end

end

0 comments on commit fb00615

Please sign in to comment.