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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c2f6125bb88bcc461c70066070d82ce8916a7b78
4
- data.tar.gz: bc3b40e5dfcbb11c44d234fba11ff0323275d620
3
+ metadata.gz: 341246c80b5b92f5e8b715d582b4006b561c2477
4
+ data.tar.gz: 5036e6f1eb1c26203792d203f41ecdbde535be5d
5
5
  SHA512:
6
- metadata.gz: 57a229e5492865f01f6c688c524379a4db4932e95fb04a7d0bab86f8c64dc73b9d4a1d4fd3bfe186e12487438cfba92fb0ad2a7a3346d0c1fd5567c8cbc02f45
7
- data.tar.gz: 7277ccc6c22a96133f8c48975b9724e26ae610c3a97e79430a4335bed2772183e73f6c25a01ccc8974dcbdc127f649010837178393297f4ba45872d990a427e8
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 | 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
-
89
- ### .company
90
- This method implements the [readCompanyProfile](https://developer.companieshouse.gov.uk/api/docs/company/company_number/readCompanyProfile.html)
91
- API and returns the full [companyProfile](https://developer.companieshouse.gov.uk/api/docs/company/company_number/companyProfile-resource.html)
92
- resource.
93
-
94
- ### .officers
95
- This method implements the [officersList](https://developer.companieshouse.gov.uk/api/docs/company/company_number/officers/officerList.html)
96
- API. It will make one or more requests against this API, as necessary, to obtain
97
- the full list of company officers. It returns only the values under the `items`
98
- key from the
99
- [officerList](https://developer.companieshouse.gov.uk/api/docs/company/company_number/officers/officerList-resource.html)
100
- resource(s) which it reads.
101
-
102
- ### Other API Methods
103
- While there are other resources exposed by the
104
- [Companies House API](https://developer.companieshouse.gov.uk/api/docs/index.html),
105
- this gem does not implement access to these resources at this time.
106
-
107
- ## Error Handling
108
- If a request to the Companies House API encounters an HTTP status other than
109
- `200 OK`, it will raise an instance of `CompaniesHouse::APIError` instead of
110
- returning response data. The error will have the following fields:
111
-
112
- | Field | Description |
113
- | ---------- | ----------- |
114
- | `response` | The Net::HTTP response object from the failed API call. |
115
- | `status` | A string containing the response status code. |
116
-
117
-
118
- Certain API responses will raise an instance of a more specific subclass of
119
- `CompaniesHouse::APIError`:
120
-
121
- | Status | Error | Description |
122
- | ------ | ------------------------------------- | ----------- |
123
- | 401 | `CompaniesHouse::AuthenticationError` | Authentication error (invalid API key) |
124
- | 404 | `CompaniesHouse::NotFoundError` | Not Found. (No record of the company is available.) |
125
- | 429 | `CompaniesHouse::RateLimitError` | Application is being [rate limited](https://developer.companieshouse.gov.uk/api/docs/index/gettingStarted/rateLimiting.html) |
126
-
127
- The client will not catch any other errors which may occur, such as
128
- errors involving network connections (e.g. `Errno::ECONNRESET`).
129
-
130
- ## Development
131
-
132
- This gem is configured for development using a `bundler` workflow.
133
- Tests are written using RSpec, and Rubocop is used to provide linting.
134
- Bug reports and pull requests are welcome on this project's
135
- [GitHub repository](https://github.com/gocardless/companies-house-rest).
136
-
137
- To get started:
138
-
139
- ``` shell
140
- bundle install --path vendor
141
- ```
142
-
143
-
144
- To run all tests and Rubocop:
145
-
146
- ```shell
147
- bundle exec rake
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
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'companies_house/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "companies-house-rest"
8
- spec.version = CompaniesHouse::VERSION
9
- spec.authors = ["GoCardless Engineering"]
10
- spec.email = ["developers@gocardless.com"]
11
- spec.license = "MIT"
12
-
13
- spec.summary = %q{Look up UK company registration information}
14
- spec.description = %q{Client for the Companies House REST API. Provides company profiles and officer lists.}
15
- spec.homepage = "https://github.com/gocardless/companies-house-rest"
16
-
17
- spec.files = `git ls-files -z lib/ *.gemspec LICENSE README.md`.split("\x0")
18
- spec.bindir = "exe"
19
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
21
- spec.required_ruby_version = '>= 2.2'
22
-
23
- spec.add_runtime_dependency "activesupport", ">= 4.2"
24
- spec.add_runtime_dependency "virtus", '~> 1.0', '>= 1.0.5'
25
-
26
- spec.add_development_dependency "bundler", "~> 1.10"
27
- spec.add_development_dependency "rake", "~> 10.0"
28
- spec.add_development_dependency "rspec", "~> 3.5"
29
- spec.add_development_dependency "rubocop", "~> 0.41.2"
30
- spec.add_development_dependency "webmock", "~> 2.1"
31
- spec.add_development_dependency 'timecop', '~> 0.8'
32
- 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
@@ -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
- require 'companies_house/request'
3
- require 'net/http'
4
-
5
- module CompaniesHouse
6
- # This class provides an interface to the Companies House API
7
- # at https://api.companieshouse.gov.uk
8
- # Specifically, it manages the connections and arranges requests.
9
- class Client
10
- ENDPOINT = 'https://api.companieshouse.gov.uk'
11
-
12
- attr_reader :api_key, :endpoint
13
-
14
- def initialize(config)
15
- raise ArgumentError, 'Missing API key' unless config[:api_key]
16
- @api_key = config[:api_key]
17
- @endpoint = URI(config[:endpoint] || ENDPOINT)
18
- raise ArgumentError, 'HTTP is not supported' if @endpoint.scheme != 'https'
19
- end
20
-
21
- def end_connection
22
- @connection.finish if @connection && @connection.started?
23
- end
24
-
25
- def company(id)
26
- request(:company, id)
27
- end
28
-
29
- # The API endpoint for company officers is paginated, and not all of the officers may
30
- # be returned in the first request. We deal with this by collating all the pages of
31
- # results into one result set before returning them.
32
- def officers(id)
33
- items = []
34
- offset = 0
35
- xid = make_transaction_id
36
-
37
- loop do
38
- page = request(:officers, id, '/officers', { start_index: offset }, xid)
39
- new_items = page['items']
40
- total = page['total_results'] || new_items.count
41
-
42
- items += new_items
43
- offset += new_items.count
44
-
45
- break if items.count >= total
46
- end
47
-
48
- items
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
- def make_transaction_id
60
- SecureRandom.hex(10)
61
- end
62
-
63
- def request(resource,
64
- company_id,
65
- extra_path = '',
66
- params = {},
67
- transaction_id = make_transaction_id)
68
- Request.new(
69
- connection: connection,
70
- api_key: @api_key,
71
- endpoint: @endpoint,
72
-
73
- path: "company/#{company_id}#{extra_path}",
74
- query: params,
75
-
76
- resource_type: resource,
77
- company_id: company_id,
78
- transaction_id: transaction_id
79
- ).execute
80
- end
81
- end
82
- 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
+ # 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(company_id = nil, response = nil)
8
- super("Company #{company_id || 'nil'} not found", response)
9
- end
10
- end
11
- 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,94 +1,93 @@
1
- # frozen_string_literal: true
2
- require 'companies_house/api_error'
3
- require 'companies_house/not_found_error'
4
- require 'companies_house/authentication_error'
5
- require 'companies_house/rate_limit_error'
6
-
7
- require 'virtus'
8
- require 'uri'
9
- require 'json'
10
-
11
- require 'active_support/notifications'
12
-
13
- module CompaniesHouse
14
- # This class manages individual requests.
15
- # Users of the CompaniesHouse gem should not instantiate this class
16
- # and should instead use CompaniesHouse::Client.
17
- class Request
18
- include Virtus.model
19
- # API-level attributes
20
- attribute :connection, Net::HTTP, required: true
21
- attribute :api_key, String, required: true
22
- attribute :endpoint, URI, required: true
23
-
24
- # Physical request attributes
25
- attribute :path, String, required: true
26
- attribute :query, Hash, required: true
27
-
28
- # Logical request attributes
29
- attribute :resource_type, Symbol, required: true
30
- attribute :company_id, String, required: true
31
-
32
- attribute :transaction_id, String, required: true
33
-
34
- def initialize(args)
35
- super(args)
36
-
37
- @uri = URI.join(endpoint, path)
38
- @uri.query = URI.encode_www_form(query)
39
-
40
- @notification_payload = {
41
- method: :get,
42
- path: path,
43
- query: query
44
- }
45
- end
46
-
47
- def execute
48
- @started = Time.now.utc
49
-
50
- req = Net::HTTP::Get.new(@uri)
51
- req.basic_auth @api_key, ''
52
-
53
- response = connection.request req
54
- @notification_payload[:status] = response.code
55
-
56
- begin
57
- response_body = @notification_payload[:response] = parse(response, company_id)
58
- rescue => e
59
- @notification_payload[:error] = e
60
- raise e
61
- ensure
62
- publish_notification
63
- end
64
-
65
- response_body
66
- end
67
-
68
- private
69
-
70
- def publish_notification
71
- ActiveSupport::Notifications.publish(
72
- "companies_house.#{resource_type}",
73
- @started, Time.now.utc,
74
- transaction_id,
75
- @notification_payload
76
- )
77
- end
78
-
79
- def parse(response, company_id)
80
- case response.code
81
- when '200'
82
- return JSON[response.body]
83
- when '401'
84
- raise CompaniesHouse::AuthenticationError, response
85
- when '404'
86
- raise CompaniesHouse::NotFoundError.new(company_id, response)
87
- when '429'
88
- raise CompaniesHouse::RateLimitError, response
89
- else
90
- raise CompaniesHouse::APIError.new("Unknown API response", response)
91
- end
92
- end
93
- end
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 = '0.2.1'
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.2.1
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: 2016-08-08 00:00:00.000000000 Z
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: rake
62
+ name: gc_ruboconfig
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: '10.0'
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: '10.0'
74
+ version: 2.3.1
75
75
  - !ruby/object:Gem::Dependency
76
- name: rspec
76
+ name: rake
77
77
  requirement: !ruby/object:Gem::Requirement
78
78
  requirements:
79
79
  - - "~>"
80
80
  - !ruby/object:Gem::Version
81
- version: '3.5'
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: '3.5'
88
+ version: '12.0'
89
89
  - !ruby/object:Gem::Dependency
90
- name: rubocop
90
+ name: rspec
91
91
  requirement: !ruby/object:Gem::Requirement
92
92
  requirements:
93
93
  - - "~>"
94
94
  - !ruby/object:Gem::Version
95
- version: 0.41.2
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: 0.41.2
102
+ version: '3.5'
103
103
  - !ruby/object:Gem::Dependency
104
- name: webmock
104
+ name: timecop
105
105
  requirement: !ruby/object:Gem::Requirement
106
106
  requirements:
107
107
  - - "~>"
108
108
  - !ruby/object:Gem::Version
109
- version: '2.1'
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: '2.1'
116
+ version: '0.8'
117
117
  - !ruby/object:Gem::Dependency
118
- name: timecop
118
+ name: webmock
119
119
  requirement: !ruby/object:Gem::Requirement
120
120
  requirements:
121
121
  - - "~>"
122
122
  - !ruby/object:Gem::Version
123
- version: '0.8'
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.8'
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/companies-house.rb
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.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.5.1
170
+ rubygems_version: 2.6.13
171
171
  signing_key:
172
172
  specification_version: 4
173
173
  summary: Look up UK company registration information
@@ -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