From 622eec0d80ee10934161d172178c2639e56f38c9 Mon Sep 17 00:00:00 2001 From: Tony Bowden Date: Mon, 6 Apr 2015 11:25:28 +0100 Subject: [PATCH 1/5] start moving code to Data class --- lib/popolo_helper.rb | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/popolo_helper.rb b/lib/popolo_helper.rb index 7225c40d1..d05a21e85 100644 --- a/lib/popolo_helper.rb +++ b/lib/popolo_helper.rb @@ -1,14 +1,24 @@ module Popolo - module Helper - 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 + + end + + module Helper + + require 'date' + + def popit_data(file='eduskunta') + @_data ||= {} + @_data[file] ||= Popolo::Data.new(file).json end def persons From d3b7c9a51508da256a6a2490c8dd7fc92e203256 Mon Sep 17 00:00:00 2001 From: Tony Bowden Date: Mon, 6 Apr 2015 11:32:17 +0100 Subject: [PATCH 2/5] use Data class directly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit baby steps… --- app.rb | 2 +- lib/popolo_helper.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app.rb b/app.rb index 18dcf5a9c..1acdfce49 100644 --- a/app.rb +++ b/app.rb @@ -19,7 +19,7 @@ end get '/people.html' do - @people = persons + @people = Popolo::Data.new('eduskunta').persons haml :people end diff --git a/lib/popolo_helper.rb b/lib/popolo_helper.rb index d05a21e85..0d0fa0eb0 100644 --- a/lib/popolo_helper.rb +++ b/lib/popolo_helper.rb @@ -10,6 +10,10 @@ def json @_data ||= JSON.parse(File.read("data/#{@_file}.json")) end + def persons + json['persons'] + end + end module Helper From f827fdbf3c4d76b353c6f6e5d3962ba68e145b83 Mon Sep 17 00:00:00 2001 From: Tony Bowden Date: Mon, 6 Apr 2015 11:44:55 +0100 Subject: [PATCH 3/5] Migrate /terms and /parties --- app.rb | 4 ++-- lib/popolo_helper.rb | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app.rb b/app.rb index 1acdfce49..099a5bc35 100644 --- a/app.rb +++ b/app.rb @@ -14,7 +14,7 @@ end get '/terms.html' do - @terms = terms + @terms = Popolo::Data.new('eduskunta').terms haml :terms end @@ -24,7 +24,7 @@ end get '/parties.html' do - @parties = parties + @parties = Popolo::Data.new('eduskunta').parties haml :parties end diff --git a/lib/popolo_helper.rb b/lib/popolo_helper.rb index 0d0fa0eb0..ac904d927 100644 --- a/lib/popolo_helper.rb +++ b/lib/popolo_helper.rb @@ -14,6 +14,27 @@ def persons json['persons'] end + def organizations + json['organizations'] + end + + def memberships + json['memberships'] + end + + def legislature + # TODO cope with more than one! + json['organizations'].find { |o| o['classification'] == 'legislature' } + end + + def parties + json['organizations'].find_all { |o| o['classification'] == 'party' } + end + + def terms + legislature['terms'] + end + end module Helper From a354f39526ea0293065544d91f9eeccaf6eaeff2 Mon Sep 17 00:00:00 2001 From: Tony Bowden Date: Mon, 6 Apr 2015 11:57:13 +0100 Subject: [PATCH 4/5] Migrate all other methods --- app.rb | 15 +++--- lib/popolo_helper.rb | 76 +++++++++++++------------- t/helpers/eduskunta.rb | 117 +++++++++++++++++++++-------------------- 3 files changed, 109 insertions(+), 99 deletions(-) diff --git a/app.rb b/app.rb index 099a5bc35..cc63fd887 100644 --- a/app.rb +++ b/app.rb @@ -29,20 +29,23 @@ 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 diff --git a/lib/popolo_helper.rb b/lib/popolo_helper.rb index ac904d927..89dbeabae 100644 --- a/lib/popolo_helper.rb +++ b/lib/popolo_helper.rb @@ -35,6 +35,45 @@ def terms legislature['terms'] end + + def term_from_id(id) + terms.detect { |t| t['id'] == id } || terms.detect { |t| t['id'].end_with? "/#{id}" } + end + + def term_memberships(t) + # for now, solely by overlapping dates + # TODO: direct memberships + legislative_memberships.find_all { |m| + ( m['start_date'] <= (t['end_date'] || Date.today.to_s) and t['start_date'] <= (m['end_date'] || Date.today.to_s) ) + }.map { |m| + m['person'] ||= person_from_id(m['person_id']) + m + } + end + + def person_from_id(id) + persons.detect { |r| r['id'] == id } || persons.detect { |r| r['id'].end_with? "/#{id}" } + 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 + } + 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 @@ -71,48 +110,11 @@ def memberships popit_data['memberships'] end - def party_from_id(id) - p = organizations.detect { |r| r['id'] == id } || organizations.detect { |r| r['id'].end_with? "/#{id}" } - 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 - } - 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 - } - end - - def term_memberships(t) - # for now, solely by overlapping dates - # TODO: direct memberships - legislative_memberships.find_all { |m| - ( m['start_date'] <= (t['end_date'] || Date.today.to_s) and t['start_date'] <= (m['end_date'] || Date.today.to_s) ) - }.map { |m| - m['person'] ||= person_from_id(m['person_id']) - m - } - end - - 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}" } - end end end diff --git a/t/helpers/eduskunta.rb b/t/helpers/eduskunta.rb index 27a5cae94..140dff955 100644 --- a/t/helpers/eduskunta.rb +++ b/t/helpers/eduskunta.rb @@ -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 - From 741f154229ef8a5376cdbfdc35ef31b33577c7f9 Mon Sep 17 00:00:00 2001 From: Tony Bowden Date: Mon, 6 Apr 2015 11:58:48 +0100 Subject: [PATCH 5/5] Remove all Helper methods --- lib/popolo_helper.rb | 46 +++++++------------------------------------- 1 file changed, 7 insertions(+), 39 deletions(-) diff --git a/lib/popolo_helper.rb b/lib/popolo_helper.rb index 89dbeabae..a76265d14 100644 --- a/lib/popolo_helper.rb +++ b/lib/popolo_helper.rb @@ -1,5 +1,7 @@ module Popolo + require 'date' + class Data def initialize(file) @@ -35,6 +37,11 @@ def terms legislature['terms'] end + def legislative_memberships + # TODO expand! + memberships.find_all { |m| m['organization_id'] == 'legislature' } + end + def term_from_id(id) terms.detect { |t| t['id'] == id } || terms.detect { |t| t['id'].end_with? "/#{id}" } @@ -78,45 +85,6 @@ def party_memberships(id) module Helper - require 'date' - - def popit_data(file='eduskunta') - @_data ||= {} - @_data[file] ||= Popolo::Data.new(file).json - end - - def persons - popit_data['persons'] - end - - def organizations - popit_data['organizations'] - end - - def parties - popit_data['organizations'].find_all { |o| o['classification'] == 'party' } - 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'] - end - - def legislative_memberships - # TODO expand! - memberships.find_all { |m| m['organization_id'] == 'legislature' } - end - end end - -