diff --git a/Gemfile b/Gemfile index 4a6524b..625d7cf 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,3 @@ gemspec # Include coveralls for CI coverage reports gem 'coveralls', require: false - -gem 'parliament-grom-decorators', path: '../parliament-grom-decorators' -gem 'parliament-ntriple', path: '../parliament-ntriple' diff --git a/lib/parliament.rb b/lib/parliament.rb index 851afd7..934808d 100644 --- a/lib/parliament.rb +++ b/lib/parliament.rb @@ -1,10 +1,8 @@ require 'net/http' -require 'grom' require 'parliament/version' require 'parliament/request' require 'parliament/response' -require 'parliament/utils' require 'parliament/builder' require 'parliament/network_error' diff --git a/lib/parliament/builder/base_response_builder.rb b/lib/parliament/builder/base_response_builder.rb index c0cfe6a..b4985c5 100644 --- a/lib/parliament/builder/base_response_builder.rb +++ b/lib/parliament/builder/base_response_builder.rb @@ -1,14 +1,14 @@ module Parliament module Builder - # API response builder, allowing the user to return the body of an HTTPResponse. + # Base response builder, allowing the user to return the body of an HTTPResponse. # @since 0.7.5 class BaseResponseBuilder # Creates a new BaseReponseBuilder. - # @param [HTTPResponse] response an HTTP response containing n-triple data. - # @param [Module] decorators the decorator modules to provide alias methods to the resulting objects. + # @param [HTTPResponse] response an HTTP response. + # @param [Module] decorators a namespace which contains modules used to decorate the objects we receive. It is not used directly by the BaseResponseBuilder, but is there for API completeness. def initialize(response:, decorators: nil) @response = response - @decorators = decorators + _ = decorators end # Builds a Parliament::Response::BaseResponse. diff --git a/lib/parliament/request/base_request.rb b/lib/parliament/request/base_request.rb index 12631b3..74958e7 100644 --- a/lib/parliament/request/base_request.rb +++ b/lib/parliament/request/base_request.rb @@ -1,6 +1,6 @@ module Parliament module Request - # API request object, allowing the user to make a request to an API. + # Base request object, allowing the user to make a request to an API. # # @since 0.7.5 # @@ -65,15 +65,21 @@ def initialize(base_url: nil, headers: nil, builder: nil, decorators: nil) # # @example HTTP GET request # request = Parliament::Request::BaseRequest.new(base_url: 'http://example.com/people/123' + # + # # url: http://example.com/people/123 + # # response = request.get #=> # # # @example HTTP GET request with URI encoded form values # request = Parliament::Request.new(base_url: 'http://example.com/people/current') + # + # # url: http://example.com/people/current?limit=10&page=4&lang=en-gb + # # response = request.get({ limit: 10, page: 4, lang: 'en-gb' }) #=> # # # @raise [Parliament::ServerError] when the server responds with a 5xx status code. # @raise [Parliament::ClientError] when the server responds with a 4xx status code. - # @raise [Parliament::NoContentResponseError] when the server responds with a 204 status code. + # @raise [Parliament::NoContentResponseError] when the response body is empty. # # @param [Hash] params (optional) additional URI encoded form values to be added to the URI. # diff --git a/lib/parliament/request/url_request.rb b/lib/parliament/request/url_request.rb index b833893..b9b911d 100644 --- a/lib/parliament/request/url_request.rb +++ b/lib/parliament/request/url_request.rb @@ -1,10 +1,10 @@ module Parliament module Request - # API request object, allowing the user to build a request to an API. + # URL request object, allowing the user to build a URL to make a request to an API. # # @since 0.7.5 # - # @attr_reader [String] base_url the base url of our API. (expected: http://example.com - without the trailing slash). + # @attr_reader [String] base_url the endpoint for our API which we will build our requests on. (expected: http://example.com - without the trailing slash). # @attr_reader [Hash] headers the headers being sent in the request. class UrlRequest < Parliament::Request::BaseRequest # Creates a new instance of Parliament::Request::UrlRequest. @@ -14,7 +14,11 @@ class UrlRequest < Parliament::Request::BaseRequest # @param [String] base_url the base url of our api. (expected: http://example.com - without the trailing slash). # @param [Hash] headers the headers being sent in the request. # @param [Parliament::Builder] builder the builder to use in order to build a response. - # @params [Module] decorators the decorator module to use in order to provide possible alias methods for any objects created by the builder. + # @param [Module] decorators the decorator module to use in order to provide possible alias methods for any objects created by the builder. + # @example Passing headers + # + # request = Parliament::Request::UrlRequest.new(base_url: 'http://example.com', headers: { 'Access-Token' => '12345678' }) + # This will create a request with the Access-Token set to 12345678. def initialize(base_url: nil, headers: nil, builder: nil, decorators: nil) @endpoint_parts = [] base_url ||= ENV['PARLIAMENT_BASE_URL'] @@ -46,7 +50,7 @@ def initialize(base_url: nil, headers: nil, builder: nil, decorators: nil) # @param [Array] params parameters passed to the specified method (url part). # @param [Block] block additional block (kept for compatibility with method_missing API). # - # @return [Parliament::Request::UrlRequest] self. + # @return [Parliament::Request::UrlRequest] self (this is to allow method chaining). def method_missing(method, *params, &block) @endpoint_parts << method.to_s @endpoint_parts << params diff --git a/lib/parliament/utils.rb b/lib/parliament/utils.rb deleted file mode 100644 index c0ab7f3..0000000 --- a/lib/parliament/utils.rb +++ /dev/null @@ -1,135 +0,0 @@ -module Parliament - # Namespace for helper methods used with parliament-ruby. - # - # @since 0.6.0 - module Utils - # Sort an Array of Objects in ascending order. The major difference between this implementation of sort_by and the - # standard one is that our implementation includes objects that return nil for our parameter values. - # - # @see Parliament::Utils.reverse_sort_by - # - # @example Sorting a list of objects by date - # response = parliament.people('123').get.filter('http://id.ukpds.org/schema/Person') - # - # objects = response.first.incumbencies - # - # args = { - # list: objects, - # parameters: [:endDate], - # prepend_rejected: false - # } - # - # sorted_list = Parliament::Util.sort_by(args) - # - # sorted_list.each { |incumbency| puts incumbency.respond_to?(:endDate) ? incumbency.endDate : 'Current' } - # # http://id.ukpds.org/1121 - 1981-07-31 - # # http://id.ukpds.org/5678 - 1991-03-15 - # # http://id.ukpds.org/1234 - 1997-01-01 - # # http://id.ukpds.org/9101 - 2011-09-04 - # # http://id.ukpds.org/3141 - Current - # - # @param [Hash] args a hash of arguments. - # @option args [Array] :list the 'list' which we are sorting. - # @option args [Array] :parameters an array of parameters we are sorting by. - # @option args [Boolean] :prepend_rejected (true) should objects that do not respond to our parameters be prepended? - # - # @return [Array] a sorted array of objects using the args passed in. - def self.sort_by(args) - rejected = [] - args = sort_defaults.merge(args) - list = args[:list].dup - parameters = args[:parameters] - - list, rejected = prune_list(list, rejected, parameters) - - list = sort_list(list, parameters) - - # Any rejected (nil) values will be added to the start of the result unless specified otherwise - args[:prepend_rejected] ? rejected.concat(list) : list.concat(rejected) - end - - # Sort an Array of Objects in descending order. Largely, this implementation runs Parliament::Utils.sort_by and - # calls reverse! on the result. - # - # @see Parliament::Utils.sort_by - # - # @example Sorting a list of objects by date - # response = parliament.people('123').get.filter('http://id.ukpds.org/schema/Person') - # - # objects = response.first.incumbencies - # - # args = { - # list: objects, - # parameters: [:endDate], - # prepend_rejected: false - # } - # - # sorted_list = Parliament::Util.reverse_sort_by(args) - # - # sorted_list.each { |incumbency| puts incumbency.respond_to?(:endDate) ? incumbency.endDate : 'Current' } - # # http://id.ukpds.org/3141 - Current - # # http://id.ukpds.org/9101 - 2011-09-04 - # # http://id.ukpds.org/1234 - 1997-01-01 - # # http://id.ukpds.org/5678 - 1991-03-15 - # # http://id.ukpds.org/1121 - 1981-07-31 - # - # @param [Hash] args a hash of arguments. - # @option args [Array] :list the 'list' which we are sorting. - # @option args [Array] :parameters an array of parameters we are sorting by. - # @option args [Boolean] :prepend_rejected (true) should objects that do not respond to our parameters be prepended? - # - # @return [Array] a sorted array of objects using the args passed in. - def self.reverse_sort_by(args) - Parliament::Utils.sort_by(args).reverse! - end - - # Default arguments hash for #sort_by and #reverse_sort_by. - # - # @see Parliament::Utils.sort_by - # @see Parliament::Utils.reverse_sort_by - # - # @return [Hash] default arguments used in sorting methods. - def self.sort_defaults - { prepend_rejected: true } - end - - # @!method self.prune_list(list, rejected, parameters) - # Prune all objects that do not respond to a given array of parameters. - # - # @private - # @!scope class - # @!visibility private - # - # @param [Array] list the 'list' of objects we are pruning from. - # @param [Array] rejected the objects we have pruned from list. - # @param [Array] parameters an array of parameters we are checking. - # - # @return [Array, Array>] an array containing first, the pruned list and secondly, the rejected list. - private_class_method def self.prune_list(list, rejected, parameters) - list.delete_if do |object| - rejected << object unless parameters.all? { |param| !object.send(param).nil? if object.respond_to?(param) } - end - - [list, rejected] - end - - # @!method self.sort_list(list, parameters) - # Sort a given list of objects by a list of parameters. - # - # @private - # @!scope class - # @!visibility private - # - # @param [Array] list the 'list' of objects we are pruning from. - # @param [Array] parameters an array of parameters we are checking. - # - # @return [Array] our sorted list. - private_class_method def self.sort_list(list, parameters) - list.sort_by! do |object| - parameters.map do |param| - object.send(param).is_a?(String) ? I18n.transliterate(object.send(param)).downcase : object.send(param) - end - end - end - end -end diff --git a/lib/parliament/version.rb b/lib/parliament/version.rb index 7e25613..4ed56e6 100644 --- a/lib/parliament/version.rb +++ b/lib/parliament/version.rb @@ -1,3 +1,3 @@ module Parliament - VERSION = '0.7.4'.freeze + VERSION = '0.7.5.pre'.freeze end diff --git a/parliament-ruby.gemspec b/parliament-ruby.gemspec index 6763537..983a618 100644 --- a/parliament-ruby.gemspec +++ b/parliament-ruby.gemspec @@ -20,8 +20,6 @@ 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.6' - spec.add_development_dependency 'bundler', '~> 1.13' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rspec', '~> 3.0' @@ -29,4 +27,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'simplecov', '~> 0.12' spec.add_development_dependency 'vcr', '~> 3.0' spec.add_development_dependency 'webmock', '~> 2.3' + spec.add_development_dependency 'parliament-grom-decorators' end diff --git a/spec/fixtures/vcr_cassettes/Parliament_Request_BaseRequest/_get/it_accepts_query_parameters/returns_a_Net_HTTPResponse.yml b/spec/fixtures/vcr_cassettes/Parliament_Request_BaseRequest/_get/it_accepts_query_parameters/sets_the_query_parameters_correctly_when_passed_in.yml similarity index 50% rename from spec/fixtures/vcr_cassettes/Parliament_Request_BaseRequest/_get/it_accepts_query_parameters/returns_a_Net_HTTPResponse.yml rename to spec/fixtures/vcr_cassettes/Parliament_Request_BaseRequest/_get/it_accepts_query_parameters/sets_the_query_parameters_correctly_when_passed_in.yml index e0b309b..b53b64a 100644 --- a/spec/fixtures/vcr_cassettes/Parliament_Request_BaseRequest/_get/it_accepts_query_parameters/returns_a_Net_HTTPResponse.yml +++ b/spec/fixtures/vcr_cassettes/Parliament_Request_BaseRequest/_get/it_accepts_query_parameters/sets_the_query_parameters_correctly_when_passed_in.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: get - uri: http://localhost:3030/people?id=3898&source=mnisId + uri: http://localhost:3030/people/lookup?id=3898&source=mnisId body: encoding: US-ASCII string: '' @@ -11,10 +11,9 @@ http_interactions: - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 Accept: - "*/*" + - application/n-triples User-Agent: - Ruby - Host: - - localhost:3030 response: status: code: 200 @@ -29,21 +28,19 @@ http_interactions: Content-Type: - application/n-triples; charset=utf-8 Etag: - - W/"070ba81d6dda5d42fda1032187263084" + - W/"cef63e03e8318b86a5ac9edfcea4cec2" Cache-Control: - max-age=0, private, must-revalidate X-Request-Id: - - d49bec10-10ca-47be-9ac3-799d0cba9b1e + - 77ad78fd-da62-4ec7-9314-10dce39a01bc X-Runtime: - - '0.030821' + - '0.094125' Transfer-Encoding: - chunked body: encoding: UTF-8 - string: | - . - "Person - givenName" . - "Person - familyName" . - http_version: - recorded_at: Tue, 21 Feb 2017 17:08:52 GMT + string: " + .\n" + http_version: + recorded_at: Thu, 20 Apr 2017 11:13:03 GMT recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Request_BaseRequest/_get/it_returns_a_status_code_of_200_and_/returns_a_Net_HTTPResponse.yml b/spec/fixtures/vcr_cassettes/Parliament_Request_BaseRequest/_get/it_returns_a_status_code_of_200_and_/returns_a_Parliament_Response_BaseResponse.yml similarity index 98% rename from spec/fixtures/vcr_cassettes/Parliament_Request_BaseRequest/_get/it_returns_a_status_code_of_200_and_/returns_a_Net_HTTPResponse.yml rename to spec/fixtures/vcr_cassettes/Parliament_Request_BaseRequest/_get/it_returns_a_status_code_of_200_and_/returns_a_Parliament_Response_BaseResponse.yml index 04c4ead..128247c 100644 --- a/spec/fixtures/vcr_cassettes/Parliament_Request_BaseRequest/_get/it_returns_a_status_code_of_200_and_/returns_a_Net_HTTPResponse.yml +++ b/spec/fixtures/vcr_cassettes/Parliament_Request_BaseRequest/_get/it_returns_a_status_code_of_200_and_/returns_a_Parliament_Response_BaseResponse.yml @@ -32,9 +32,9 @@ http_interactions: Cache-Control: - max-age=0, private, must-revalidate X-Request-Id: - - c6300663-a798-4e81-90bf-3d19f7380b39 + - a63c6493-f46a-4365-aa4b-850969e9b90b X-Runtime: - - '0.317661' + - '0.187055' Transfer-Encoding: - chunked body: @@ -79,5 +79,5 @@ http_interactions: . "Sinn Fein" . http_version: - recorded_at: Tue, 04 Apr 2017 14:55:59 GMT + recorded_at: Thu, 20 Apr 2017 10:58:30 GMT recorded_with: VCR 3.0.3 diff --git a/spec/fixtures/vcr_cassettes/Parliament_Request_UrlRequest/_query_url/makes_a_request_to_the_correctly_built_endpoint.yml b/spec/fixtures/vcr_cassettes/Parliament_Request_UrlRequest/_query_url/makes_a_request_to_the_correctly_built_endpoint.yml new file mode 100644 index 0000000..0d2ea6c --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Parliament_Request_UrlRequest/_query_url/makes_a_request_to_the_correctly_built_endpoint.yml @@ -0,0 +1,98 @@ +--- +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/"05e7a0e4523d4b7fd2d5c532758dd189" + Cache-Control: + - max-age=0, private, must-revalidate + X-Request-Id: + - 5e6b946b-1e71-43b5-adef-cb0599adaa60 + X-Runtime: + - '2.041267' + Transfer-Encoding: + - chunked + body: + encoding: UTF-8 + string: | + . + "Person 1 - givenName" . + "Person 1 - familyName" . + . + . + . + . + "2006-05-30"^^ . + . + "House of Lords" . + . + . + "1987-06-11"^^ . + "2005-05-05"^^ . + . + "Labour" . + . + . + . + "2006-05-30"^^ . + . + "Person 2 - givenName" . + "Person 2 - familyName" . + . + . + . + . + "2015-10-26"^^ . + . + . + "1987-06-11"^^ . + "2015-03-30"^^ . + . + . + . + "2015-10-26"^^ . + . + "Person 3 - givenName" . + "Person 3 - familyName" . + . + . + . + . + "2010-07-15"^^ . + . + . + "1997-05-01"^^ . + "2010-05-06"^^ . + . + . + . + "2010-07-15"^^ . + + http_version: + recorded_at: Thu, 20 Apr 2017 11:55:10 GMT +recorded_with: VCR 3.0.3 diff --git a/spec/parliament/request/base_request_spec.rb b/spec/parliament/request/base_request_spec.rb index be44517..aad4295 100644 --- a/spec/parliament/request/base_request_spec.rb +++ b/spec/parliament/request/base_request_spec.rb @@ -29,8 +29,8 @@ context 'it returns a status code of 200 and ..' do let(:base_response) { Parliament::Request::BaseRequest.new(base_url: 'http://localhost:3030/parties/current').get } - it 'returns a Net::HTTPResponse' do - expect(base_response.response).to be_a(Net::HTTPResponse) + it 'returns a Parliament::Response::BaseResponse' do + expect(base_response).to be_a(Parliament::Response::BaseResponse) end end @@ -61,10 +61,11 @@ end context 'it accepts query parameters' do - let(:base_response) { Parliament::Request::BaseRequest.new(base_url: 'http://localhost:3030/people').get(params: { source: 'mnisId', id: '3898' }) } + it 'sets the query parameters correctly when passed in' do + Parliament::Request::BaseRequest.new(base_url: 'http://localhost:3030/people/lookup').get(params: { source: 'mnisId', id: '3898' }) - it 'returns a Net::HTTPResponse' do - expect(base_response.response).to be_a(Net::HTTPResponse) + expect(WebMock).to have_requested(:get, 'http://localhost:3030/people/lookup?id=3898&source=mnisId'). + with(:headers => {'Accept'=>['*/*', 'application/n-triples'], 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).once end end diff --git a/spec/parliament/request/url_request_spec.rb b/spec/parliament/request/url_request_spec.rb index ac43f0d..1c0ed5e 100644 --- a/spec/parliament/request/url_request_spec.rb +++ b/spec/parliament/request/url_request_spec.rb @@ -4,7 +4,6 @@ describe '#method_missing' do subject { Parliament::Request::UrlRequest.new(base_url: 'http://test.com', decorators: Parliament::Grom::Decorator) } - it 'stores method names in @endpoint_parts' do request = subject.people @@ -32,4 +31,16 @@ expect(subject.send(:respond_to_missing?, :bar)).to eq(true) end end + + describe '#query_url' do + it 'makes a request to the correctly built endpoint' do + request = Parliament::Request::UrlRequest.new(base_url: 'http://localhost:3030', decorators: Parliament::Grom::Decorator) + + request.people.members.current.get + + expect(WebMock).to have_requested(:get, 'http://localhost:3030/people/members/current'). + with(:headers => {'Accept'=>['*/*', 'application/n-triples'], 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).once + + end + end end \ No newline at end of file diff --git a/spec/parliament/response/base_response_spec.rb b/spec/parliament/response/base_response_spec.rb index d35360a..f6e9964 100644 --- a/spec/parliament/response/base_response_spec.rb +++ b/spec/parliament/response/base_response_spec.rb @@ -1,10 +1,20 @@ require_relative '../../spec_helper' -describe Parliament::Response, vcr: true do +describe Parliament::Response::BaseResponse do let(:response) { 'hello world' } - subject { Parliament::Response::BaseResponse.new(response) } + let(:nil_response) { nil } - it 'has a response' do - expect(subject.response).to eq('hello world') + describe 'setting the response variable' do + it 'sets the response variable when a value is passed in' do + base_response = Parliament::Response::BaseResponse.new('hello world') + + expect(base_response.response).to eq('hello world') + end + + it 'sets the response variable to nil when a nil value is passed in' do + base_response = Parliament::Response::BaseResponse.new(nil) + + expect(base_response.response).to be(nil) + end end end diff --git a/spec/parliament/utils_spec.rb b/spec/parliament/utils_spec.rb deleted file mode 100644 index bb65036..0000000 --- a/spec/parliament/utils_spec.rb +++ /dev/null @@ -1,187 +0,0 @@ -require_relative '../spec_helper' - -describe Parliament::Utils, vcr: true do - describe '#sort_by' do - context 'all nodes have the parameter being sorted on' do - it 'returns a response sorted by personFamilyName' do - response = Parliament::Request::UrlRequest.new(base_url: 'http://localhost:3030', - builder: Parliament::Builder::NTripleResponseBuilder).people.get - - sorted_people = Parliament::Utils.sort_by({ - list: response.nodes, - parameters: [:personFamilyName] - }) - - expect(sorted_people.first.personGivenName).to eq('Jane') - end - - it 'returns a response sorted by seatIncumbencyStartDate' do - response = Parliament::Request::UrlRequest.new(base_url: 'http://localhost:3030', - builder: Parliament::Builder::NTripleResponseBuilder).people('2c196540-13f3-4c07-8714-b356912beceb').get - filtered_response = response.filter('http://id.ukpds.org/schema/SeatIncumbency') - - sorted_incumbencies = Parliament::Utils.sort_by({ - list: filtered_response.nodes, - parameters: [:incumbencyStartDate] - }) - - expect(sorted_incumbencies.first.incumbencyStartDate).to eq('1987-06-11') - expect(sorted_incumbencies[1].incumbencyStartDate).to eq('1992-04-09') - end - end - - context 'not all nodes have the parameter being sorted on' do - it 'returns a response sorted by personGivenName' do - response = Parliament::Request::UrlRequest.new(base_url: 'http://localhost:3030', - builder: Parliament::Builder::NTripleResponseBuilder, - decorators: Parliament::Grom::Decorator).people.get - - sorted_people = Parliament::Utils.sort_by({ - list: response.nodes, - parameters: [:personGivenName] - }) - - expect(sorted_people.first.given_name).to eq('') - expect(sorted_people[1].given_name).to eq('Alice') - end - - it 'returns a response sorted by end_date (it can handle nil values)' do - response = Parliament::Request::UrlRequest.new(base_url: 'http://localhost:3030', - builder: Parliament::Builder::NTripleResponseBuilder, - decorators: Parliament::Grom::Decorator).people('1921fc4a-6867-48fa-a4f4-6df05be005ce').get - person = response.filter('http://id.ukpds.org/schema/Person').first - - sorted_incumbencies = Parliament::Utils.sort_by({ - list: person.incumbencies, - parameters: [:end_date], - prepend_rejected: false - }) - - expect(sorted_incumbencies.last.end_date).to eq(nil) - expect(sorted_incumbencies[sorted_incumbencies.length - 2].end_date).to eq(DateTime.new(2015, 3, 30)) - end - - it 'uses the prepend_rejected parameter correctly - defaults to true so nil values will be at the start' do - response = Parliament::Request::UrlRequest.new(base_url: 'http://localhost:3030', - builder: Parliament::Builder::NTripleResponseBuilder, - decorators: Parliament::Grom::Decorator).people('1921fc4a-6867-48fa-a4f4-6df05be005ce').get - person = response.filter('http://id.ukpds.org/schema/Person').first - - sorted_incumbencies = Parliament::Utils.sort_by({ - list: person.incumbencies, - parameters: [:end_date] - }) - - expect(sorted_incumbencies.first.end_date).to be(nil) - end - - it 'uses the prepend_rejected parameter correctly - when set to false the nil values will be at the end' do - response = Parliament::Request::UrlRequest.new(base_url: 'http://localhost:3030', - builder: Parliament::Builder::NTripleResponseBuilder, - decorators: Parliament::Grom::Decorator).people('1921fc4a-6867-48fa-a4f4-6df05be005ce').get - person = response.filter('http://id.ukpds.org/schema/Person').first - - sorted_incumbencies = Parliament::Utils.sort_by({ - list: person.incumbencies, - parameters: [:end_date], - prepend_rejected: false - }) - - expect(sorted_incumbencies.last.end_date).to be(nil) - end - end - - context 'sorting by multiple parameters' do - it 'returns a response sorted by personFamilyName, then personGivenName' do - response = Parliament::Request::UrlRequest.new(base_url: 'http://localhost:3030', - builder: Parliament::Builder::NTripleResponseBuilder).people.get - - sorted_people = Parliament::Utils.sort_by({ - list: response.nodes, - parameters: [:personFamilyName, :personGivenName] - }) - - expect(sorted_people.first.personGivenName).to eq('Rebecca') - expect(sorted_people[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::UrlRequest.new(base_url: 'http://localhost:3030', - builder: Parliament::Builder::NTripleResponseBuilder).people.get - - sorted_people = Parliament::Utils.sort_by({ - list: response.nodes, - parameters: [:personFamilyName] - }) - - expect(sorted_people.first.personGivenName).to eq('Jane') - expect(sorted_people[1].personGivenName).to eq('Alice') - end - end - - context 'sorting strings with accents' do - it 'returns a response sorted by personGivenName' do - response = Parliament::Request::UrlRequest.new(base_url: 'http://localhost:3030', - builder: Parliament::Builder::NTripleResponseBuilder).people.get - - sorted_people = Parliament::Utils.sort_by({ - list: response.nodes, - parameters: [:personGivenName] - }) - - expect(sorted_people.first.personGivenName).to eq('Sarah') - expect(sorted_people[1].personGivenName).to eq('Sóley') - expect(sorted_people[2].personGivenName).to eq('Solomon') - end - - it 'returns a response sorted by personFamilyName, personGivenName' do - response = Parliament::Request::UrlRequest.new(base_url: 'http://localhost:3030', - builder: Parliament::Builder::NTripleResponseBuilder).people.get - - sorted_people = Parliament::Utils.sort_by({ - list: response.nodes, - parameters: [:personFamilyName, :personGivenName] - }) - - expect(sorted_people.first.personGivenName).to eq('Solomon') - expect(sorted_people[1].personGivenName).to eq('Sophie') - expect(sorted_people[2].personGivenName).to eq('Sarah') - end - end - end - - describe '#reverse_sort_by' do - it 'returns a response sorted by incumbencyStartDate' do - response = Parliament::Request::UrlRequest.new(base_url: 'http://localhost:3030', - builder: Parliament::Builder::NTripleResponseBuilder, - decorators: Parliament::Grom::Decorator).people('2c196540-13f3-4c07-8714-b356912beceb').get - person = response.filter('http://id.ukpds.org/schema/Person').first - - sorted_incumbencies = Parliament::Utils.reverse_sort_by({ - list: person.incumbencies, - parameters: [:start_date] - }) - - expect(sorted_incumbencies[0].start_date).to eq(DateTime.new(2015, 5, 7)) - expect(sorted_incumbencies[1].start_date).to eq(DateTime.new(2010, 5, 6)) - end - - it 'returns a response sorted by end_date (it can handle nil values)' do - response = Parliament::Request::UrlRequest.new(base_url: 'http://localhost:3030', - builder: Parliament::Builder::NTripleResponseBuilder, - decorators: Parliament::Grom::Decorator).people('1921fc4a-6867-48fa-a4f4-6df05be005ce').get - person = response.filter('http://id.ukpds.org/schema/Person').first - - sorted_incumbencies = Parliament::Utils.reverse_sort_by({ - list: person.incumbencies, - parameters: [:end_date], - prepend_rejected: false - }) - - expect(sorted_incumbencies.first.end_date).to be(nil) - expect(sorted_incumbencies[1].end_date).to eq(DateTime.new(2015, 3, 30)) - end - end -end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cfaca2d..4ddabaf 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,7 +9,7 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) require 'parliament' require 'parliament/grom/decorator' -require 'parliament/ntriple' + require 'webmock/rspec' require 'vcr'