diff --git a/Gemfile b/Gemfile index 9df355f..625d7cf 100644 --- a/Gemfile +++ b/Gemfile @@ -5,5 +5,3 @@ gemspec # Include coveralls for CI coverage reports gem 'coveralls', require: false - -gem 'pry' diff --git a/lib/parliament/response.rb b/lib/parliament/response.rb index 0c5ea18..f0fcfef 100644 --- a/lib/parliament/response.rb +++ b/lib/parliament/response.rb @@ -21,7 +21,7 @@ def initialize(nodes) # Given our array of Grom::Nodes, filter them into arrays of 'types' of nodes. # - # Note: this method assumes all of your nodes include a #type attribute. + # Note: this method assumes all of your nodes include a #type attribute or are blank nodes. # # @since 0.2.0 # @@ -58,6 +58,17 @@ def initialize(nodes) # type_2 #=> [#] # type_1 #=> [#, #] # + # @example Filtering blank nodes + # node_1 = Grom::Node.new + # node_1.instance_variable_set(:type, 'type_1') + # node_2 = Grom::Node.new + # node_3 = Grom::Node.new + # node_3.instance_variable_set(:type, 'type_1') + # nodes = [node_1, node_2, node_3] + # + # response = Parliament::Response.new(nodes) + # response.filter(Grom::Node::BLANK) #=> [#] + # # @param [Array] types An array of type strings that you are looking for. # @return [Array || Array<*Array>] If you pass one type, this returns an Array of Grom::Node objects. If you pass multiple, it returns an array, of arrays of Grom::Node objects. def filter(*types) @@ -65,7 +76,8 @@ def filter(*types) unless types.empty? @nodes.each do |node| - type_index = types.index(node.type) + type_index = node.blank? ? types.index(Grom::Node::BLANK) : types.index(node.type) + filtered_objects[type_index] << node unless type_index.nil? end end diff --git a/lib/parliament/version.rb b/lib/parliament/version.rb index de58326..6dbe159 100644 --- a/lib/parliament/version.rb +++ b/lib/parliament/version.rb @@ -1,3 +1,3 @@ module Parliament - VERSION = '0.6.1'.freeze + VERSION = '0.6.2'.freeze end diff --git a/parliament-ruby.gemspec b/parliament-ruby.gemspec index dbfc75f..81b3030 100644 --- a/parliament-ruby.gemspec +++ b/parliament-ruby.gemspec @@ -21,7 +21,7 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] - spec.add_dependency 'grom', '~> 0.3.5' + spec.add_dependency 'grom', '~> 0.3.6' spec.add_development_dependency 'bundler', '~> 1.13' spec.add_development_dependency 'rake', '~> 10.0' diff --git a/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/filters_a_mixture_of_typed_nodes_and_blank_nodes.yml b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/filters_a_mixture_of_typed_nodes_and_blank_nodes.yml new file mode 100644 index 0000000..8905533 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/filters_a_mixture_of_typed_nodes_and_blank_nodes.yml @@ -0,0 +1,124 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/people/members/current + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + - application/n-triples + User-Agent: + - Ruby + 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/"9dc79b6426182289fc121cc661ff8ef7" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - df20a843-38fe-44be-a36e-9959b9c4cf07 + X-Runtime: + - '1.968393' + Transfer-Encoding: + - chunked + body: + encoding: UTF-8 + string: | + . + "Person 1 - forename" . + "Person 1 - surname" . + . + . + . + "Constituency 1 - name" . + . + . + "Party 1 - name" . + . + . + "2016-05-03"^^ . + . + . + . + . + "2016-05-03"^^ . + . + . + . + "Person 2 - forename" . + "Person 2 - surname" . + . + . + "Constituency 2 - name" . + . + . + . + "2016-05-03"^^ . + . + . + . + . + "2016-05-03"^^ . + . + . + . + "Person 3 - forename" . + "Person 3 - surname" . + . + . + "Constituency 3 - name" . + . + . + . + "2016-05-03"^^ . + . + . + . + . + "2016-05-03"^^ . + . + . + _:node132919 "C" . + _:node132920 "A" . + _:node132921 "M" . + _:node132922 "G" . + _:node132923 "R" . + _:node132924 "L" . + _:node132925 "H" . + _:node132926 "J" . + _:node132927 "B" . + _:node132928 "D" . + _:node132929 "T" . + _:node132930 "S" . + _:node132931 "E" . + _:node132932 "K" . + _:node132933 "F" . + _:node132934 "U" . + _:node132935 "P" . + _:node132936 "W" . + _:node132937 "V" . + _:node132938 "Q" . + _:node132939 "O" . + _:node132940 "N" . + _:node132941 "I" . + _:node132942 "Y" . + _:node132943 "Z" . + http_version: + recorded_at: Mon, 27 Mar 2017 09:00:18 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/filters_blank_nodes.yml b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/filters_blank_nodes.yml new file mode 100644 index 0000000..aa1a003 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/filters_blank_nodes.yml @@ -0,0 +1,124 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/people/members/current + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + - application/n-triples + User-Agent: + - Ruby + 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/"9dc79b6426182289fc121cc661ff8ef7" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - df20a843-38fe-44be-a36e-9959b9c4cf07 + X-Runtime: + - '1.968393' + Transfer-Encoding: + - chunked + body: + encoding: UTF-8 + string: | + . + "Person 1 - forename" . + "Person 1 - surname" . + . + . + . + "Constituency 1 - name" . + . + . + "Party 1 - name" . + . + . + "2016-05-03"^^ . + . + . + . + . + "2016-05-03"^^ . + . + . + . + "Person 2 - forename" . + "Person 2 - surname" . + . + . + "Constituency 2 - name" . + . + . + . + "2016-05-03"^^ . + . + . + . + . + "2016-05-03"^^ . + . + . + . + "Person 3 - forename" . + "Person 3 - surname" . + . + . + "Constituency 3 - name" . + . + . + . + "2016-05-03"^^ . + . + . + . + . + "2016-05-03"^^ . + . + . + _:node132919 "C" . + _:node132920 "A" . + _:node132921 "M" . + _:node132922 "G" . + _:node132923 "R" . + _:node132924 "L" . + _:node132925 "H" . + _:node132926 "J" . + _:node132927 "B" . + _:node132928 "D" . + _:node132929 "T" . + _:node132930 "S" . + _:node132931 "E" . + _:node132932 "K" . + _:node132933 "F" . + _:node132934 "U" . + _:node132935 "P" . + _:node132936 "W" . + _:node132937 "V" . + _:node132938 "Q" . + _:node132939 "O" . + _:node132940 "N" . + _:node132941 "I" . + _:node132942 "Y" . + _:node132943 "Z" . + http_version: + recorded_at: Mon, 27 Mar 2017 09:00:18 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/filters_typed_nodes_from_a_mixture_of_typed_nodes_and_blank_nodes.yml b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/filters_typed_nodes_from_a_mixture_of_typed_nodes_and_blank_nodes.yml new file mode 100644 index 0000000..8905533 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/filters_typed_nodes_from_a_mixture_of_typed_nodes_and_blank_nodes.yml @@ -0,0 +1,124 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/people/members/current + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + - application/n-triples + User-Agent: + - Ruby + 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/"9dc79b6426182289fc121cc661ff8ef7" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - df20a843-38fe-44be-a36e-9959b9c4cf07 + X-Runtime: + - '1.968393' + Transfer-Encoding: + - chunked + body: + encoding: UTF-8 + string: | + . + "Person 1 - forename" . + "Person 1 - surname" . + . + . + . + "Constituency 1 - name" . + . + . + "Party 1 - name" . + . + . + "2016-05-03"^^ . + . + . + . + . + "2016-05-03"^^ . + . + . + . + "Person 2 - forename" . + "Person 2 - surname" . + . + . + "Constituency 2 - name" . + . + . + . + "2016-05-03"^^ . + . + . + . + . + "2016-05-03"^^ . + . + . + . + "Person 3 - forename" . + "Person 3 - surname" . + . + . + "Constituency 3 - name" . + . + . + . + "2016-05-03"^^ . + . + . + . + . + "2016-05-03"^^ . + . + . + _:node132919 "C" . + _:node132920 "A" . + _:node132921 "M" . + _:node132922 "G" . + _:node132923 "R" . + _:node132924 "L" . + _:node132925 "H" . + _:node132926 "J" . + _:node132927 "B" . + _:node132928 "D" . + _:node132929 "T" . + _:node132930 "S" . + _:node132931 "E" . + _:node132932 "K" . + _:node132933 "F" . + _:node132934 "U" . + _:node132935 "P" . + _:node132936 "W" . + _:node132937 "V" . + _:node132938 "Q" . + _:node132939 "O" . + _:node132940 "N" . + _:node132941 "I" . + _:node132942 "Y" . + _:node132943 "Z" . + http_version: + recorded_at: Mon, 27 Mar 2017 09:00:18 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/parliament/response_spec.rb b/spec/parliament/response_spec.rb index 5325062..d539855 100644 --- a/spec/parliament/response_spec.rb +++ b/spec/parliament/response_spec.rb @@ -104,6 +104,31 @@ expect(node.type).to eq('http://id.ukpds.org/schema/Person') end end + + it 'filters blank nodes' do + filtered_response = @response.filter(Grom::Node::BLANK) + expect(filtered_response).to be_a(Parliament::Response) + expect(filtered_response.size).to eq(25) + end + + it 'filters a mixture of typed nodes and blank nodes' do + filtered_response = @response.filter('http://id.ukpds.org/schema/Person', Grom::Node::BLANK) + expect(filtered_response[0].size).to eq(3) + expect(filtered_response[1].size).to eq(25) + + filtered_response[0].each do |node| + expect(node.type).to eq('http://id.ukpds.org/schema/Person') + end + end + + it 'filters typed nodes from a mixture of typed nodes and blank nodes' do + filtered_response = @response.filter('http://id.ukpds.org/schema/Person') + expect(filtered_response.size).to eq(3) + + filtered_response.each do |node| + expect(node.type).to eq('http://id.ukpds.org/schema/Person') + end + end end describe '#sort_by' do