companies-house-gateway 0.3.4 → 0.3.5

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: 9468ef053dc2a11fdc6aeedc3bd388860a601c04
4
- data.tar.gz: 6c8a2bcc628048b75f7a77421c00bcdd683ed056
3
+ metadata.gz: 730c887e04b54fc8bb3e60f7540e68eae00d59a9
4
+ data.tar.gz: b5f7058d26397da50d1afb2ae9be1eda41e916b8
5
5
  SHA512:
6
- metadata.gz: b5530cd08982bca3a4196d3f1021ca76404701e67a02a9e9a4db2c83fd2edfed3225e516b04b7a39810a47ac00489d3d62a139a0981b7b34636ecdea562cad4d
7
- data.tar.gz: 769936150be0ed783ca238c210e163d3375ff73b4c789c206ab4681a7f2e7d034f8f80f258d7cb60777d3c1a4f500f065575f474a67b19a9b31508a1a6e2f022
6
+ metadata.gz: 6b0d5806eb08164da30a9fc469429ee2807ad4fb9e7f98eee4da224a2d0d2acd8c57deeb2270bf84588b15bdd42d3ef047a188e83abedc4ad172737f2fdea9a9
7
+ data.tar.gz: 11dc3b0517c6d66b87654fd3136ee3a1c6bacb5391eb6e5bfc9c670ab00e9ddb8599232ddcd0317d244effdd10db905ad7d1d329df5b11b51d0b20dc1fe6cd8a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.3.5 - February 26, 2014
2
+
3
+ - Allow requests to be cached
4
+
1
5
  ## 0.3.4 - February 26, 2014
2
6
 
3
7
  - Fix XML generation for CompanyAppointments requests
data/README.md CHANGED
@@ -54,4 +54,13 @@ Set the "raw" argument to true if you need the full, unprocessed response
54
54
  ```ruby
55
55
  CompaniesHouseGateway.config[:raw] = true
56
56
  CompaniesHouseGateway.name_search(...) # => Faraday::Response object
57
+ ```
58
+
59
+ ### Caching
60
+
61
+ If you'd like to cache your requests, you can configure the gem:
62
+
63
+ ```ruby
64
+ CompaniesHouseGateway.config[:cache] = Rails.cache
65
+ CompaniesHouseGateway.config[:cache_args] = { expires_in: 10.minutes }
57
66
  ```
@@ -5,8 +5,10 @@ module CompaniesHouseGateway
5
5
  end
6
6
 
7
7
  def perform_check(*args)
8
- request = Request.new(connection, @config)
9
- request.perform(*args)
8
+ with_caching(args) do
9
+ request = Request.new(connection, @config)
10
+ request.perform(*args)
11
+ end
10
12
  end
11
13
 
12
14
  def config
@@ -42,5 +44,12 @@ module CompaniesHouseGateway
42
44
  c.adapter @config[:adapter]
43
45
  end
44
46
  end
47
+
48
+ # Use cache if configured
49
+ def with_caching(args, &block)
50
+ cache_args = config[:cache_args] || {}
51
+ cache = config[:cache]
52
+ cache ? cache.fetch(args, cache_args, &block) : yield
53
+ end
45
54
  end
46
55
  end
@@ -5,6 +5,8 @@ module CompaniesHouseGateway
5
5
  sender_id: nil,
6
6
  password: nil,
7
7
  email: nil,
8
+ cache: nil,
9
+ cache_args: nil,
8
10
  raw: false,
9
11
  api_endpoint: "http://xmlgw.companieshouse.gov.uk/v1-0/xmlgw/Gateway",
10
12
  user_agent: "CompaniesHouseGateway Ruby Gem #{CompaniesHouseGateway::VERSION}".freeze
@@ -18,5 +18,15 @@ module CompaniesHouseGateway
18
18
  "CUR", # Current (i.e., not above)
19
19
  "EUR", # SE and ES appointments only
20
20
  ].freeze
21
+
22
+ # White list of allowed prefixes for companies house numbers.
23
+ # 1st line is English and Welsh prefixes
24
+ # 2nd line is Scottish prefixes
25
+ # 3rd line is Northen Irish prefixes
26
+ # \d\d is the default prefix in regex notation.
27
+ ALLOWED_PREFIXES = %w(AC BR FC GE IP LP OC RC SE ZC
28
+ SC SA SF SL SZ SP SO SR
29
+ NI NA NF NL NZ NP NO NR
30
+ \d\d)
21
31
  end
22
32
  end
@@ -3,8 +3,8 @@ module CompaniesHouseGateway
3
3
 
4
4
  VALIDATIONS = {
5
5
  company_name: ->(value) { clean_string(value, :company_name) },
6
- company_number: ->(value) { value }, # TODO!
7
- partial_company_number: ->(value) { value }, # TODO!
6
+ company_number: ->(value) { clean_company_number(value) },
7
+ partial_company_number: ->(value) { value },
8
8
  give_mort_totals: ->(value) { clean_boolean(value, :give_mort_totals) },
9
9
  search_rows: ->(value) { clean_number(value, :search_rows) },
10
10
  data_set: ->(value) { clean_data_set(value) },
@@ -66,6 +66,25 @@ module CompaniesHouseGateway
66
66
  type
67
67
  end
68
68
 
69
+ def self.clean_company_number(number)
70
+ return unless number
71
+ number = number.to_s.strip # remove whitespace
72
+
73
+ # 0-pad 5 or 7 digit registration number
74
+ if number.match /\A(\D{2})(\d{5})\z/
75
+ number = $1 + $2.rjust(6,"0")
76
+ elsif number.match /\A\d{7}\z/
77
+ number = number.rjust(8, "0")
78
+ end
79
+
80
+ companies_house_regex = /\A(#{Constants::ALLOWED_PREFIXES * '|'})\d{6}\z/
81
+ if number =~ companies_house_regex
82
+ number
83
+ else
84
+ input_error(:company_number, number)
85
+ end
86
+ end
87
+
69
88
  def self.input_error(param, value=nil)
70
89
  msg = if value.nil?
71
90
  "#{param} is a required param"
@@ -1,3 +1,3 @@
1
1
  module CompaniesHouseGateway
2
- VERSION = '0.3.4'.freeze
2
+ VERSION = '0.3.5'.freeze
3
3
  end
data/spec/client_spec.rb CHANGED
@@ -43,6 +43,16 @@ describe CompaniesHouseGateway::Client do
43
43
  to receive(:perform).once
44
44
  client.perform_check({})
45
45
  end
46
+
47
+ context "if caching is enabled" do
48
+ let(:cached_response) { double("Cached response") }
49
+ let(:cache_stub) { double("Cache", fetch: cached_response) }
50
+ before { config[:cache] = cache_stub }
51
+
52
+ it "caches the request (if enabled)" do
53
+ client.perform_check({}) == cached_response
54
+ end
55
+ end
46
56
  end
47
57
 
48
58
  it_behaves_like "it delegates to the check", :name_search
@@ -154,4 +154,70 @@ describe CompaniesHouseGateway::Validations do
154
154
  end
155
155
  end
156
156
  end
157
+
158
+ describe '#clean_company_number' do
159
+ subject do
160
+ CompaniesHouseGateway::Validations.clean_company_number(number)
161
+ end
162
+
163
+ context "with a number that's too short" do
164
+ let(:number) { "123456" }
165
+ it "throws an exception" do
166
+ expect { subject }.
167
+ to raise_error CompaniesHouseGateway::InvalidRequestError
168
+ end
169
+ end
170
+
171
+ context "with a number that's too long" do
172
+ let(:number) { "123456789" }
173
+ it "throws an exception" do
174
+ expect { subject }.
175
+ to raise_error CompaniesHouseGateway::InvalidRequestError
176
+ end
177
+ end
178
+
179
+ context "with a number that contains spaces" do
180
+ let(:number) { "1234 ABC" }
181
+ it "throws an exception" do
182
+ expect { subject }.
183
+ to raise_error CompaniesHouseGateway::InvalidRequestError
184
+ end
185
+ end
186
+
187
+ context "with a number that contains non alphanumeric chars" do
188
+ let(:number) { "1234?456" }
189
+ it "throws an exception" do
190
+ expect { subject }.
191
+ to raise_error CompaniesHouseGateway::InvalidRequestError
192
+ end
193
+ end
194
+
195
+ CompaniesHouseGateway::Constants::ALLOWED_PREFIXES.each do |prefix|
196
+ context "allows a company number starting with #{prefix}" do
197
+ prefix = (prefix == '\d\d') ? '07' : prefix
198
+ let(:number) { "#{prefix}112233" }
199
+ it { should == "#{prefix}112233" }
200
+ end
201
+ end
202
+
203
+ context "does not 0-pads 8 digit registration numbers" do
204
+ let(:number) { "17495895" }
205
+ it { should == "17495895" }
206
+ end
207
+
208
+ context "0-pads 7 digit registration numbers" do
209
+ let(:number) { "7495895" }
210
+ it { should == "07495895" }
211
+ end
212
+
213
+ context "0-pads 5 digit NI registration numbers" do
214
+ let(:number) { "NI27768" }
215
+ it { should == "NI027768" }
216
+ end
217
+
218
+ context "does not 0-pads 6 digit NI registration numbers" do
219
+ let(:number) { "NI127768" }
220
+ it { should == "NI127768" }
221
+ end
222
+ end
157
223
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: companies-house-gateway
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grey Baker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-26 00:00:00.000000000 Z
11
+ date: 2014-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday