diff --git a/lib/parliament/decorators/house_incumbency.rb b/lib/parliament/decorators/house_incumbency.rb index 5f9a2f9..b74c82e 100644 --- a/lib/parliament/decorators/house_incumbency.rb +++ b/lib/parliament/decorators/house_incumbency.rb @@ -28,4 +28,4 @@ def contact_points end end end -end \ No newline at end of file +end diff --git a/lib/parliament/decorators/incumbency.rb b/lib/parliament/decorators/incumbency.rb index ca61b2e..257eabd 100644 --- a/lib/parliament/decorators/incumbency.rb +++ b/lib/parliament/decorators/incumbency.rb @@ -24,4 +24,4 @@ def contact_points end end end -end \ No newline at end of file +end diff --git a/lib/parliament/decorators/party.rb b/lib/parliament/decorators/party.rb index 8d6b52e..ae64d90 100644 --- a/lib/parliament/decorators/party.rb +++ b/lib/parliament/decorators/party.rb @@ -8,6 +8,10 @@ def name def party_memberships respond_to?(:partyHasPartyMembership) ? partyHasPartyMembership : [] end + + def member_count + respond_to?(:count) ? count : nil + end end end end diff --git a/lib/parliament/decorators/person.rb b/lib/parliament/decorators/person.rb index 7011979..cdb6757 100644 --- a/lib/parliament/decorators/person.rb +++ b/lib/parliament/decorators/person.rb @@ -36,7 +36,7 @@ def seats return @seats unless @seats.nil? seats = [] - incumbencies.each do |incumbency| + seat_incumbencies.each do |incumbency| seats << incumbency.seat if incumbency.respond_to?(:seat) end @@ -91,7 +91,6 @@ def gender_identities def gender gender_identities.empty? ? nil : gender_identities.first.gender end - end end end diff --git a/lib/parliament/response.rb b/lib/parliament/response.rb index 906feea..1b3c889 100644 --- a/lib/parliament/response.rb +++ b/lib/parliament/response.rb @@ -40,7 +40,9 @@ def sort_by(*parameters) grom_nodes = @nodes.dup grom_nodes.delete_if { |node| rejected << node unless parameters.all? { |param| node.respond_to?(param) } } grom_nodes.sort_by! do |node| - parameters.map { |param| node.send(param) } + parameters.map do |param| + node.send(param).is_a?(String) ? I18n.transliterate(node.send(param)).downcase : node.send(param) + end end rejected.concat(grom_nodes) diff --git a/spec/fixtures/vcr_cassettes/Parliament_Decorators_Party/_member_count/Grom_Node_has_a_member_count/returns_the_member_count_for_a_Grom_Node_object_of_type_Party.yml b/spec/fixtures/vcr_cassettes/Parliament_Decorators_Party/_member_count/Grom_Node_has_a_member_count/returns_the_member_count_for_a_Grom_Node_object_of_type_Party.yml new file mode 100644 index 0000000..01295e7 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Decorators_Party/_member_count/Grom_Node_has_a_member_count/returns_the_member_count_for_a_Grom_Node_object_of_type_Party.yml @@ -0,0 +1,87 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/houses/4b77dd58-f6ba-4121-b521-c8ad70465f52/parties/current + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + Host: + - localhost:3030 + response: + status: + code: 200 + message: OK + headers: + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + Content-Type: + - application/n-triples; charset=utf-8 + Etag: + - W/"2dc4c4cd100cb54a3c420c0e45fc2e30" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 708ed2d5-c08f-4374-bc20-97a96930533d + X-Runtime: + - '0.267783' + Transfer-Encoding: + - chunked + body: + encoding: UTF-8 + string: | + . + "House of Commons" . + . + "Ulster Unionist Party" . + "2"^^ . + . + "Green Party" . + "1"^^ . + . + "Conservative" . + "330"^^ . + . + "Social Democratic & Labour Party" . + "3"^^ . + . + "Scottish National Party" . + "54"^^ . + . + "Liberal Democrat" . + "9"^^ . + . + "Independent" . + "4"^^ . + . + "Sinn Fein" . + "4"^^ . + . + "Labour" . + "229"^^ . + . + "Plaid Cymru" . + "3"^^ . + . + "Speaker" . + "1"^^ . + . + "Democratic Unionist Party" . + "8"^^ . + . + "UK Independence Party" . + "1"^^ . + http_version: + recorded_at: Mon, 06 Mar 2017 13:13:55 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Decorators_Party/_member_count/Grom_Node_has_no_count/returns_nil.yml b/spec/fixtures/vcr_cassettes/Parliament_Decorators_Party/_member_count/Grom_Node_has_no_count/returns_nil.yml new file mode 100644 index 0000000..50e65dd --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Decorators_Party/_member_count/Grom_Node_has_no_count/returns_nil.yml @@ -0,0 +1,86 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/houses/4b77dd58-f6ba-4121-b521-c8ad70465f52/parties/current + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + Host: + - localhost:3030 + response: + status: + code: 200 + message: OK + headers: + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + Content-Type: + - application/n-triples; charset=utf-8 + Etag: + - W/"2dc4c4cd100cb54a3c420c0e45fc2e30" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 0d84b0ff-2169-42b5-a0a1-469dc7ce22fb + X-Runtime: + - '0.302627' + Transfer-Encoding: + - chunked + body: + encoding: UTF-8 + string: | + . + "House of Commons" . + . + "Ulster Unionist Party" . + "2"^^ . + . + "Green Party" . + . + "Conservative" . + "330"^^ . + . + "Social Democratic & Labour Party" . + "3"^^ . + . + "Scottish National Party" . + "54"^^ . + . + "Liberal Democrat" . + "9"^^ . + . + "Independent" . + "4"^^ . + . + "Sinn Fein" . + "4"^^ . + . + "Labour" . + "229"^^ . + . + "Plaid Cymru" . + "3"^^ . + . + "Speaker" . + "1"^^ . + . + "Democratic Unionist Party" . + "8"^^ . + . + "UK Independence Party" . + "1"^^ . + http_version: + recorded_at: Mon, 06 Mar 2017 13:13:56 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Decorators_Person/_seat_incumbencies/Grom_Node_has_all_the_required_objects/returns_the_seat_incumbencies_for_a_Grom_Node_object_of_type_Person.yml b/spec/fixtures/vcr_cassettes/Parliament_Decorators_Person/_seat_incumbencies/Grom_Node_has_all_the_required_objects/returns_the_seat_incumbencies_for_a_Grom_Node_object_of_type_Person.yml index 7ed81bb..60c3440 100644 --- a/spec/fixtures/vcr_cassettes/Parliament_Decorators_Person/_seat_incumbencies/Grom_Node_has_all_the_required_objects/returns_the_seat_incumbencies_for_a_Grom_Node_object_of_type_Person.yml +++ b/spec/fixtures/vcr_cassettes/Parliament_Decorators_Person/_seat_incumbencies/Grom_Node_has_all_the_required_objects/returns_the_seat_incumbencies_for_a_Grom_Node_object_of_type_Person.yml @@ -47,7 +47,7 @@ http_interactions: "Person 1 - familyName" . . . - . + . . . . @@ -73,7 +73,7 @@ http_interactions: . "2015-10-27"^^ . . - . + . . "Dulwich and West Norwood" . . @@ -83,17 +83,17 @@ http_interactions: . . . - . + . . "2005-05-05"^^ . "2001-06-07"^^ . . - . + . . "2010-05-06"^^ . "2005-05-05"^^ . . - . + . . "Dulwich and West Norwood" . . diff --git a/spec/fixtures/vcr_cassettes/Parliament_Response/_sort_by/all_nodes_have_the_parameter_being_sorted_on/returns_a_response_sorted_by_seatIncumbencyStartDate.yml b/spec/fixtures/vcr_cassettes/Parliament_Response/_sort_by/all_nodes_have_the_parameter_being_sorted_on/returns_a_response_sorted_by_seatIncumbencyStartDate.yml index ceeef9f..e8eff57 100644 --- a/spec/fixtures/vcr_cassettes/Parliament_Response/_sort_by/all_nodes_have_the_parameter_being_sorted_on/returns_a_response_sorted_by_seatIncumbencyStartDate.yml +++ b/spec/fixtures/vcr_cassettes/Parliament_Response/_sort_by/all_nodes_have_the_parameter_being_sorted_on/returns_a_response_sorted_by_seatIncumbencyStartDate.yml @@ -57,7 +57,7 @@ http_interactions: "Hackney North and Stoke Newington" . . "1992-04-09"^^ . - "1987-06-11"^^ . + "1987-06-11"^^ . . . . @@ -77,14 +77,14 @@ http_interactions: . . "1997-05-01"^^ . - "1992-04-09"^^ . + "1992-04-09"^^ . . . . "Hackney North and Stoke Newington" . . "2001-06-07"^^ . - "1997-05-01"^^ . + "1997-05-01"^^ . . . . @@ -92,26 +92,26 @@ http_interactions: . . "2005-05-05"^^ . - "2001-06-07"^^ . + "2001-06-07"^^ . . . . "2010-05-06"^^ . - "2005-05-05"^^ . + "2005-05-05"^^ . . . . "Hackney North and Stoke Newington" . . "2015-03-30"^^ . - "2010-05-06"^^ . + "2010-05-06"^^ . . . . . . . - "2015-05-07"^^ . + "2015-05-07"^^ . . http_version: recorded_at: Thu, 23 Feb 2017 14:56:39 GMT diff --git a/spec/fixtures/vcr_cassettes/Parliament_Response/_sort_by/sorting_strings_of_different_cases/returns_a_response_sorted_by_personFamilyName.yml b/spec/fixtures/vcr_cassettes/Parliament_Response/_sort_by/sorting_strings_of_different_cases/returns_a_response_sorted_by_personFamilyName.yml new file mode 100644 index 0000000..dd5da1a --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Response/_sort_by/sorting_strings_of_different_cases/returns_a_response_sorted_by_personFamilyName.yml @@ -0,0 +1,56 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/people + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + Host: + - localhost:3030 + response: + status: + code: 200 + message: OK + headers: + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + Content-Type: + - application/n-triples; charset=utf-8 + Etag: + - W/"707521c37caf375e2ce268639be647f8" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 71c3c50f-d20e-4a97-a275-4202ef3e9ac3 + X-Runtime: + - '0.722607' + Transfer-Encoding: + - chunked + body: + encoding: UTF-8 + string: | + . + "Alice" . + "Dennis" . + . + "Jane" . + "de Bois" . + . + "Sarah" . + "du Vonn" . + + http_version: + recorded_at: Thu, 23 Feb 2017 14:38:30 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Response/_sort_by/sorting_strings_with_accents/returns_a_response_sorted_by_personFamilyName_personGivenName.yml b/spec/fixtures/vcr_cassettes/Parliament_Response/_sort_by/sorting_strings_with_accents/returns_a_response_sorted_by_personFamilyName_personGivenName.yml new file mode 100644 index 0000000..3bdee61 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Response/_sort_by/sorting_strings_with_accents/returns_a_response_sorted_by_personFamilyName_personGivenName.yml @@ -0,0 +1,56 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/people + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + Host: + - localhost:3030 + response: + status: + code: 200 + message: OK + headers: + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + Content-Type: + - application/n-triples; charset=utf-8 + Etag: + - W/"707521c37caf375e2ce268639be647f8" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 71c3c50f-d20e-4a97-a275-4202ef3e9ac3 + X-Runtime: + - '0.722607' + Transfer-Encoding: + - chunked + body: + encoding: UTF-8 + string: | + . + "Sophie" . + "Öpik" . + . + "Solomon" . + "Oliver" . + . + "Sarah" . + "Otter" . + + http_version: + recorded_at: Thu, 23 Feb 2017 14:38:30 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Response/_sort_by/sorting_strings_with_accents/returns_a_response_sorted_by_personGivenName.yml b/spec/fixtures/vcr_cassettes/Parliament_Response/_sort_by/sorting_strings_with_accents/returns_a_response_sorted_by_personGivenName.yml new file mode 100644 index 0000000..7862326 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Response/_sort_by/sorting_strings_with_accents/returns_a_response_sorted_by_personGivenName.yml @@ -0,0 +1,56 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/people + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + Host: + - localhost:3030 + response: + status: + code: 200 + message: OK + headers: + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + Content-Type: + - application/n-triples; charset=utf-8 + Etag: + - W/"707521c37caf375e2ce268639be647f8" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 71c3c50f-d20e-4a97-a275-4202ef3e9ac3 + X-Runtime: + - '0.722607' + Transfer-Encoding: + - chunked + body: + encoding: UTF-8 + string: | + . + "Sóley" . + "B" . + . + "Solomon" . + "A" . + . + "Sarah" . + "L" . + + http_version: + recorded_at: Thu, 23 Feb 2017 14:38:30 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/parliament/decorators/party_spec.rb b/spec/parliament/decorators/party_spec.rb index 3c956c4..f5d968f 100644 --- a/spec/parliament/decorators/party_spec.rb +++ b/spec/parliament/decorators/party_spec.rb @@ -40,6 +40,7 @@ response = Parliament::Request.new(base_url: 'http://localhost:3030').people(id).parties.get @party_nodes = response.filter('http://id.ukpds.org/schema/Party') end + context 'Grom::Node has all the required objects' do it 'returns the party memberships for a Grom::Node object of type Party' do party_node = @party_nodes.first @@ -57,4 +58,28 @@ end end end + + describe '#member_count' do + before(:each) do + id = '4b77dd58-f6ba-4121-b521-c8ad70465f52' + response = Parliament::Request.new(base_url: 'http://localhost:3030').houses(id).parties.current.get + @party_nodes = response.filter('http://id.ukpds.org/schema/Party') + end + + context 'Grom::Node has a member count' do + it 'returns the member count for a Grom::Node object of type Party' do + party_node = @party_nodes.first + + expect(party_node.member_count).to eq('2') + end + end + + context 'Grom::Node has no count' do + it 'returns nil' do + party_node = @party_nodes[1] + + expect(party_node.member_count).to be(nil) + end + end + end end diff --git a/spec/parliament/decorators/person_spec.rb b/spec/parliament/decorators/person_spec.rb index f2dbac3..eadbb3e 100644 --- a/spec/parliament/decorators/person_spec.rb +++ b/spec/parliament/decorators/person_spec.rb @@ -51,6 +51,29 @@ end end + describe '#seat_incumbencies' do + before(:each) do + @people_nodes = response.filter('http://id.ukpds.org/schema/Person') + end + + context 'Grom::Node has all the required objects' do + it 'returns the seat incumbencies for a Grom::Node object of type Person' do + person_node = @people_nodes.first + + expect(person_node.seat_incumbencies.size).to eq(5) + expect(person_node.seat_incumbencies.first.type).to eq('http://id.ukpds.org/schema/SeatIncumbency') + end + end + + context 'Grom::Node has no seat incumbencies' do + it 'returns an empty array' do + person_node = @people_nodes[1] + + expect(person_node.seat_incumbencies).to eq([]) + end + end + end + describe '#seats' do before(:each) do @people_nodes = response.filter('http://id.ukpds.org/schema/Person') diff --git a/spec/parliament/response_spec.rb b/spec/parliament/response_spec.rb index d7d427d..0224046 100644 --- a/spec/parliament/response_spec.rb +++ b/spec/parliament/response_spec.rb @@ -119,9 +119,10 @@ it 'returns a response sorted by seatIncumbencyStartDate' do response = Parliament::Request.new(base_url: 'http://localhost:3030').people('2c196540-13f3-4c07-8714-b356912beceb').get filtered_response = response.filter('http://id.ukpds.org/schema/SeatIncumbency') - sorted_response = filtered_response.sort_by(:seatIncumbencyStartDate) + sorted_response = filtered_response.sort_by(:start_date) - expect(sorted_response.first.seatIncumbencyStartDate).to eq('1987-06-11') + expect(sorted_response.first.start_date).to eq(DateTime.new(1987, 6, 11)) + expect(sorted_response[1].start_date).to eq(DateTime.new(1992, 4, 9)) end end @@ -144,5 +145,35 @@ expect(sorted_response[1].personGivenName).to eq('Sarah') end end + + context 'sorting strings of different cases' do + it 'returns a response sorted by personFamilyName' do + response = Parliament::Request.new(base_url: 'http://localhost:3030').people.get + sorted_response = response.sort_by(:personFamilyName) + + expect(sorted_response.first.personGivenName).to eq('Jane') + expect(sorted_response[1].personGivenName).to eq('Alice') + end + end + + context 'sorting strings with accents' do + it 'returns a response sorted by personGivenName' do + response = Parliament::Request.new(base_url: 'http://localhost:3030').people.get + sorted_response = response.sort_by(:personGivenName) + + expect(sorted_response.first.personGivenName).to eq('Sarah') + expect(sorted_response[1].personGivenName).to eq('Sóley') + expect(sorted_response[2].personGivenName).to eq('Solomon') + end + + it 'returns a response sorted by personFamilyName, personGivenName' do + response = Parliament::Request.new(base_url: 'http://localhost:3030').people.get + sorted_response = response.sort_by(:personFamilyName, :personGivenName) + + expect(sorted_response.first.personGivenName).to eq('Solomon') + expect(sorted_response[1].personGivenName).to eq('Sophie') + expect(sorted_response[2].personGivenName).to eq('Sarah') + end + end end end