companies-house-rest 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 341246c80b5b92f5e8b715d582b4006b561c2477
4
- data.tar.gz: 5036e6f1eb1c26203792d203f41ecdbde535be5d
3
+ metadata.gz: 16492c4a1e345ed9aa639feec2f5643cee73bb88
4
+ data.tar.gz: 5e4eed3091473b4bfa5ef4809b00fe8f04b6e8b5
5
5
  SHA512:
6
- metadata.gz: 66d5ff4591518013685b9ab7923c8ca70f831fa1e2777bf420ccc3d9bc5e5a6484e795591c11614feef46f567da51a7db39e11f3f0b2ed28088f491ba0e7eed6
7
- data.tar.gz: 80c0979bf45534fa8e46bc06d3961528ca5615a26b4ab744f03b665f51502620718c79b2715c120f26248ea27fd93b424e47cf5b8f23d6969d58d06e70cdaab2
6
+ metadata.gz: d9d95e1da9d81f11ac54a41326d326fd81cf97ddf1c9f3260bebda803a9d4207efb784495261cb9b1043cfd44422c85455ded0bb9bedf8f0bb704a2598879c31
7
+ data.tar.gz: 8e7b0791cfce1a2880a1f0fbcaadd779884f2a1c529b163a1bebf0deddd4c2dfcb7be2ab508105143a1805fff94c49462f53ba620ad251505adcc60a2f483a78
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,154 +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 | 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
+ # 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,34 +1,34 @@
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
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
@@ -1,11 +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
+ # 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,90 +1,100 @@
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
+ # 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
+ def officers(id)
31
+ get_all_pages(:officers, "company/#{id}/officers", id)
32
+ end
33
+
34
+ def persons_with_significant_control(id)
35
+ get_all_pages(
36
+ :persons_with_significant_control,
37
+ "company/#{id}/persons-with-significant-control",
38
+ id,
39
+ register_view: true,
40
+ )
41
+ end
42
+
43
+ def company_search(query, items_per_page: nil, start_index: nil)
44
+ request(
45
+ :company_search,
46
+ "search/companies",
47
+ { q: query, items_per_page: items_per_page, start_index: start_index }.compact,
48
+ )
49
+ end
50
+
51
+ def connection
52
+ @connection ||= Net::HTTP.new(endpoint.host, endpoint.port).tap do |conn|
53
+ conn.use_ssl = true
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ # Fetch and combine all pages of a paginated API call
60
+ def get_all_pages(resource, path, id, query = {})
61
+ items = []
62
+ offset = 0
63
+ xid = make_transaction_id
64
+
65
+ loop do
66
+ page = request(resource, path, query.merge(start_index: offset), xid, id)
67
+ new_items = page["items"]
68
+ total = page["total_results"] || new_items.count
69
+
70
+ items += new_items
71
+ offset += new_items.count
72
+
73
+ break if items.count >= total
74
+ end
75
+
76
+ items
77
+ end
78
+
79
+ def make_transaction_id
80
+ SecureRandom.hex(10)
81
+ end
82
+
83
+ def request(resource,
84
+ path,
85
+ params = {},
86
+ transaction_id = make_transaction_id,
87
+ resource_id = nil)
88
+ Request.new(
89
+ connection: connection,
90
+ api_key: @api_key,
91
+ endpoint: @endpoint,
92
+ path: path,
93
+ query: params,
94
+ resource_type: resource,
95
+ resource_id: resource_id,
96
+ transaction_id: transaction_id,
97
+ ).execute
98
+ end
99
+ end
100
+ end
@@ -1,14 +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(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
+ # 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,93 +1,93 @@
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
+ # 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 = "0.3.0"
5
- end
1
+ # frozen_string_literal: true
2
+
3
+ module CompaniesHouse
4
+ VERSION = "0.4.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.3.0
4
+ version: 0.4.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: 2018-02-14 00:00:00.000000000 Z
11
+ date: 2018-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport