Skip to content

Commit

Permalink
Merge pull request #43 from gocardless/hmac/persons-with-significant-…
Browse files Browse the repository at this point in the history
…control

Add endpoint for persons-with-significant-control
  • Loading branch information
Harry Maclean authored Mar 6, 2018
2 parents 69a7f43 + 96b60ac commit adf34be
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 20 deletions.
48 changes: 29 additions & 19 deletions lib/companies_house/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/companies_house/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module CompaniesHouse
VERSION = "0.3.0"
VERSION = "0.4.0"
end
73 changes: 73 additions & 0 deletions spec/companies_house/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit adf34be

Please sign in to comment.