Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metods for retrieving persons of significant control and filings history #27

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ The endpoints currently implemented by the gem are:
| --------------------------- | --------------------------------------- | ----------- |
| `.company(company_number)` | `GET /company/:company_number` | Retrieves a company profile. |
| `.officers(company_number)` | `GET /company/:company_number/officers` | Retrieves a list of company officers. |
| `.pscs(company_number)` | `GET /company/:company_number/persons-with-significant-control` | Retrieves a list of company persons of significant control. |
| `.filing_history` | `GET /company/:company_number/filing_history` | Retrieves a list of filing history. |

### .company
This method implements the [readCompanyProfile](https://developer.companieshouse.gov.uk/api/docs/company/company_number/readCompanyProfile.html)
Expand All @@ -99,6 +101,12 @@ key from the
[officerList](https://developer.companieshouse.gov.uk/api/docs/company/company_number/officers/officerList-resource.html)
resource(s) which it reads.

### .pscs
This method implements the [list] method(https://developer.companieshouse.gov.uk/api/docs/company/company_number/persons-with-significant-control/persons-with-significant-control.html). It will make one or more request againsts this API, as necessary, to obtain the full list of company persons of significant control. It returns only the values under the `items` key from the resource(s) which it reads.

### .filing_history
This method implements the catching of filing history.

### Other API Methods
While there are other resources exposed by the
[Companies House API](https://developer.companieshouse.gov.uk/api/docs/index.html),
Expand Down
32 changes: 25 additions & 7 deletions lib/companies_house/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,41 @@ def company(id)
# 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)
return gather_items(:officers, id, '/officers')
end

# The API endpoint for persons of significant control is paginated,
# We deal with this by collating all the pages of results into one
# result set before returning them.
def pscs(id)
return gather_items(:pscs, id, '/persons-with-significant-control')
end

def charges(id)
return gather_items(:charges, id, '/charges')
end

def filing_history(id)
return gather_items(:filing_history, id, '/filing-history')
end

# This method can be used for gathering items from all
# pages of a paginated API query
def gather_items(resource, id, extra_path)
items = []
offset = 0
xid = make_transaction_id

loop do
page = request(:officers, id, '/officers', { start_index: offset }, xid)
page = request(resource, id, extra_path, { start_index: items.count }, xid)
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
return items
end

def connection
@connection ||= Net::HTTP.new(endpoint.host, endpoint.port).tap do |conn|
Expand Down
6 changes: 3 additions & 3 deletions lib/companies_house/not_found_error.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

module CompaniesHouse
# Specific error class for when a company cannot be found (for example, if the company
# Specific error class for when a resource cannot be found (for example, if the company
# number given is invaid)
class NotFoundError < APIError
def initialize(company_id = nil, response = nil)
super("Company #{company_id || 'nil'} not found", response)
def initialize(resource = nil, company_id = nil, response = nil)
super("#{resource || 'Unknown'} resource (company_id: #{company_id || 'nil'}) not found", response)
end
end
end
2 changes: 1 addition & 1 deletion lib/companies_house/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def parse(response, company_id)
when '401'
raise CompaniesHouse::AuthenticationError, response
when '404'
raise CompaniesHouse::NotFoundError.new(company_id, response)
raise CompaniesHouse::NotFoundError.new(resource_type, company_id, response)
when '429'
raise CompaniesHouse::RateLimitError, response
else
Expand Down