diff --git a/.gitignore b/.gitignore index 4838c81..be1c306 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,6 @@ /pkg/ /spec/reports/ /tmp/ +*.gem .idea \ No newline at end of file diff --git a/.hound.yml b/.hound.yml new file mode 100644 index 0000000..abca34a --- /dev/null +++ b/.hound.yml @@ -0,0 +1,4 @@ +ruby: + config_file: .rubocop.yml + +fail_on_violations: true \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index b9fb8f9..aafb331 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,4 +2,3 @@ sudo: false language: ruby rvm: - 2.3.1 -before_install: gem install bundler -v 1.13.7 diff --git a/Gemfile b/Gemfile index 773d5e4..625d7cf 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,7 @@ source 'https://rubygems.org' -# Specify your gem's dependencies in parliament.gemspec +# Specify your gem's dependencies in parliament-ruby.gemspec gemspec + +# Include coveralls for CI coverage reports +gem 'coveralls', require: false diff --git a/README.md b/README.md index f64fac4..aa15f93 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Parliament Data API Wrapper (Ruby) [parliament-ruby][parliament-ruby] is a gem created by the [Parliamentary Digital Service][pds] to allow easy communication with the internal parliament data api. -[![License][shield-license]][info-license] +[![Build Status][shield-travis]][info-travis] [![Test Coverage][shield-coveralls]][info-coveralls] [![License][shield-license]][info-license] > **NOTE:** This gem is in active development and is likely to change at short notice. It is not recommended that you use this in any production environment. @@ -128,5 +128,11 @@ If you wish to submit a bug fix or feature, you can create a pull request and it [pds]: https://www.parliament.uk/mps-lords-and-offices/offices/bicameral/parliamentary-digital-service/ [ruby-version]: https://github.com/ukparliament/parliament-ruby/blob/master/.ruby-version +[info-travis]: https://travis-ci.org/ukparliament/parliament-ruby +[shield-travis]: https://img.shields.io/travis/ukparliament/parliament-ruby.svg + +[info-coveralls]: https://coveralls.io/github/ukparliament/parliament-ruby +[shield-coveralls]: https://img.shields.io/coveralls/ukparliament/parliament-ruby.svg + [info-license]: http://www.parliament.uk/site-information/copyright/open-parliament-licence/ [shield-license]: https://img.shields.io/badge/license-Open%20Parliament%20Licence-blue.svg \ No newline at end of file diff --git a/lib/parliament.rb b/lib/parliament.rb index 4348621..425d163 100644 --- a/lib/parliament.rb +++ b/lib/parliament.rb @@ -1,8 +1,10 @@ require 'pry' require 'net/http' +require 'grom' require 'parliament/version' require 'parliament/request' +require 'parliament/response' module Parliament # Your code goes here... diff --git a/lib/parliament/request.rb b/lib/parliament/request.rb index 39ee214..1962d52 100644 --- a/lib/parliament/request.rb +++ b/lib/parliament/request.rb @@ -22,7 +22,12 @@ def respond_to_missing?(method, include_private = false) end def get - Net::HTTP.get(URI(api_endpoint)) + response = Net::HTTP.get_response(URI(api_endpoint)) + + raise StandardError, 'This is a HTTPClientError' if response.is_a?(Net::HTTPClientError) + raise StandardError, 'This is a HTTPServerError' if response.is_a?(Net::HTTPServerError) + + Parliament::Response.new(Grom::Reader.new(response.body).objects) end private diff --git a/lib/parliament/response.rb b/lib/parliament/response.rb new file mode 100644 index 0000000..23d9186 --- /dev/null +++ b/lib/parliament/response.rb @@ -0,0 +1,29 @@ +require 'forwardable' + +module Parliament + class Response + include Enumerable + extend Forwardable + attr_reader :nodes + def_delegators :@nodes, :size, :each, :select, :map, :select!, :map!, :count, :length + + def initialize(nodes) + @nodes = nodes + end + + def filter(*types) + filtered_objects = Array.new(types.size) { [] } + + unless types.empty? + @nodes.each do |node| + type_index = types.index(node.type) + filtered_objects[type_index] << node unless type_index.nil? + end + end + + filtered_objects.each do |objects| + Parliament::Response.new(objects) + end + end + end +end diff --git a/parliament.gemspec b/parliament-ruby.gemspec similarity index 60% rename from parliament.gemspec rename to parliament-ruby.gemspec index 016a1e4..58a47c1 100644 --- a/parliament.gemspec +++ b/parliament-ruby.gemspec @@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'parliament/version' Gem::Specification.new do |spec| - spec.name = 'parliament' + spec.name = 'parliament-ruby' spec.version = Parliament::VERSION spec.authors = ['Matt Rayner'] spec.email = ['mattrayner1@gmail.com'] @@ -13,14 +13,6 @@ Gem::Specification.new do |spec| spec.homepage = 'http://github.com/ukparliament/parliament_ruby' spec.license = 'Open Parliament License' - # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' - # to allow pushing to a single host or delete this section to allow pushing to any host. - if spec.respond_to?(:metadata) - spec.metadata['allowed_push_host'] = 'TODO: Set to \'http://mygemserver.com\'' - else - raise 'RubyGems 2.0 or newer is required to protect against ' \ - 'public gem pushes.' - end spec.files = `git ls-files -z`.split("\x0").reject do |f| f.match(%r{^(test|spec|features)/}) @@ -29,12 +21,15 @@ 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.2.0' + spec.add_development_dependency 'bundler', '~> 1.13' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rspec', '~> 3.0' - spec.add_development_dependency 'rubocop' - spec.add_development_dependency 'pry' - spec.add_development_dependency 'simplecov' - spec.add_development_dependency 'vcr' - spec.add_development_dependency 'webmock' + spec.add_development_dependency 'rubocop', '~> 0.47.1' + spec.add_development_dependency 'pry', '~> 0.10.4' + spec.add_development_dependency 'pry-nav', '~> 0.2.4' + spec.add_development_dependency 'simplecov', '~> 0.12.0' + spec.add_development_dependency 'vcr', '~> 3.0.3' + spec.add_development_dependency 'webmock', '~> 2.3.2' end diff --git a/spec/fixtures/vcr_cassettes/Parliament_Request/_get/returns_a_Net_HTTP_Object.yml b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/it_returns_a_status_code_of_200_and_/returns_27_objects.yml similarity index 98% rename from spec/fixtures/vcr_cassettes/Parliament_Request/_get/returns_a_Net_HTTP_Object.yml rename to spec/fixtures/vcr_cassettes/Parliament_Request/_get/it_returns_a_status_code_of_200_and_/returns_27_objects.yml index b1d4108..a7f3165 100644 --- a/spec/fixtures/vcr_cassettes/Parliament_Request/_get/returns_a_Net_HTTP_Object.yml +++ b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/it_returns_a_status_code_of_200_and_/returns_27_objects.yml @@ -33,9 +33,9 @@ http_interactions: Cache-Control: - max-age=0, private, must-revalidate X-Request-Id: - - 9194c35c-1276-4203-bb73-dd9e2b617144 + - 4de9df0d-8a42-4438-8d3e-c8763af7d228 X-Runtime: - - '0.123767' + - '0.102228' Transfer-Encoding: - chunked body: @@ -96,5 +96,5 @@ http_interactions: . "Independent Social Democrat" . http_version: - recorded_at: Wed, 18 Jan 2017 16:53:33 GMT + recorded_at: Mon, 23 Jan 2017 16:15:49 GMT recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Request/_get/it_returns_a_status_code_of_200_and_/returns_a_Parliament_Response.yml b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/it_returns_a_status_code_of_200_and_/returns_a_Parliament_Response.yml new file mode 100644 index 0000000..c71e485 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/it_returns_a_status_code_of_200_and_/returns_a_Parliament_Response.yml @@ -0,0 +1,100 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/parties/current.nt + 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/"35226693d138904248fc51c972ce2c45" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 2a166491-2a65-427f-b638-25a12b3dd1eb + X-Runtime: + - '0.090822' + Transfer-Encoding: + - chunked + body: + encoding: UTF-8 + string: | + . + "Labour" . + . + "Liberal Democrat" . + . + "Conservative" . + . + "Non-affiliated" . + . + "Independent Labour" . + . + "Independent SDP" . + . + "Crossbench" . + . + "Scottish National Party" . + . + "Independent" . + . + "Independent Conservative" . + . + "Green Party" . + . + "Independent Ulster Unionist" . + . + "Social Democratic Party" . + . + "UK Independence Party" . + . + "Democratic Unionist Party" . + . + "Plaid Cymru" . + . + "Bishops" . + . + "Alliance" . + . + "Anti H Block" . + . + "Liberal" . + . + "Sinn Fein" . + . + "Ulster Unionist Party" . + . + "Social Democratic & Labour Party" . + . + "Opposition Unity" . + . + "Speaker" . + . + "United Kingdom Unionist" . + . + "Independent Social Democrat" . + http_version: + recorded_at: Mon, 23 Jan 2017 16:54:10 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Request/_get/it_returns_a_status_code_of_200_and_/returns_an_array_of_Grom_Node_objects.yml b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/it_returns_a_status_code_of_200_and_/returns_an_array_of_Grom_Node_objects.yml new file mode 100644 index 0000000..c3af6fe --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/it_returns_a_status_code_of_200_and_/returns_an_array_of_Grom_Node_objects.yml @@ -0,0 +1,100 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/parties/current.nt + 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/"35226693d138904248fc51c972ce2c45" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - a8042f97-4219-48f7-8986-c05b1de220a5 + X-Runtime: + - '0.101241' + Transfer-Encoding: + - chunked + body: + encoding: UTF-8 + string: | + . + "Labour" . + . + "Liberal Democrat" . + . + "Conservative" . + . + "Non-affiliated" . + . + "Independent Labour" . + . + "Independent SDP" . + . + "Crossbench" . + . + "Scottish National Party" . + . + "Independent" . + . + "Independent Conservative" . + . + "Green Party" . + . + "Independent Ulster Unionist" . + . + "Social Democratic Party" . + . + "UK Independence Party" . + . + "Democratic Unionist Party" . + . + "Plaid Cymru" . + . + "Bishops" . + . + "Alliance" . + . + "Anti H Block" . + . + "Liberal" . + . + "Sinn Fein" . + . + "Ulster Unionist Party" . + . + "Social Democratic & Labour Party" . + . + "Opposition Unity" . + . + "Speaker" . + . + "United Kingdom Unionist" . + . + "Independent Social Democrat" . + http_version: + recorded_at: Mon, 23 Jan 2017 16:14:20 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Request/_get/it_returns_a_status_code_of_200_and_/returns_linked_objects_where_links_are_in_the_data.yml b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/it_returns_a_status_code_of_200_and_/returns_linked_objects_where_links_are_in_the_data.yml new file mode 100644 index 0000000..21051d2 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/it_returns_a_status_code_of_200_and_/returns_linked_objects_where_links_are_in_the_data.yml @@ -0,0 +1,100 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/people/members/current.nt + 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/"fa950a6e269562ab5979350ba2595602" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 7d3c7e04-fd8d-483d-aa03-3edaea7cdab4 + X-Runtime: + - '0.643812' + 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"^^ . + . + . + http_version: + recorded_at: Mon, 23 Jan 2017 16:19:23 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Request/_get/returns_27_objects.yml b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/returns_27_objects.yml new file mode 100644 index 0000000..a7f3165 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/returns_27_objects.yml @@ -0,0 +1,100 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/parties/current.nt + 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/"35226693d138904248fc51c972ce2c45" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 4de9df0d-8a42-4438-8d3e-c8763af7d228 + X-Runtime: + - '0.102228' + Transfer-Encoding: + - chunked + body: + encoding: UTF-8 + string: | + . + "Labour" . + . + "Liberal Democrat" . + . + "Conservative" . + . + "Non-affiliated" . + . + "Independent Labour" . + . + "Independent SDP" . + . + "Crossbench" . + . + "Scottish National Party" . + . + "Independent" . + . + "Independent Conservative" . + . + "Green Party" . + . + "Independent Ulster Unionist" . + . + "Social Democratic Party" . + . + "UK Independence Party" . + . + "Democratic Unionist Party" . + . + "Plaid Cymru" . + . + "Bishops" . + . + "Alliance" . + . + "Anti H Block" . + . + "Liberal" . + . + "Sinn Fein" . + . + "Ulster Unionist Party" . + . + "Social Democratic & Labour Party" . + . + "Opposition Unity" . + . + "Speaker" . + . + "United Kingdom Unionist" . + . + "Independent Social Democrat" . + http_version: + recorded_at: Mon, 23 Jan 2017 16:15:49 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Request/_get/returns_a_Parliament_Response.yml b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/returns_a_Parliament_Response.yml new file mode 100644 index 0000000..c71e485 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/returns_a_Parliament_Response.yml @@ -0,0 +1,100 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/parties/current.nt + 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/"35226693d138904248fc51c972ce2c45" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 2a166491-2a65-427f-b638-25a12b3dd1eb + X-Runtime: + - '0.090822' + Transfer-Encoding: + - chunked + body: + encoding: UTF-8 + string: | + . + "Labour" . + . + "Liberal Democrat" . + . + "Conservative" . + . + "Non-affiliated" . + . + "Independent Labour" . + . + "Independent SDP" . + . + "Crossbench" . + . + "Scottish National Party" . + . + "Independent" . + . + "Independent Conservative" . + . + "Green Party" . + . + "Independent Ulster Unionist" . + . + "Social Democratic Party" . + . + "UK Independence Party" . + . + "Democratic Unionist Party" . + . + "Plaid Cymru" . + . + "Bishops" . + . + "Alliance" . + . + "Anti H Block" . + . + "Liberal" . + . + "Sinn Fein" . + . + "Ulster Unionist Party" . + . + "Social Democratic & Labour Party" . + . + "Opposition Unity" . + . + "Speaker" . + . + "United Kingdom Unionist" . + . + "Independent Social Democrat" . + http_version: + recorded_at: Mon, 23 Jan 2017 16:54:10 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Request/_get/returns_an_array_of_Grom_Node_objects.yml b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/returns_an_array_of_Grom_Node_objects.yml new file mode 100644 index 0000000..c3af6fe --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/returns_an_array_of_Grom_Node_objects.yml @@ -0,0 +1,100 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/parties/current.nt + 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/"35226693d138904248fc51c972ce2c45" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - a8042f97-4219-48f7-8986-c05b1de220a5 + X-Runtime: + - '0.101241' + Transfer-Encoding: + - chunked + body: + encoding: UTF-8 + string: | + . + "Labour" . + . + "Liberal Democrat" . + . + "Conservative" . + . + "Non-affiliated" . + . + "Independent Labour" . + . + "Independent SDP" . + . + "Crossbench" . + . + "Scottish National Party" . + . + "Independent" . + . + "Independent Conservative" . + . + "Green Party" . + . + "Independent Ulster Unionist" . + . + "Social Democratic Party" . + . + "UK Independence Party" . + . + "Democratic Unionist Party" . + . + "Plaid Cymru" . + . + "Bishops" . + . + "Alliance" . + . + "Anti H Block" . + . + "Liberal" . + . + "Sinn Fein" . + . + "Ulster Unionist Party" . + . + "Social Democratic & Labour Party" . + . + "Opposition Unity" . + . + "Speaker" . + . + "United Kingdom Unionist" . + . + "Independent Social Democrat" . + http_version: + recorded_at: Mon, 23 Jan 2017 16:14:20 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Request/_get/returns_linked_objects_where_links_are_in_the_data.yml b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/returns_linked_objects_where_links_are_in_the_data.yml new file mode 100644 index 0000000..21051d2 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Request/_get/returns_linked_objects_where_links_are_in_the_data.yml @@ -0,0 +1,100 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/people/members/current.nt + 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/"fa950a6e269562ab5979350ba2595602" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 7d3c7e04-fd8d-483d-aa03-3edaea7cdab4 + X-Runtime: + - '0.643812' + 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"^^ . + . + . + http_version: + recorded_at: Mon, 23 Jan 2017 16:19:23 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/returns_an_array_of_arrays_of_objects_filtered_by_type_Party.yml b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/returns_an_array_of_arrays_of_objects_filtered_by_type_Party.yml new file mode 100644 index 0000000..6a8387f --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/returns_an_array_of_arrays_of_objects_filtered_by_type_Party.yml @@ -0,0 +1,100 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/people/members/current.nt + 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/"2505426358eb85f5dbcae2a82e4c5d01" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 9a5e0f2f-ee94-4300-9ab7-297f863824a9 + X-Runtime: + - '1.251670' + 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"^^ . + . + . + http_version: + recorded_at: Tue, 24 Jan 2017 11:14:30 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/returns_an_array_of_arrays_of_objects_filtered_by_type_Person.yml b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/returns_an_array_of_arrays_of_objects_filtered_by_type_Person.yml new file mode 100644 index 0000000..4945def --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/returns_an_array_of_arrays_of_objects_filtered_by_type_Person.yml @@ -0,0 +1,100 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/people/members/current.nt + 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/"2505426358eb85f5dbcae2a82e4c5d01" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 1f6d09ee-daf9-43ab-a584-94b40c433bb2 + X-Runtime: + - '0.664406' + 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"^^ . + . + . + http_version: + recorded_at: Tue, 24 Jan 2017 11:14:28 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/returns_an_empty_array_of_response_objects_when_the_type_passed_in_does_not_exist.yml b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/returns_an_empty_array_of_response_objects_when_the_type_passed_in_does_not_exist.yml new file mode 100644 index 0000000..8e6a518 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/returns_an_empty_array_of_response_objects_when_the_type_passed_in_does_not_exist.yml @@ -0,0 +1,100 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/people/members/current.nt + 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/"fa950a6e269562ab5979350ba2595602" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 24512dd6-d774-4406-ae09-d5b5a8373fe2 + X-Runtime: + - '0.661245' + 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"^^ . + . + . + http_version: + recorded_at: Wed, 25 Jan 2017 10:46:58 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/returns_an_empty_array_when_no_types_are_passed_in.yml b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/returns_an_empty_array_when_no_types_are_passed_in.yml new file mode 100644 index 0000000..952a57e --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/returns_an_empty_array_when_no_types_are_passed_in.yml @@ -0,0 +1,100 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/people/members/current.nt + 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/"fa950a6e269562ab5979350ba2595602" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 24512dd6-d774-4406-ae09-d5b5a8373fe2 + X-Runtime: + - '0.661245' + 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"^^ . + . + . + http_version: + recorded_at: Wed, 25 Jan 2017 10:46:58 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/returns_an_empty_array_when_the_response_is_empty.yml b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/returns_an_empty_array_when_the_response_is_empty.yml new file mode 100644 index 0000000..8e6a518 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Response/_filter/returns_an_empty_array_when_the_response_is_empty.yml @@ -0,0 +1,100 @@ +--- +http_interactions: +- request: + method: get + uri: http://localhost:3030/people/members/current.nt + 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/"fa950a6e269562ab5979350ba2595602" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 24512dd6-d774-4406-ae09-d5b5a8373fe2 + X-Runtime: + - '0.661245' + 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"^^ . + . + . + http_version: + recorded_at: Wed, 25 Jan 2017 10:46:58 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/parliament/request_spec.rb b/spec/parliament/request_spec.rb index 05ec8b4..bc16dc2 100644 --- a/spec/parliament/request_spec.rb +++ b/spec/parliament/request_spec.rb @@ -1,4 +1,5 @@ require_relative '../spec_helper' +require 'pry' describe Parliament::Request, vcr: true do context 'with endpoint set via an initializer' do @@ -63,9 +64,47 @@ end describe '#get' do - subject { Parliament::Request.new(base_url: 'http://localhost:3030') } - it 'returns a Net::HTTP Object' do - expect(subject.parties.current.get).to be_a(String) + context 'it returns a status code of 200 and ..' do + subject { Parliament::Request.new(base_url: 'http://localhost:3030').parties.current.get } + + it 'returns a Parliament::Response' do + expect(subject).to be_a(Parliament::Response) + end + + it 'returns 27 objects' do + expect(subject.size).to eq(27) + end + + it 'returns an array of Grom::Node objects' do + subject.each do |object| + expect(object).to be_a(Grom::Node) + end + end + + # NOTE: ensure all fixtures use anonymised data + it 'returns linked objects where links are in the data' do + linked_objects = Parliament::Request.new(base_url: 'http://localhost:3030').people.members.current.get + + expect(linked_objects.first.sittings.first.constituencies.first.constituencyName).to eq('Constituency 1 - name') + end + end + + context 'it returns any other status code than a 200' do + it 'and raises client error when status is within the 400 range' do + stub_request(:get, 'http://localhost:3030/dogs/cats.nt').to_return(status: 400) + + expect do + Parliament::Request.new(base_url: 'http://localhost:3030').dogs.cats.get + end.to raise_error(StandardError, 'This is a HTTPClientError') + end + + it 'and raises server error when status is within the 500 range' do + stub_request(:get, 'http://localhost:3030/parties/current.nt').to_return(status: 500) + + expect do + Parliament::Request.new(base_url: 'http://localhost:3030').parties.current.get + end.to raise_error(StandardError, 'This is a HTTPServerError') + end end end end diff --git a/spec/parliament/response_spec.rb b/spec/parliament/response_spec.rb new file mode 100644 index 0000000..499ed05 --- /dev/null +++ b/spec/parliament/response_spec.rb @@ -0,0 +1,89 @@ +require_relative '../spec_helper' +require 'pry' + +describe Parliament::Response, vcr: true do + let(:nodes) { [] } + subject { Parliament::Response.new(nodes) } + + describe '#initialize' do + it 'sets an instance variable for the nodes' do + expect(subject.instance_variable_get(:@nodes)).to eq(nodes) + end + + it 'should respond to size' do + expect(subject).to respond_to(:size) + end + + it 'should respond to each' do + expect(subject).to respond_to(:each) + end + + it 'should respond to select' do + expect(subject).to respond_to(:select) + end + + it 'should respond to map' do + expect(subject).to respond_to(:map) + end + + it 'should respond to select!' do + expect(subject).to respond_to(:select!) + end + + it 'should respond to map!' do + expect(subject).to respond_to(:map!) + end + + it 'should respond to count' do + expect(subject).to respond_to(:count) + end + + it 'should respond to length' do + expect(subject).to respond_to(:length) + end + end + + describe '#filter' do + before(:each) do + @response = Parliament::Request.new(base_url: 'http://localhost:3030').people.members.current.get + end + + it 'returns an empty array when no types are passed in' do + filtered_response = @response.filter + + expect(filtered_response.size).to eq(0) + end + + it 'returns an array of arrays of objects filtered by type Person' do + filtered_response = @response.filter('http://id.ukpds.org/schema/Person', 'http://id.ukpds.org/schema/Party') + + expect(filtered_response.first.size).to eq(3) + + expect(filtered_response.size).to eq(2) + + filtered_response.first.each do |node| + expect(node.type).to eq('http://id.ukpds.org/schema/Person') + end + end + + it 'returns an array of arrays of objects filtered by type Party' do + filtered_response = @response.filter('http://id.ukpds.org/schema/Person', 'http://id.ukpds.org/schema/Party') + expect(filtered_response[1].size).to eq(1) + + filtered_response[1].each do |node| + expect(node.type).to eq('http://id.ukpds.org/schema/Party') + end + end + + it 'returns an empty array of response objects when the type passed in does not exist' do + filtered_response = @response.filter('http://id.ukpds.org/schema/Person', 'banana') + + expect(filtered_response.first.size).to eq(3) + expect(filtered_response[1].size).to eq(0) + end + + it 'returns an empty array when the response is empty' do + expect(subject.filter('http://id.ukpds.org/schema/Person').first.size).to eq(0) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 89d0d3d..1d4c6a5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,8 +1,14 @@ require 'simplecov' +require 'coveralls' +SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ + Coveralls::SimpleCov::Formatter, + SimpleCov::Formatter::HTMLFormatter +] SimpleCov.start $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) require 'parliament' +require 'webmock/rspec' require 'vcr'