companies-house-rest 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +20 -20
- data/README.md +154 -148
- data/companies-house-rest.gemspec +34 -32
- data/lib/companies_house.rb +11 -0
- data/lib/companies_house/api_error.rb +18 -18
- data/lib/companies_house/authentication_error.rb +10 -10
- data/lib/companies_house/client.rb +90 -82
- data/lib/companies_house/not_found_error.rb +14 -11
- data/lib/companies_house/rate_limit_error.rb +10 -10
- data/lib/companies_house/request.rb +93 -94
- data/lib/companies_house/version.rb +5 -5
- metadata +20 -20
- data/lib/companies-house.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 341246c80b5b92f5e8b715d582b4006b561c2477
|
4
|
+
data.tar.gz: 5036e6f1eb1c26203792d203f41ecdbde535be5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66d5ff4591518013685b9ab7923c8ca70f831fa1e2777bf420ccc3d9bc5e5a6484e795591c11614feef46f567da51a7db39e11f3f0b2ed28088f491ba0e7eed6
|
7
|
+
data.tar.gz: 80c0979bf45534fa8e46bc06d3961528ca5615a26b4ab744f03b665f51502620718c79b2715c120f26248ea27fd93b424e47cf5b8f23d6969d58d06e70cdaab2
|
data/LICENSE
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
Copyright (c) 2016 GOCARDLESS LTD
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
1
|
+
Copyright (c) 2016 GOCARDLESS LTD
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,148 +1,154 @@
|
|
1
|
-
# CompaniesHouse::Client
|
2
|
-
|
3
|
-
[![CircleCI](https://circleci.com/gh/gocardless/companies-house-rest.svg?style=svg)](https://circleci.com/gh/gocardless/companies-house-rest)
|
4
|
-
|
5
|
-
This Gem implements an API client for the Companies House REST API. It can be
|
6
|
-
used to look up information about companies registered in the United Kingdom.
|
7
|
-
As of July 2016, this API is described by Companies House as a "beta service."
|
8
|
-
More information about this free API can be found
|
9
|
-
[on the Companies House API website](https://developer.companieshouse.gov.uk/api/docs/index.html).
|
10
|
-
|
11
|
-
To interact the older [CompaniesHouse XML-based API](http://xmlgw.companieshouse.gov.uk/),
|
12
|
-
see the gem [companies-house-gateway](https://github.com/gocardless/companies-house-gateway-ruby).
|
13
|
-
(Monthly subscription [fees](http://xmlgw.companieshouse.gov.uk/CHDpriceList.shtml), and other fees, may apply.)
|
14
|
-
|
15
|
-
Quick start:
|
16
|
-
* Register an account via the `Sign In / Register` link
|
17
|
-
[on the CompaniesHouse Developers website](https://developer.companieshouse.gov.uk/api/docs/)
|
18
|
-
* Register an API key at [Your Applications](https://developer.companieshouse.gov.uk/developer/applications)
|
19
|
-
* Put your API key in an environment variable (not in your code):
|
20
|
-
|
21
|
-
``` shell
|
22
|
-
export COMPANIES_HOUSE_API_KEY=YOUR_API_KEY_HERE
|
23
|
-
|
24
|
-
```
|
25
|
-
* Install `companies-house-rest` through [RubyGems](https://rubygems.org/gems/companies-house-rest)
|
26
|
-
* Create and use a client:
|
27
|
-
|
28
|
-
``` ruby
|
29
|
-
require 'companies_house/client'
|
30
|
-
client = CompaniesHouse::Client.new(api_key: ENV['COMPANIES_HOUSE_API_KEY'])
|
31
|
-
profile = client.company('07495895')
|
32
|
-
```
|
33
|
-
|
34
|
-
|
35
|
-
## Overview
|
36
|
-
This gem is meant to provide a simple synchronous API to look up company profile
|
37
|
-
information and company officers. The data returned is parsed JSON.
|
38
|
-
|
39
|
-
This gem provides information on companies by their Companies House company
|
40
|
-
number. This "company number" is actually a string and should be treated as such.
|
41
|
-
The string may consist solely of digits (including leading 0s) or begin with
|
42
|
-
alphabetic characters such as `NI` or `SC`.
|
43
|
-
|
44
|
-
## Authentication
|
45
|
-
|
46
|
-
Using the Companies House REST API requires you to register an account
|
47
|
-
[on the CompaniesHouse Developers website](https://developer.companieshouse.gov.uk/api/docs/)
|
48
|
-
and [configure an API key](https://developer.companieshouse.gov.uk/developer/applications).
|
49
|
-
Developers should read
|
50
|
-
[the Companies House developer guidelines](https://developer.companieshouse.gov.uk/api/docs/index/gettingStarted/developerGuidelines.html)
|
51
|
-
before using this API, and will note that these guidelines contain several
|
52
|
-
instructions regarding API keys:
|
53
|
-
|
54
|
-
* Do not embed API keys in your code
|
55
|
-
* Do not store API keys in your source tree
|
56
|
-
* Restrict API key use by IP address and domain
|
57
|
-
* **Regenerate your API keys regularly**
|
58
|
-
* Delete API keys when no longer required
|
59
|
-
|
60
|
-
## Client Initialization
|
61
|
-
|
62
|
-
All requests to the API are made through a client object:
|
63
|
-
|
64
|
-
```ruby
|
65
|
-
require 'companies_house/client'
|
66
|
-
client = CompaniesHouse::Client.new(config)
|
67
|
-
```
|
68
|
-
|
69
|
-
The client is configured by passing a hash to the constructor. The supported keys for this
|
70
|
-
hash are:
|
71
|
-
|
72
|
-
| Key | Description |
|
73
|
-
| ----------- | ----------- |
|
74
|
-
| `:api_key` | Required. The API key received after registration. |
|
75
|
-
| `:endpoint` | Optional. Specifies the base URI for the API (e.g. if using a self-hosted version) |
|
76
|
-
|
77
|
-
## Requests
|
78
|
-
|
79
|
-
Once a client has been initialised, requests can be made to the API.
|
80
|
-
Details of the available fields in the response are in the Companies House
|
81
|
-
[documentation](https://developer.companieshouse.gov.uk/api/docs/index.html).
|
82
|
-
The endpoints currently implemented by the gem are:
|
83
|
-
|
84
|
-
| Client Method
|
85
|
-
|
|
86
|
-
| `.company(company_number)`
|
87
|
-
| `.officers(company_number)`
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
resource.
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
resource
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
1
|
+
# CompaniesHouse::Client
|
2
|
+
|
3
|
+
[![CircleCI](https://circleci.com/gh/gocardless/companies-house-rest.svg?style=svg)](https://circleci.com/gh/gocardless/companies-house-rest)
|
4
|
+
|
5
|
+
This Gem implements an API client for the Companies House REST API. It can be
|
6
|
+
used to look up information about companies registered in the United Kingdom.
|
7
|
+
As of July 2016, this API is described by Companies House as a "beta service."
|
8
|
+
More information about this free API can be found
|
9
|
+
[on the Companies House API website](https://developer.companieshouse.gov.uk/api/docs/index.html).
|
10
|
+
|
11
|
+
To interact the older [CompaniesHouse XML-based API](http://xmlgw.companieshouse.gov.uk/),
|
12
|
+
see the gem [companies-house-gateway](https://github.com/gocardless/companies-house-gateway-ruby).
|
13
|
+
(Monthly subscription [fees](http://xmlgw.companieshouse.gov.uk/CHDpriceList.shtml), and other fees, may apply.)
|
14
|
+
|
15
|
+
Quick start:
|
16
|
+
* Register an account via the `Sign In / Register` link
|
17
|
+
[on the CompaniesHouse Developers website](https://developer.companieshouse.gov.uk/api/docs/)
|
18
|
+
* Register an API key at [Your Applications](https://developer.companieshouse.gov.uk/developer/applications)
|
19
|
+
* Put your API key in an environment variable (not in your code):
|
20
|
+
|
21
|
+
``` shell
|
22
|
+
export COMPANIES_HOUSE_API_KEY=YOUR_API_KEY_HERE
|
23
|
+
|
24
|
+
```
|
25
|
+
* Install `companies-house-rest` through [RubyGems](https://rubygems.org/gems/companies-house-rest)
|
26
|
+
* Create and use a client:
|
27
|
+
|
28
|
+
``` ruby
|
29
|
+
require 'companies_house/client'
|
30
|
+
client = CompaniesHouse::Client.new(api_key: ENV['COMPANIES_HOUSE_API_KEY'])
|
31
|
+
profile = client.company('07495895')
|
32
|
+
```
|
33
|
+
|
34
|
+
|
35
|
+
## Overview
|
36
|
+
This gem is meant to provide a simple synchronous API to look up company profile
|
37
|
+
information and company officers. The data returned is parsed JSON.
|
38
|
+
|
39
|
+
This gem provides information on companies by their Companies House company
|
40
|
+
number. This "company number" is actually a string and should be treated as such.
|
41
|
+
The string may consist solely of digits (including leading 0s) or begin with
|
42
|
+
alphabetic characters such as `NI` or `SC`.
|
43
|
+
|
44
|
+
## Authentication
|
45
|
+
|
46
|
+
Using the Companies House REST API requires you to register an account
|
47
|
+
[on the CompaniesHouse Developers website](https://developer.companieshouse.gov.uk/api/docs/)
|
48
|
+
and [configure an API key](https://developer.companieshouse.gov.uk/developer/applications).
|
49
|
+
Developers should read
|
50
|
+
[the Companies House developer guidelines](https://developer.companieshouse.gov.uk/api/docs/index/gettingStarted/developerGuidelines.html)
|
51
|
+
before using this API, and will note that these guidelines contain several
|
52
|
+
instructions regarding API keys:
|
53
|
+
|
54
|
+
* Do not embed API keys in your code
|
55
|
+
* Do not store API keys in your source tree
|
56
|
+
* Restrict API key use by IP address and domain
|
57
|
+
* **Regenerate your API keys regularly**
|
58
|
+
* Delete API keys when no longer required
|
59
|
+
|
60
|
+
## Client Initialization
|
61
|
+
|
62
|
+
All requests to the API are made through a client object:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
require 'companies_house/client'
|
66
|
+
client = CompaniesHouse::Client.new(config)
|
67
|
+
```
|
68
|
+
|
69
|
+
The client is configured by passing a hash to the constructor. The supported keys for this
|
70
|
+
hash are:
|
71
|
+
|
72
|
+
| Key | Description |
|
73
|
+
| ----------- | ----------- |
|
74
|
+
| `:api_key` | Required. The API key received after registration. |
|
75
|
+
| `:endpoint` | Optional. Specifies the base URI for the API (e.g. if using a self-hosted version) |
|
76
|
+
|
77
|
+
## Requests
|
78
|
+
|
79
|
+
Once a client has been initialised, requests can be made to the API.
|
80
|
+
Details of the available fields in the response are in the Companies House
|
81
|
+
[documentation](https://developer.companieshouse.gov.uk/api/docs/index.html).
|
82
|
+
The endpoints currently implemented by the gem are:
|
83
|
+
|
84
|
+
| Client Method | Endpoint | Description |
|
85
|
+
| --------------------------------------------------------------- | --------------------------------------- | ----------- |
|
86
|
+
| `.company(company_number)` | `GET /company/:company_number` | Retrieves a company profile. |
|
87
|
+
| `.officers(company_number)` | `GET /company/:company_number/officers` | Retrieves a list of company officers. |
|
88
|
+
| `.company_search(query, items_per_page: nil, start_index: nil)` | `GET /search/companies` | Retrieves a list of companies that match the given query. |
|
89
|
+
|
90
|
+
### .company
|
91
|
+
This method implements the [readCompanyProfile](https://developer.companieshouse.gov.uk/api/docs/company/company_number/readCompanyProfile.html)
|
92
|
+
API and returns the full [companyProfile](https://developer.companieshouse.gov.uk/api/docs/company/company_number/companyProfile-resource.html)
|
93
|
+
resource.
|
94
|
+
|
95
|
+
### .officers
|
96
|
+
This method implements the [officersList](https://developer.companieshouse.gov.uk/api/docs/company/company_number/officers/officerList.html)
|
97
|
+
API. It will make one or more requests against this API, as necessary, to obtain
|
98
|
+
the full list of company officers. It returns only the values under the `items`
|
99
|
+
key from the
|
100
|
+
[officerList](https://developer.companieshouse.gov.uk/api/docs/company/company_number/officers/officerList-resource.html)
|
101
|
+
resource(s) which it reads.
|
102
|
+
|
103
|
+
### .company_search
|
104
|
+
This method implements the [searchCompanies](https://developer.companieshouse.gov.uk/api/docs/search/companies/companysearch.html)
|
105
|
+
API and returns the list of [companySearch](https://developer.companieshouse.gov.uk/api/docs/search-overview/CompanySearch-resource.html)
|
106
|
+
resources that match the given query. The `items_per_page` and `start_index` parameters are optional.
|
107
|
+
|
108
|
+
### Other API Methods
|
109
|
+
While there are other resources exposed by the
|
110
|
+
[Companies House API](https://developer.companieshouse.gov.uk/api/docs/index.html),
|
111
|
+
this gem does not implement access to these resources at this time.
|
112
|
+
|
113
|
+
## Error Handling
|
114
|
+
If a request to the Companies House API encounters an HTTP status other than
|
115
|
+
`200 OK`, it will raise an instance of `CompaniesHouse::APIError` instead of
|
116
|
+
returning response data. The error will have the following fields:
|
117
|
+
|
118
|
+
| Field | Description |
|
119
|
+
| ---------- | ----------- |
|
120
|
+
| `response` | The Net::HTTP response object from the failed API call. |
|
121
|
+
| `status` | A string containing the response status code. |
|
122
|
+
|
123
|
+
|
124
|
+
Certain API responses will raise an instance of a more specific subclass of
|
125
|
+
`CompaniesHouse::APIError`:
|
126
|
+
|
127
|
+
| Status | Error | Description |
|
128
|
+
| ------ | ------------------------------------- | ----------- |
|
129
|
+
| 401 | `CompaniesHouse::AuthenticationError` | Authentication error (invalid API key) |
|
130
|
+
| 404 | `CompaniesHouse::NotFoundError` | Not Found. (No record of the company is available.) |
|
131
|
+
| 429 | `CompaniesHouse::RateLimitError` | Application is being [rate limited](https://developer.companieshouse.gov.uk/api/docs/index/gettingStarted/rateLimiting.html) |
|
132
|
+
|
133
|
+
The client will not catch any other errors which may occur, such as
|
134
|
+
errors involving network connections (e.g. `Errno::ECONNRESET`).
|
135
|
+
|
136
|
+
## Development
|
137
|
+
|
138
|
+
This gem is configured for development using a `bundler` workflow.
|
139
|
+
Tests are written using RSpec, and Rubocop is used to provide linting.
|
140
|
+
Bug reports and pull requests are welcome on this project's
|
141
|
+
[GitHub repository](https://github.com/gocardless/companies-house-rest).
|
142
|
+
|
143
|
+
To get started:
|
144
|
+
|
145
|
+
``` shell
|
146
|
+
bundle install --path vendor
|
147
|
+
```
|
148
|
+
|
149
|
+
|
150
|
+
To run all tests and Rubocop:
|
151
|
+
|
152
|
+
```shell
|
153
|
+
bundle exec rake
|
154
|
+
```
|
@@ -1,32 +1,34 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
spec.
|
9
|
-
spec.
|
10
|
-
spec.
|
11
|
-
spec.
|
12
|
-
|
13
|
-
|
14
|
-
spec.
|
15
|
-
spec.
|
16
|
-
|
17
|
-
spec.
|
18
|
-
|
19
|
-
spec.
|
20
|
-
spec.
|
21
|
-
spec.
|
22
|
-
|
23
|
-
spec.
|
24
|
-
|
25
|
-
|
26
|
-
spec.
|
27
|
-
|
28
|
-
spec.add_development_dependency "
|
29
|
-
spec.add_development_dependency "
|
30
|
-
spec.add_development_dependency "
|
31
|
-
spec.add_development_dependency
|
32
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "companies_house/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "companies-house-rest"
|
9
|
+
spec.version = CompaniesHouse::VERSION
|
10
|
+
spec.authors = ["GoCardless Engineering"]
|
11
|
+
spec.email = ["developers@gocardless.com"]
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.summary = "Look up UK company registration information"
|
15
|
+
spec.description = "Client for the Companies House REST API. Provides company " \
|
16
|
+
"profiles and officer lists."
|
17
|
+
spec.homepage = "https://github.com/gocardless/companies-house-rest"
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z lib/ *.gemspec LICENSE README.md`.split("\x0")
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
spec.required_ruby_version = ">= 2.3"
|
24
|
+
|
25
|
+
spec.add_runtime_dependency "activesupport", ">= 4.2"
|
26
|
+
spec.add_runtime_dependency "virtus", "~> 1.0", ">= 1.0.5"
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
29
|
+
spec.add_development_dependency "gc_ruboconfig", "~> 2.3.1"
|
30
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.5"
|
32
|
+
spec.add_development_dependency "timecop", "~> 0.8"
|
33
|
+
spec.add_development_dependency "webmock", "~> 3.0"
|
34
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "companies_house/client"
|
4
|
+
require "companies_house/api_error"
|
5
|
+
require "companies_house/not_found_error"
|
6
|
+
require "companies_house/authentication_error"
|
7
|
+
require "companies_house/rate_limit_error"
|
8
|
+
|
9
|
+
module CompaniesHouse
|
10
|
+
# The module that contains all the classes implementing the API client
|
11
|
+
end
|
@@ -1,18 +1,18 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module CompaniesHouse
|
4
|
-
# Represents any response from the API which is not a 200 OK
|
5
|
-
class APIError < StandardError
|
6
|
-
attr_reader :status, :response
|
7
|
-
|
8
|
-
def initialize(msg, response = nil)
|
9
|
-
if response
|
10
|
-
msg = "#{msg} - HTTP #{response.code}"
|
11
|
-
@status = response.code
|
12
|
-
end
|
13
|
-
|
14
|
-
super(msg)
|
15
|
-
@response = response
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CompaniesHouse
|
4
|
+
# Represents any response from the API which is not a 200 OK
|
5
|
+
class APIError < StandardError
|
6
|
+
attr_reader :status, :response
|
7
|
+
|
8
|
+
def initialize(msg, response = nil)
|
9
|
+
if response
|
10
|
+
msg = "#{msg} - HTTP #{response.code}"
|
11
|
+
@status = response.code
|
12
|
+
end
|
13
|
+
|
14
|
+
super(msg)
|
15
|
+
@response = response
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module CompaniesHouse
|
4
|
-
# Specific error class for when an invalid API key is used to access the service
|
5
|
-
class AuthenticationError < APIError
|
6
|
-
def initialize(response = nil)
|
7
|
-
super("Invalid API key", response)
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CompaniesHouse
|
4
|
+
# Specific error class for when an invalid API key is used to access the service
|
5
|
+
class AuthenticationError < APIError
|
6
|
+
def initialize(response = nil)
|
7
|
+
super("Invalid API key", response)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -1,82 +1,90 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
#
|
8
|
-
#
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
@
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
#
|
31
|
-
#
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "companies_house/request"
|
4
|
+
require "net/http"
|
5
|
+
|
6
|
+
module CompaniesHouse
|
7
|
+
# This class provides an interface to the Companies House API
|
8
|
+
# at https://api.companieshouse.gov.uk.
|
9
|
+
# Specifically, it manages the connections and arranges requests.
|
10
|
+
class Client
|
11
|
+
ENDPOINT = "https://api.companieshouse.gov.uk"
|
12
|
+
|
13
|
+
attr_reader :api_key, :endpoint
|
14
|
+
|
15
|
+
def initialize(config)
|
16
|
+
raise ArgumentError, "Missing API key" unless config[:api_key]
|
17
|
+
@api_key = config[:api_key]
|
18
|
+
@endpoint = URI(config[:endpoint] || ENDPOINT)
|
19
|
+
raise ArgumentError, "HTTP is not supported" if @endpoint.scheme != "https"
|
20
|
+
end
|
21
|
+
|
22
|
+
def end_connection
|
23
|
+
@connection.finish if @connection&.started?
|
24
|
+
end
|
25
|
+
|
26
|
+
def company(id)
|
27
|
+
request(:company, "company/#{id}", {}, make_transaction_id, id)
|
28
|
+
end
|
29
|
+
|
30
|
+
# The API endpoint for company officers is paginated, and not all of the officers may
|
31
|
+
# be returned in the first request. We deal with this by collating all the pages of
|
32
|
+
# results into one result set before returning them.
|
33
|
+
def officers(id)
|
34
|
+
items = []
|
35
|
+
offset = 0
|
36
|
+
xid = make_transaction_id
|
37
|
+
|
38
|
+
loop do
|
39
|
+
page = request(:officers, "company/#{id}/officers",
|
40
|
+
{ start_index: offset }, xid, id)
|
41
|
+
new_items = page["items"]
|
42
|
+
total = page["total_results"] || new_items.count
|
43
|
+
|
44
|
+
items += new_items
|
45
|
+
offset += new_items.count
|
46
|
+
|
47
|
+
break if items.count >= total
|
48
|
+
end
|
49
|
+
|
50
|
+
items
|
51
|
+
end
|
52
|
+
|
53
|
+
def company_search(query, items_per_page: nil, start_index: nil)
|
54
|
+
request(
|
55
|
+
:company_search,
|
56
|
+
"search/companies",
|
57
|
+
{ q: query, items_per_page: items_per_page, start_index: start_index }.compact,
|
58
|
+
)
|
59
|
+
end
|
60
|
+
|
61
|
+
def connection
|
62
|
+
@connection ||= Net::HTTP.new(endpoint.host, endpoint.port).tap do |conn|
|
63
|
+
conn.use_ssl = true
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def make_transaction_id
|
70
|
+
SecureRandom.hex(10)
|
71
|
+
end
|
72
|
+
|
73
|
+
def request(resource,
|
74
|
+
path,
|
75
|
+
params = {},
|
76
|
+
transaction_id = make_transaction_id,
|
77
|
+
resource_id = nil)
|
78
|
+
Request.new(
|
79
|
+
connection: connection,
|
80
|
+
api_key: @api_key,
|
81
|
+
endpoint: @endpoint,
|
82
|
+
path: path,
|
83
|
+
query: params,
|
84
|
+
resource_type: resource,
|
85
|
+
resource_id: resource_id,
|
86
|
+
transaction_id: transaction_id,
|
87
|
+
).execute
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -1,11 +1,14 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module CompaniesHouse
|
4
|
-
# Specific error class for when a company cannot be found (for example, if the company
|
5
|
-
# number given is invaid)
|
6
|
-
class NotFoundError < APIError
|
7
|
-
def initialize(
|
8
|
-
super(
|
9
|
-
|
10
|
-
|
11
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CompaniesHouse
|
4
|
+
# Specific error class for when a company cannot be found (for example, if the company
|
5
|
+
# number given is invaid)
|
6
|
+
class NotFoundError < APIError
|
7
|
+
def initialize(resource_type, resource_id = nil, response = nil)
|
8
|
+
super(
|
9
|
+
"Resource not found - type `#{resource_type}`, id `#{resource_id || 'nil'}`",
|
10
|
+
response,
|
11
|
+
)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module CompaniesHouse
|
4
|
-
# Specific error class for when an invalid API key is used to access the service
|
5
|
-
class RateLimitError < APIError
|
6
|
-
def initialize(response = nil)
|
7
|
-
super("Rate limit exceeded", response)
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CompaniesHouse
|
4
|
+
# Specific error class for when an invalid API key is used to access the service
|
5
|
+
class RateLimitError < APIError
|
6
|
+
def initialize(response = nil)
|
7
|
+
super("Rate limit exceeded", response)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -1,94 +1,93 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
|
7
|
-
|
8
|
-
require
|
9
|
-
require
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
#
|
16
|
-
#
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
attribute :
|
22
|
-
attribute :
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
attribute :
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
attribute :
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
@uri
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
req.
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "companies_house/api_error"
|
4
|
+
require "companies_house/not_found_error"
|
5
|
+
require "companies_house/authentication_error"
|
6
|
+
require "companies_house/rate_limit_error"
|
7
|
+
|
8
|
+
require "virtus"
|
9
|
+
require "uri"
|
10
|
+
require "json"
|
11
|
+
|
12
|
+
require "active_support/notifications"
|
13
|
+
|
14
|
+
module CompaniesHouse
|
15
|
+
# This class manages individual requests.
|
16
|
+
# Users of the CompaniesHouse gem should not instantiate this class
|
17
|
+
# and should instead use CompaniesHouse::Client.
|
18
|
+
class Request
|
19
|
+
include Virtus.model
|
20
|
+
# API-level attributes
|
21
|
+
attribute :connection, Net::HTTP, required: true
|
22
|
+
attribute :api_key, String, required: true
|
23
|
+
attribute :endpoint, URI, required: true
|
24
|
+
|
25
|
+
# Physical request attributes
|
26
|
+
attribute :path, String, required: true
|
27
|
+
attribute :query, Hash, required: true
|
28
|
+
|
29
|
+
# Logical request attributes
|
30
|
+
attribute :resource_type, Symbol, required: true
|
31
|
+
attribute :resource_id, String
|
32
|
+
|
33
|
+
attribute :transaction_id, String, required: true
|
34
|
+
|
35
|
+
def initialize(args)
|
36
|
+
super(args)
|
37
|
+
|
38
|
+
@uri = URI.join(endpoint, path)
|
39
|
+
@uri.query = URI.encode_www_form(query)
|
40
|
+
|
41
|
+
@notification_payload = {
|
42
|
+
method: :get,
|
43
|
+
path: path,
|
44
|
+
query: query,
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
def execute
|
49
|
+
@started = Time.now.utc
|
50
|
+
|
51
|
+
req = Net::HTTP::Get.new(@uri)
|
52
|
+
req.basic_auth @api_key, ""
|
53
|
+
|
54
|
+
response = connection.request req
|
55
|
+
@notification_payload[:status] = response.code
|
56
|
+
|
57
|
+
begin
|
58
|
+
@notification_payload[:response] = parse(response, resource_type, resource_id)
|
59
|
+
rescue StandardError => e
|
60
|
+
@notification_payload[:error] = e
|
61
|
+
raise e
|
62
|
+
ensure
|
63
|
+
publish_notification
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def publish_notification
|
70
|
+
ActiveSupport::Notifications.publish(
|
71
|
+
"companies_house.#{resource_type}",
|
72
|
+
@started, Time.now.utc,
|
73
|
+
transaction_id,
|
74
|
+
@notification_payload
|
75
|
+
)
|
76
|
+
end
|
77
|
+
|
78
|
+
def parse(response, resource_type, resource_id)
|
79
|
+
case response.code
|
80
|
+
when "200"
|
81
|
+
JSON[response.body]
|
82
|
+
when "401"
|
83
|
+
raise CompaniesHouse::AuthenticationError, response
|
84
|
+
when "404"
|
85
|
+
raise CompaniesHouse::NotFoundError.new(resource_type, resource_id, response)
|
86
|
+
when "429"
|
87
|
+
raise CompaniesHouse::RateLimitError, response
|
88
|
+
else
|
89
|
+
raise CompaniesHouse::APIError.new("Unknown API response", response)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module CompaniesHouse
|
4
|
-
VERSION =
|
5
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CompaniesHouse
|
4
|
+
VERSION = "0.3.0"
|
5
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: companies-house-rest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GoCardless Engineering
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -59,75 +59,75 @@ dependencies:
|
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '1.10'
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
|
-
name:
|
62
|
+
name: gc_ruboconfig
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
64
64
|
requirements:
|
65
65
|
- - "~>"
|
66
66
|
- !ruby/object:Gem::Version
|
67
|
-
version:
|
67
|
+
version: 2.3.1
|
68
68
|
type: :development
|
69
69
|
prerelease: false
|
70
70
|
version_requirements: !ruby/object:Gem::Requirement
|
71
71
|
requirements:
|
72
72
|
- - "~>"
|
73
73
|
- !ruby/object:Gem::Version
|
74
|
-
version:
|
74
|
+
version: 2.3.1
|
75
75
|
- !ruby/object:Gem::Dependency
|
76
|
-
name:
|
76
|
+
name: rake
|
77
77
|
requirement: !ruby/object:Gem::Requirement
|
78
78
|
requirements:
|
79
79
|
- - "~>"
|
80
80
|
- !ruby/object:Gem::Version
|
81
|
-
version: '
|
81
|
+
version: '12.0'
|
82
82
|
type: :development
|
83
83
|
prerelease: false
|
84
84
|
version_requirements: !ruby/object:Gem::Requirement
|
85
85
|
requirements:
|
86
86
|
- - "~>"
|
87
87
|
- !ruby/object:Gem::Version
|
88
|
-
version: '
|
88
|
+
version: '12.0'
|
89
89
|
- !ruby/object:Gem::Dependency
|
90
|
-
name:
|
90
|
+
name: rspec
|
91
91
|
requirement: !ruby/object:Gem::Requirement
|
92
92
|
requirements:
|
93
93
|
- - "~>"
|
94
94
|
- !ruby/object:Gem::Version
|
95
|
-
version:
|
95
|
+
version: '3.5'
|
96
96
|
type: :development
|
97
97
|
prerelease: false
|
98
98
|
version_requirements: !ruby/object:Gem::Requirement
|
99
99
|
requirements:
|
100
100
|
- - "~>"
|
101
101
|
- !ruby/object:Gem::Version
|
102
|
-
version:
|
102
|
+
version: '3.5'
|
103
103
|
- !ruby/object:Gem::Dependency
|
104
|
-
name:
|
104
|
+
name: timecop
|
105
105
|
requirement: !ruby/object:Gem::Requirement
|
106
106
|
requirements:
|
107
107
|
- - "~>"
|
108
108
|
- !ruby/object:Gem::Version
|
109
|
-
version: '
|
109
|
+
version: '0.8'
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
112
|
version_requirements: !ruby/object:Gem::Requirement
|
113
113
|
requirements:
|
114
114
|
- - "~>"
|
115
115
|
- !ruby/object:Gem::Version
|
116
|
-
version: '
|
116
|
+
version: '0.8'
|
117
117
|
- !ruby/object:Gem::Dependency
|
118
|
-
name:
|
118
|
+
name: webmock
|
119
119
|
requirement: !ruby/object:Gem::Requirement
|
120
120
|
requirements:
|
121
121
|
- - "~>"
|
122
122
|
- !ruby/object:Gem::Version
|
123
|
-
version: '0
|
123
|
+
version: '3.0'
|
124
124
|
type: :development
|
125
125
|
prerelease: false
|
126
126
|
version_requirements: !ruby/object:Gem::Requirement
|
127
127
|
requirements:
|
128
128
|
- - "~>"
|
129
129
|
- !ruby/object:Gem::Version
|
130
|
-
version: '0
|
130
|
+
version: '3.0'
|
131
131
|
description: Client for the Companies House REST API. Provides company profiles and
|
132
132
|
officer lists.
|
133
133
|
email:
|
@@ -139,7 +139,7 @@ files:
|
|
139
139
|
- LICENSE
|
140
140
|
- README.md
|
141
141
|
- companies-house-rest.gemspec
|
142
|
-
- lib/
|
142
|
+
- lib/companies_house.rb
|
143
143
|
- lib/companies_house/api_error.rb
|
144
144
|
- lib/companies_house/authentication_error.rb
|
145
145
|
- lib/companies_house/client.rb
|
@@ -159,7 +159,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
159
159
|
requirements:
|
160
160
|
- - ">="
|
161
161
|
- !ruby/object:Gem::Version
|
162
|
-
version: '2.
|
162
|
+
version: '2.3'
|
163
163
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
164
164
|
requirements:
|
165
165
|
- - ">="
|
@@ -167,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
167
|
version: '0'
|
168
168
|
requirements: []
|
169
169
|
rubyforge_project:
|
170
|
-
rubygems_version: 2.
|
170
|
+
rubygems_version: 2.6.13
|
171
171
|
signing_key:
|
172
172
|
specification_version: 4
|
173
173
|
summary: Look up UK company registration information
|
data/lib/companies-house.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'companies_house/client'
|
4
|
-
require 'companies_house/api_error'
|
5
|
-
require 'companies_house/not_found_error'
|
6
|
-
require 'companies_house/authentication_error'
|
7
|
-
require 'companies_house/rate_limit_error'
|
8
|
-
|
9
|
-
module CompaniesHouse
|
10
|
-
# The module that contains all the classes implementing the API client
|
11
|
-
end
|