class CompaniesHouse::Client
This class provides an interface to the Companies House API at api.companieshouse.gov.uk. Specifically, it manages the connections and arranges requests.
Constants
- ENDPOINT
Attributes
api_key[R]
endpoint[R]
instrumentation[R]
Public Class Methods
new(config)
click to toggle source
# File lib/companies_house/client.rb, line 18 def initialize(config) raise ArgumentError, "Missing API key" unless config[:api_key] @api_key = config[:api_key] @endpoint = URI(config[:endpoint] || ENDPOINT) @open_timeout = config[:open_timeout] || 60 @read_timeout = config[:read_timeout] || 60 @instrumentation = configure_instrumentation(config[:instrumentation]) raise ArgumentError, "HTTP is not supported" if @endpoint.scheme != "https" end
Public Instance Methods
company(id)
click to toggle source
# File lib/companies_house/client.rb, line 33 def company(id) request(:company, "company/#{id}", {}, make_transaction_id, id) end
company_search(query, items_per_page: nil, start_index: nil)
click to toggle source
# File lib/companies_house/client.rb, line 61 def company_search(query, items_per_page: nil, start_index: nil) request( :company_search, "search/companies", { q: query, items_per_page: items_per_page, start_index: start_index }.compact, ) end
connection()
click to toggle source
# File lib/companies_house/client.rb, line 69 def connection @connection ||= Net::HTTP.new(endpoint.host, endpoint.port).tap do |conn| conn.use_ssl = true conn.open_timeout = @open_timeout conn.read_timeout = @read_timeout end end
end_connection()
click to toggle source
# File lib/companies_house/client.rb, line 29 def end_connection @connection.finish if @connection&.started? end
filing_history_item(id, transaction_id)
click to toggle source
# File lib/companies_house/client.rb, line 54 def filing_history_item(id, transaction_id) request( :filing_history_item, "company/#{id}/filing-history/#{transaction_id}", ) end
filing_history_list(id)
click to toggle source
# File lib/companies_house/client.rb, line 50 def filing_history_list(id) get_all_pages(:filing_history_list, "company/#{id}/filing-history", id) end
officers(id)
click to toggle source
# File lib/companies_house/client.rb, line 37 def officers(id) get_all_pages(:officers, "company/#{id}/officers", id) end
persons_with_significant_control(id, register_view: false)
click to toggle source
# File lib/companies_house/client.rb, line 41 def persons_with_significant_control(id, register_view: false) get_all_pages( :persons_with_significant_control, "company/#{id}/persons-with-significant-control", id, register_view: register_view, ) end
request(resource, path, params = {}, transaction_id = make_transaction_id, resource_id = nil, headers = {})
click to toggle source
# File lib/companies_house/client.rb, line 77 def request(resource, path, params = {}, transaction_id = make_transaction_id, resource_id = nil, headers = {}) Request.new( connection: connection, api_key: @api_key, endpoint: @endpoint, path: path, query: params, resource_type: resource, resource_id: resource_id, transaction_id: transaction_id, instrumentation: instrumentation, headers: headers, ).execute end
Private Instance Methods
configure_instrumentation(instrumentation)
click to toggle source
# File lib/companies_house/client.rb, line 123 def configure_instrumentation(instrumentation) return instrumentation unless instrumentation.nil? if defined?(ActiveSupport::Notifications) Instrumentation::ActiveSupport else Instrumentation::Null end end
get_all_pages(resource, path, id, query = {})
click to toggle source
Fetch and combine all pages of a paginated API call
# File lib/companies_house/client.rb, line 100 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
make_transaction_id()
click to toggle source
# File lib/companies_house/client.rb, line 119 def make_transaction_id SecureRandom.hex(10) end