From 4f78b5e6d3ab6d39b181c6da4c38c8ba241d1aeb Mon Sep 17 00:00:00 2001 From: Harry Maclean Date: Mon, 5 Mar 2018 15:38:48 +0000 Subject: [PATCH 1/2] Add endpoint for persons-with-significant-control --- lib/companies_house/client.rb | 48 +++++++++++-------- spec/companies_house/client_spec.rb | 73 +++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 19 deletions(-) diff --git a/lib/companies_house/client.rb b/lib/companies_house/client.rb index c70a0e6..accc4df 100644 --- a/lib/companies_house/client.rb +++ b/lib/companies_house/client.rb @@ -27,27 +27,17 @@ def company(id) request(:company, "company/#{id}", {}, make_transaction_id, id) end - # The API endpoint for company officers is paginated, and not all of the officers may - # be returned in the first request. We deal with this by collating all the pages of - # results into one result set before returning them. def officers(id) - items = [] - offset = 0 - xid = make_transaction_id - - loop do - page = request(:officers, "company/#{id}/officers", - { start_index: offset }, xid, id) - new_items = page["items"] - total = page["total_results"] || new_items.count - - items += new_items - offset += new_items.count - - break if items.count >= total - end + get_all_pages(:officers, "company/#{id}/officers", id) + end - items + def persons_with_significant_control(id) + get_all_pages( + :persons_with_significant_control, + "company/#{id}/persons-with-significant-control", + id, + register_view: true, + ) end def company_search(query, items_per_page: nil, start_index: nil) @@ -66,6 +56,26 @@ def connection private + # Fetch and combine all pages of a paginated API call + def get_all_pages(resource, path, id, query = {}) + items = [] + offset = 0 + xid = make_transaction_id + + loop do + page = request(resource, path, query.merge(start_index: offset), xid, id) + new_items = page["items"] + total = page["total_results"] || new_items.count + + items += new_items + offset += new_items.count + + break if items.count >= total + end + + items + end + def make_transaction_id SecureRandom.hex(10) end diff --git a/spec/companies_house/client_spec.rb b/spec/companies_house/client_spec.rb index a1d4206..f974942 100644 --- a/spec/companies_house/client_spec.rb +++ b/spec/companies_house/client_spec.rb @@ -196,6 +196,79 @@ end end + describe "#persons_with_significant_control" do + include_context "test client" + + subject(:response) { client.persons_with_significant_control(company_id) } + + let(:rest_path) do + "company/#{company_id}/persons-with-significant-control?register_view=true" + end + let(:request_method) { "persons_with_significant_control" } + + context "when all results are on a single page" do + let(:single_page) do + { + items_per_page: 2, + total_results: 2, + start_index: 0, + items: %w[item1 item2], + }.to_json + end + + before do + stub_request(:get, "#{example_endpoint}/#{rest_path}"). + with( + basic_auth: [api_key, ""], + query: { "start_index" => 0, register_view: true }, + ).to_return(body: single_page, status: status) + end + + it "returns items from the one, single page" do + expect(response).to eq(%w[item1 item2]) + end + end + + context "when results are spread across several pages" do + let(:page1) do + { + items_per_page: 1, + total_results: 2, + start_index: 0, + items: ["item1"], + }.to_json + end + + let(:page2) do + { + items_per_page: 1, + total_results: 2, + start_index: 1, + items: ["item2"], + }.to_json + end + + before do + stub_request(:get, "#{example_endpoint}/#{rest_path}"). + with( + basic_auth: [api_key, ""], + query: { "start_index" => 0, register_view: true }, + ). + to_return(body: page1, status: status) + stub_request(:get, "#{example_endpoint}/#{rest_path}"). + with( + basic_auth: [api_key, ""], + query: { "start_index" => 1, register_view: true }, + ). + to_return(body: page2, status: status) + end + + it "returns items from all pages" do + expect(response).to eq(%w[item1 item2]) + end + end + end + describe "#company_search" do include_context "test client" From 96b60ac0d998ad462de9548a5190d2c4eaf51784 Mon Sep 17 00:00:00 2001 From: Harry Maclean Date: Tue, 6 Mar 2018 08:45:38 +0000 Subject: [PATCH 2/2] Bump version to 0.4.0 --- lib/companies_house/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/companies_house/version.rb b/lib/companies_house/version.rb index a7773b0..05c2375 100644 --- a/lib/companies_house/version.rb +++ b/lib/companies_house/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module CompaniesHouse - VERSION = "0.3.0" + VERSION = "0.4.0" end