defra_ruby_validators 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -18
  3. data/config/locales/defra_ruby/validators/companies_house_number_validator/en.yml +10 -0
  4. data/lib/defra_ruby/validators.rb +15 -0
  5. data/lib/defra_ruby/validators/base_validator.rb +21 -0
  6. data/lib/defra_ruby/validators/companies_house_number_validator.rb +54 -0
  7. data/lib/defra_ruby/validators/companies_house_service.rb +36 -0
  8. data/lib/defra_ruby/validators/configuration.rb +35 -0
  9. data/lib/defra_ruby/validators/engine.rb +22 -0
  10. data/lib/defra_ruby/validators/version.rb +7 -0
  11. data/lib/defra_ruby_validators.rb +27 -9
  12. data/spec/cassettes/company_no_inactive.yml +14 -14
  13. data/spec/cassettes/company_no_not_found.yml +15 -15
  14. data/spec/cassettes/company_no_valid.yml +14 -14
  15. data/spec/defra_ruby/validators/companies_house_number_validator_spec.rb +88 -0
  16. data/spec/defra_ruby/validators/companies_house_service_spec.rb +58 -0
  17. data/spec/defra_ruby/validators/configuration_spec.rb +49 -0
  18. data/spec/defra_ruby/validators_spec.rb +12 -0
  19. data/spec/examples.txt +36 -27
  20. data/spec/spec_helper.rb +69 -19
  21. data/spec/support/defra_ruby_validators.rb +13 -2
  22. data/spec/support/dotenv.rb +4 -0
  23. data/spec/support/helpers/translator.rb +14 -0
  24. data/spec/support/pry.rb +7 -0
  25. data/spec/support/shared_examples/validators/invalid_record.rb +18 -0
  26. data/spec/support/shared_examples/validators/valid_record.rb +12 -0
  27. data/spec/support/shared_examples/validators/validator.rb +8 -0
  28. data/spec/support/simplecov.rb +17 -0
  29. metadata +37 -19
  30. data/config/locales/defra_ruby_validators/companies_house_number/en.yml +0 -14
  31. data/lib/defra_ruby_validators/companies_house_number_validator.rb +0 -56
  32. data/lib/defra_ruby_validators/companies_house_service.rb +0 -34
  33. data/lib/defra_ruby_validators/engine.rb +0 -20
  34. data/lib/defra_ruby_validators/validators.rb +0 -33
  35. data/lib/defra_ruby_validators/version.rb +0 -5
  36. data/spec/defra_ruby_validators/companies_house_number_validator_spec.rb +0 -125
  37. data/spec/defra_ruby_validators/companies_house_service_spec.rb +0 -55
  38. data/spec/defra_ruby_validators/version_spec.rb +0 -7
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.shared_examples "a valid record" do |validatable|
4
+ it "confirms the object is valid" do
5
+ expect(validatable).to be_valid
6
+ end
7
+
8
+ it "the errors are empty" do
9
+ validatable.valid?
10
+ expect(validatable.errors).to be_empty
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.shared_examples "a validator" do
4
+ it "is a type of BaseValidator" do
5
+ expect(described_class.ancestors)
6
+ .to include(DefraRuby::Validators::BaseValidator)
7
+ end
8
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "simplecov"
4
+
5
+ # We start it with the rails param to ensure it includes coverage for all code
6
+ # started by the rails app, and not just the files touched by our unit tests.
7
+ # This gives us the most accurate assessment of our unit test coverage
8
+ # https://github.com/colszowka/simplecov#getting-started
9
+ SimpleCov.start do
10
+ # We filter the spec folder, mainly to ensure that any dummy apps don't get
11
+ # included in the coverage report. However our intent is that nothing in the
12
+ # spec folder should be included
13
+ add_filter "/spec/"
14
+ # The version file is simply just that, so we do not feel the need to ensure
15
+ # we have a test for it
16
+ add_filter "lib/defra_ruby/validators/version"
17
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: defra_ruby_validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Defra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-30 00:00:00.000000000 Z
11
+ date: 2019-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: byebug
42
+ name: defra_ruby_style
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: defra_ruby_style
56
+ name: dotenv
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: dotenv
70
+ name: github_changelog_generator
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: github_changelog_generator
84
+ name: i18n
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -95,7 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: i18n
98
+ name: pry-byebug
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -204,23 +204,33 @@ files:
204
204
  - Rakefile
205
205
  - bin/console
206
206
  - bin/setup
207
- - config/locales/defra_ruby_validators/companies_house_number/en.yml
207
+ - config/locales/defra_ruby/validators/companies_house_number_validator/en.yml
208
+ - lib/defra_ruby/validators.rb
209
+ - lib/defra_ruby/validators/base_validator.rb
210
+ - lib/defra_ruby/validators/companies_house_number_validator.rb
211
+ - lib/defra_ruby/validators/companies_house_service.rb
212
+ - lib/defra_ruby/validators/configuration.rb
213
+ - lib/defra_ruby/validators/engine.rb
214
+ - lib/defra_ruby/validators/version.rb
208
215
  - lib/defra_ruby_validators.rb
209
- - lib/defra_ruby_validators/companies_house_number_validator.rb
210
- - lib/defra_ruby_validators/companies_house_service.rb
211
- - lib/defra_ruby_validators/engine.rb
212
- - lib/defra_ruby_validators/validators.rb
213
- - lib/defra_ruby_validators/version.rb
214
216
  - spec/cassettes/company_no_inactive.yml
215
217
  - spec/cassettes/company_no_not_found.yml
216
218
  - spec/cassettes/company_no_valid.yml
217
- - spec/defra_ruby_validators/companies_house_number_validator_spec.rb
218
- - spec/defra_ruby_validators/companies_house_service_spec.rb
219
- - spec/defra_ruby_validators/version_spec.rb
219
+ - spec/defra_ruby/validators/companies_house_number_validator_spec.rb
220
+ - spec/defra_ruby/validators/companies_house_service_spec.rb
221
+ - spec/defra_ruby/validators/configuration_spec.rb
222
+ - spec/defra_ruby/validators_spec.rb
220
223
  - spec/examples.txt
221
224
  - spec/spec_helper.rb
222
225
  - spec/support/defra_ruby_validators.rb
226
+ - spec/support/dotenv.rb
227
+ - spec/support/helpers/translator.rb
223
228
  - spec/support/i18n.rb
229
+ - spec/support/pry.rb
230
+ - spec/support/shared_examples/validators/invalid_record.rb
231
+ - spec/support/shared_examples/validators/valid_record.rb
232
+ - spec/support/shared_examples/validators/validator.rb
233
+ - spec/support/simplecov.rb
224
234
  - spec/support/vcr.rb
225
235
  homepage: https://github.com/DEFRA/defra-ruby-validators
226
236
  licenses:
@@ -249,13 +259,21 @@ specification_version: 4
249
259
  summary: Defra ruby on rails validations
250
260
  test_files:
251
261
  - spec/spec_helper.rb
252
- - spec/defra_ruby_validators/companies_house_service_spec.rb
253
- - spec/defra_ruby_validators/version_spec.rb
254
- - spec/defra_ruby_validators/companies_house_number_validator_spec.rb
262
+ - spec/defra_ruby/validators_spec.rb
263
+ - spec/defra_ruby/validators/companies_house_service_spec.rb
264
+ - spec/defra_ruby/validators/configuration_spec.rb
265
+ - spec/defra_ruby/validators/companies_house_number_validator_spec.rb
255
266
  - spec/examples.txt
256
267
  - spec/cassettes/company_no_not_found.yml
257
268
  - spec/cassettes/company_no_valid.yml
258
269
  - spec/cassettes/company_no_inactive.yml
270
+ - spec/support/simplecov.rb
259
271
  - spec/support/i18n.rb
260
272
  - spec/support/vcr.rb
273
+ - spec/support/pry.rb
274
+ - spec/support/dotenv.rb
261
275
  - spec/support/defra_ruby_validators.rb
276
+ - spec/support/helpers/translator.rb
277
+ - spec/support/shared_examples/validators/invalid_record.rb
278
+ - spec/support/shared_examples/validators/valid_record.rb
279
+ - spec/support/shared_examples/validators/validator.rb
@@ -1,14 +0,0 @@
1
- en:
2
- defra_ruby_validators:
3
- companies_house_number:
4
- hint: An 8 digit number, or 2 letters followed by a 6 digit number, or 2 letters followed by 5 digits and another letter
5
- help:
6
- heading: What's a registration number?
7
- limitedCompany: This is issued when the company is registered, and can be found on your Certificate of Incorporation and any paperwork from Companies House.
8
- limitedLiabilityPartnership: This is issued when the limited liability partnership is registered, and can be found on your Certificate of Incorporation and any paperwork from Companies House.
9
- errors:
10
- blank: Enter a company registration number
11
- invalid: "Enter a valid number - it should have 8 digits, or 2 letters followed by 6 digits, or 2 letters followed by 5 digits and another letter. If your number has only 7 digits, enter it with a zero at the start."
12
- not_found: Companies House couldn't find a company with this number
13
- inactive: Your company must be registered as an active company
14
- error: There was an error connecting with Companies House. Hopefully this is a one off and will work if you try again.
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DefraRubyValidators
4
- class CompaniesHouseNumberValidator < ActiveModel::EachValidator
5
-
6
- # Examples we need to validate are
7
- # 10997904, 09764739
8
- # SC534714, CE000958
9
- # IP00141R, IP27702R, SP02252R
10
- # https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/426891/uniformResourceIdentifiersCustomerGuide.pdf
11
- VALID_COMPANIES_HOUSE_REGISTRATION_NUMBER_REGEX = Regexp.new(
12
- /\A(\d{8,8}$)|([a-zA-Z]{2}\d{6}$)|([a-zA-Z]{2}\d{5}[a-zA-Z]{1}$)\z/i
13
- ).freeze
14
-
15
- def validate_each(record, attribute, value)
16
- return false unless value_is_present?(record, attribute, value)
17
- return false unless format_is_valid?(record, attribute, value)
18
-
19
- validate_with_companies_house(record, attribute, value)
20
- end
21
-
22
- private
23
-
24
- def value_is_present?(record, attribute, value)
25
- return true if value.present?
26
-
27
- record.errors[attribute] << error_message("blank")
28
- false
29
- end
30
-
31
- def format_is_valid?(record, attribute, value)
32
- return true if value.match?(VALID_COMPANIES_HOUSE_REGISTRATION_NUMBER_REGEX)
33
-
34
- record.errors[attribute] << error_message("invalid")
35
- false
36
- end
37
-
38
- def validate_with_companies_house(record, attribute, value)
39
- case CompaniesHouseService.new(value).status
40
- when :active
41
- true
42
- when :inactive
43
- record.errors[attribute] << error_message("inactive")
44
- when :not_found
45
- record.errors[attribute] << error_message("not_found")
46
- end
47
- rescue StandardError
48
- record.errors[attribute] << error_message("error")
49
- end
50
-
51
- def error_message(error)
52
- I18n.t("defra_ruby_validators.companies_house_number.errors.#{error}")
53
- end
54
-
55
- end
56
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rest-client"
4
-
5
- module DefraRubyValidators
6
- class CompaniesHouseService
7
- def initialize(company_no)
8
- @company_no = company_no
9
- @url = "#{DefraRubyValidators.configuration.companies_house_host}#{@company_no}"
10
- @api_key = DefraRubyValidators.configuration.companies_house_api_key
11
- end
12
-
13
- def status
14
- response = RestClient::Request.execute(
15
- method: :get,
16
- url: @url,
17
- user: @api_key,
18
- password: ""
19
- )
20
-
21
- json = JSON.parse(response)
22
-
23
- status_is_allowed?(json["company_status"]) ? :active : :inactive
24
- rescue RestClient::ResourceNotFound
25
- :not_found
26
- end
27
-
28
- private
29
-
30
- def status_is_allowed?(status)
31
- %w[active voluntary-arrangement].include?(status)
32
- end
33
- end
34
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "defra_ruby_validators/validators"
4
-
5
- module DefraRubyValidators
6
- # Engine used to load in the custom validations
7
- class Engine < ::Rails::Engine
8
- isolate_namespace DefraRubyValidators
9
-
10
- # Add a load path for this specific Engine
11
- config.autoload_paths += Dir[File.join(config.root, "lib", "**")]
12
-
13
- # Load I18n translation files from engine before loading ones from the host app
14
- # This means values in the host app can override those in the engine
15
- config.before_initialize do
16
- engine_locales = Dir["#{config.root}/config/locales/**/*.yml"]
17
- config.i18n.load_path = engine_locales + config.i18n.load_path
18
- end
19
- end
20
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "active_model"
4
- require "defra_ruby_validators/version"
5
- require "defra_ruby_validators/companies_house_service"
6
-
7
- require "defra_ruby_validators/companies_house_number_validator"
8
-
9
- module DefraRubyValidators
10
- # Enable the ability to configure the gem from its host app, rather than
11
- # reading directly from env vars. Derived from
12
- # https://robots.thoughtbot.com/mygem-configure-block
13
- class << self
14
- attr_writer :configuration
15
-
16
- def configuration
17
- @configuration ||= Configuration.new
18
- end
19
- end
20
-
21
- def self.configure
22
- yield(configuration)
23
- end
24
-
25
- class Configuration
26
- attr_accessor :companies_house_host, :companies_house_api_key
27
-
28
- def initialize
29
- @companies_house_host = "https://api.companieshouse.gov.uk/company/"
30
- @companies_house_api_key = nil
31
- end
32
- end
33
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DefraRubyValidators
4
- VERSION = "0.1.1"
5
- end
@@ -1,125 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DefraRubyValidators
4
- RSpec.describe CompaniesHouseNumberValidator do
5
- context "when given a valid company number" do
6
- before do
7
- allow_any_instance_of(CompaniesHouseService).to receive(:status).and_return(:active)
8
- end
9
-
10
- %w[10997904 09764739 SC534714 IP00141R].each do |company_no|
11
- context "like #{company_no}" do
12
- it "confirms the number is valid" do
13
- expect(DummyCompaniesHouseNumber.new(company_no)).to be_valid
14
- end
15
- end
16
- end
17
- end
18
-
19
- context "when given an invalid company number" do
20
- context "because it is blank" do
21
- subject { DummyCompaniesHouseNumber.new("") }
22
-
23
- it "confirms the number is not valid" do
24
- expect(subject).to_not be_valid
25
- end
26
- it "adds an error to the subject's errors collection" do
27
- subject.valid?
28
- expect(subject.errors.count).to eq(1)
29
- end
30
- it "adds an error with the correct message" do
31
- subject.valid?
32
- expect(subject.errors[:company_no][0]).to eq(I18n.t("defra_ruby_validators.companies_house_number.errors.blank"))
33
- end
34
- end
35
-
36
- context "because the format is wrong" do
37
- subject { DummyCompaniesHouseNumber.new("foobar42") }
38
-
39
- it "confirms the number is not valid" do
40
- expect(subject).to_not be_valid
41
- end
42
- it "adds an error to the subject's errors collection" do
43
- subject.valid?
44
- expect(subject.errors.count).to eq(1)
45
- end
46
- it "adds an error with the correct message" do
47
- subject.valid?
48
- expect(subject.errors[:company_no][0]).to eq(I18n.t("defra_ruby_validators.companies_house_number.errors.invalid"))
49
- end
50
- end
51
-
52
- context "because it's not found on companies house" do
53
- before do
54
- allow_any_instance_of(CompaniesHouseService).to receive(:status).and_return(:not_found)
55
- end
56
-
57
- subject { DummyCompaniesHouseNumber.new("99999999") }
58
-
59
- it "confirms the number is not valid" do
60
- expect(subject).to_not be_valid
61
- end
62
- it "adds an error to the subject's errors collection" do
63
- subject.valid?
64
- expect(subject.errors.count).to eq(1)
65
- end
66
- it "adds an error with the correct message" do
67
- subject.valid?
68
- expect(subject.errors[:company_no][0]).to eq(I18n.t("defra_ruby_validators.companies_house_number.errors.not_found"))
69
- end
70
- end
71
-
72
- context "because it's not 'active' on companies house" do
73
- before do
74
- allow_any_instance_of(CompaniesHouseService).to receive(:status).and_return(:inactive)
75
- end
76
-
77
- subject { DummyCompaniesHouseNumber.new("07281919") }
78
-
79
- it "confirms the number is not valid" do
80
- expect(subject).to_not be_valid
81
- end
82
- it "adds an error to the subject's errors collection" do
83
- subject.valid?
84
- expect(subject.errors.count).to eq(1)
85
- end
86
- it "adds an error with the correct message" do
87
- subject.valid?
88
- expect(subject.errors[:company_no][0]).to eq(I18n.t("defra_ruby_validators.companies_house_number.errors.inactive"))
89
- end
90
- end
91
- end
92
-
93
- context "when there is an error connecting with companies house" do
94
- before do
95
- allow_any_instance_of(CompaniesHouseService).to receive(:status).and_raise(StandardError)
96
- end
97
-
98
- subject { DummyCompaniesHouseNumber.new("10997904") }
99
-
100
- it "confirms the number is not valid" do
101
- expect(subject).to_not be_valid
102
- end
103
- it "adds an error to the subject's errors collection" do
104
- subject.valid?
105
- expect(subject.errors.count).to eq(1)
106
- end
107
- it "adds an error with the correct message" do
108
- subject.valid?
109
- expect(subject.errors[:company_no][0]).to eq(I18n.t("defra_ruby_validators.companies_house_number.errors.error"))
110
- end
111
- end
112
-
113
- class DummyCompaniesHouseNumber
114
- include ActiveModel::Model
115
-
116
- attr_accessor :company_no
117
-
118
- def initialize(company_no)
119
- @company_no = company_no
120
- end
121
-
122
- validates :company_no, "defra_ruby_validators/companies_house_number": true
123
- end
124
- end
125
- end