companies-house-rest 0.1.3 → 0.2.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: 5e65c6b0dc6a852028c762b9321fda74d1b1ad97
4
- data.tar.gz: e07022f3a98a5e906975d0f300158345a3163570
3
+ metadata.gz: 67fd9432223ef1ff69e1f6bbe6c67a4687725948
4
+ data.tar.gz: ce5123c5283a4c6c0bdb54e72a6dd2ac8526c1a2
5
5
  SHA512:
6
- metadata.gz: 8f3901911201a184ded7db0e560135a281e3befe4cca48f4bccbc08d8842eec0200bade212524c98eb6f876e8eb82a02db18038d94a94a096537c6503d23e4cf
7
- data.tar.gz: ed781a6ac56a719dc667ea3e3c22c686cb2ef2019c5259e714cc1aa5a482d46c7f5ac580b940b1ab8acbfd90b7913074aedb2a3a9ae9c78c30eb3be85e93d803
6
+ metadata.gz: 5581a2599700c5c798d64097f1d5331149e78507165dfaeab0f0c1c07721cc01c99a772cac7a9749d8b8a661e7216a8f4d81927bfc5f0753ead28e5a90ea3d24
7
+ data.tar.gz: 85659d09bd029eb39850ecd748cfc6028be4dde8589d6873dd4182c4911747dde3ef91d73aff88663ce97676ef644ce790d1c7b39da4d821305fecd3d3f59580
@@ -18,11 +18,15 @@ Gem::Specification.new do |spec|
18
18
  spec.bindir = "exe"
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ["lib"]
21
- spec.required_ruby_version = '~> 2.0'
21
+ spec.required_ruby_version = '~> 2.2.2'
22
+
23
+ spec.add_runtime_dependency "activesupport", ">= 4.2"
24
+ spec.add_runtime_dependency "virtus", '~> 1.0', '>= 1.0.5'
22
25
 
23
26
  spec.add_development_dependency "bundler", "~> 1.10"
24
27
  spec.add_development_dependency "rake", "~> 10.0"
25
- spec.add_development_dependency "rspec", "~> 3.5.0"
28
+ spec.add_development_dependency "rspec", "~> 3.5"
26
29
  spec.add_development_dependency "rubocop", "~> 0.41.2"
27
- spec.add_development_dependency "webmock", "~> 2.1.0"
30
+ spec.add_development_dependency "webmock", "~> 2.1"
31
+ spec.add_development_dependency 'timecop', '~> 0.8'
28
32
  end
@@ -1,14 +1,11 @@
1
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'
2
+ require 'companies_house/request'
6
3
  require 'net/http'
7
- require 'json'
8
4
 
9
5
  module CompaniesHouse
10
- # This class connects to the Companies House API
6
+ # This class provides an interface to the Companies House API
11
7
  # at https://api.companieshouse.gov.uk
8
+ # Specifically, it manages the connections and arranges requests.
12
9
  class Client
13
10
  ENDPOINT = 'https://api.companieshouse.gov.uk'
14
11
 
@@ -26,7 +23,7 @@ module CompaniesHouse
26
23
  end
27
24
 
28
25
  def company(id)
29
- request(id)
26
+ request(:company, id)
30
27
  end
31
28
 
32
29
  # The API endpoint for company officers is paginated, and not all of the officers may
@@ -35,9 +32,10 @@ module CompaniesHouse
35
32
  def officers(id)
36
33
  items = []
37
34
  offset = 0
35
+ xid = make_transaction_id
38
36
 
39
37
  loop do
40
- page = request(id, '/officers', start_index: offset)
38
+ page = request(:officers, id, '/officers', { start_index: offset }, xid)
41
39
  new_items = page['items']
42
40
  total = page['total_results'] || new_items.count
43
41
 
@@ -58,30 +56,27 @@ module CompaniesHouse
58
56
 
59
57
  private
60
58
 
61
- def request(company_id, extra_path = '', params = {})
62
- uri = URI.join(endpoint, 'company/', "#{company_id}#{extra_path}")
63
- uri.query = URI.encode_www_form(params)
64
-
65
- req = Net::HTTP::Get.new(uri)
66
- req.basic_auth api_key, ''
67
-
68
- response = connection.request req
69
- parse(response, company_id)
59
+ def make_transaction_id
60
+ SecureRandom.hex(10)
70
61
  end
71
62
 
72
- def parse(response, company_id)
73
- case response.code
74
- when '200'
75
- return JSON[response.body]
76
- when '401'
77
- raise CompaniesHouse::AuthenticationError, response
78
- when '404'
79
- raise CompaniesHouse::NotFoundError.new(company_id, response)
80
- when '429'
81
- raise CompaniesHouse::RateLimitError, response
82
- else
83
- raise CompaniesHouse::APIError.new("Unknown API response", response)
84
- end
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
85
80
  end
86
81
  end
87
82
  end
@@ -0,0 +1,94 @@
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,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CompaniesHouse
4
- VERSION = '0.1.3'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,85 +1,133 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: companies-house-rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.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-03 00:00:00.000000000 Z
11
+ date: 2016-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: virtus
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 1.0.5
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '1.0'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.0.5
13
47
  - !ruby/object:Gem::Dependency
14
48
  name: bundler
15
49
  requirement: !ruby/object:Gem::Requirement
16
50
  requirements:
17
- - - ~>
51
+ - - "~>"
18
52
  - !ruby/object:Gem::Version
19
53
  version: '1.10'
20
54
  type: :development
21
55
  prerelease: false
22
56
  version_requirements: !ruby/object:Gem::Requirement
23
57
  requirements:
24
- - - ~>
58
+ - - "~>"
25
59
  - !ruby/object:Gem::Version
26
60
  version: '1.10'
27
61
  - !ruby/object:Gem::Dependency
28
62
  name: rake
29
63
  requirement: !ruby/object:Gem::Requirement
30
64
  requirements:
31
- - - ~>
65
+ - - "~>"
32
66
  - !ruby/object:Gem::Version
33
67
  version: '10.0'
34
68
  type: :development
35
69
  prerelease: false
36
70
  version_requirements: !ruby/object:Gem::Requirement
37
71
  requirements:
38
- - - ~>
72
+ - - "~>"
39
73
  - !ruby/object:Gem::Version
40
74
  version: '10.0'
41
75
  - !ruby/object:Gem::Dependency
42
76
  name: rspec
43
77
  requirement: !ruby/object:Gem::Requirement
44
78
  requirements:
45
- - - ~>
79
+ - - "~>"
46
80
  - !ruby/object:Gem::Version
47
- version: 3.5.0
81
+ version: '3.5'
48
82
  type: :development
49
83
  prerelease: false
50
84
  version_requirements: !ruby/object:Gem::Requirement
51
85
  requirements:
52
- - - ~>
86
+ - - "~>"
53
87
  - !ruby/object:Gem::Version
54
- version: 3.5.0
88
+ version: '3.5'
55
89
  - !ruby/object:Gem::Dependency
56
90
  name: rubocop
57
91
  requirement: !ruby/object:Gem::Requirement
58
92
  requirements:
59
- - - ~>
93
+ - - "~>"
60
94
  - !ruby/object:Gem::Version
61
95
  version: 0.41.2
62
96
  type: :development
63
97
  prerelease: false
64
98
  version_requirements: !ruby/object:Gem::Requirement
65
99
  requirements:
66
- - - ~>
100
+ - - "~>"
67
101
  - !ruby/object:Gem::Version
68
102
  version: 0.41.2
69
103
  - !ruby/object:Gem::Dependency
70
104
  name: webmock
71
105
  requirement: !ruby/object:Gem::Requirement
72
106
  requirements:
73
- - - ~>
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '2.1'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '2.1'
117
+ - !ruby/object:Gem::Dependency
118
+ name: timecop
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
74
122
  - !ruby/object:Gem::Version
75
- version: 2.1.0
123
+ version: '0.8'
76
124
  type: :development
77
125
  prerelease: false
78
126
  version_requirements: !ruby/object:Gem::Requirement
79
127
  requirements:
80
- - - ~>
128
+ - - "~>"
81
129
  - !ruby/object:Gem::Version
82
- version: 2.1.0
130
+ version: '0.8'
83
131
  description: Client for the Companies House REST API. Provides company profiles and
84
132
  officer lists.
85
133
  email:
@@ -97,6 +145,7 @@ files:
97
145
  - lib/companies_house/client.rb
98
146
  - lib/companies_house/not_found_error.rb
99
147
  - lib/companies_house/rate_limit_error.rb
148
+ - lib/companies_house/request.rb
100
149
  - lib/companies_house/version.rb
101
150
  homepage: https://github.com/gocardless/companies-house-rest
102
151
  licenses:
@@ -108,17 +157,17 @@ require_paths:
108
157
  - lib
109
158
  required_ruby_version: !ruby/object:Gem::Requirement
110
159
  requirements:
111
- - - ~>
160
+ - - "~>"
112
161
  - !ruby/object:Gem::Version
113
- version: '2.0'
162
+ version: 2.2.2
114
163
  required_rubygems_version: !ruby/object:Gem::Requirement
115
164
  requirements:
116
- - - '>='
165
+ - - ">="
117
166
  - !ruby/object:Gem::Version
118
167
  version: '0'
119
168
  requirements: []
120
169
  rubyforge_project:
121
- rubygems_version: 2.0.14.1
170
+ rubygems_version: 2.5.1
122
171
  signing_key:
123
172
  specification_version: 4
124
173
  summary: Look up UK company registration information