ideal_postcodes 0.1.1 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ci.yml +22 -0
  3. data/CHANGELOG.md +20 -0
  4. data/LICENSE +21 -0
  5. data/README.md +289 -43
  6. data/Rakefile +13 -6
  7. data/ideal-postcodes-ruby.gemspec +11 -11
  8. data/lib/ideal_postcodes.rb +1 -1
  9. data/lib/idealpostcodes/address.rb +31 -0
  10. data/lib/idealpostcodes/errors.rb +31 -26
  11. data/lib/idealpostcodes/key.rb +14 -0
  12. data/lib/idealpostcodes/postcode.rb +20 -32
  13. data/lib/idealpostcodes/util.rb +38 -25
  14. data/lib/idealpostcodes/version.rb +2 -2
  15. data/lib/idealpostcodes.rb +114 -81
  16. data/spec/addresses_spec.rb +68 -0
  17. data/spec/idealpostcodes_spec.rb +56 -0
  18. data/spec/keys_spec.rb +25 -0
  19. data/spec/postcodes_spec.rb +77 -0
  20. data/spec/spec_helper.rb +89 -0
  21. data/spec/vcr_cassettes/IdealPostcodes_Address_lookup_raises_an_exception_if_invalid_key.yml +42 -0
  22. data/spec/vcr_cassettes/IdealPostcodes_Address_lookup_raises_an_exception_if_limit_breached.yml +42 -0
  23. data/spec/vcr_cassettes/IdealPostcodes_Address_lookup_raises_an_exception_if_no_lookups_remaining.yml +42 -0
  24. data/spec/vcr_cassettes/IdealPostcodes_Address_lookup_returns_an_address_for_a_valid_UDPRN.yml +74 -0
  25. data/spec/vcr_cassettes/IdealPostcodes_Address_lookup_returns_nil_for_an_invalid_UDPRN.yml +42 -0
  26. data/spec/vcr_cassettes/IdealPostcodes_Address_search_is_sensitive_to_limit.yml +81 -0
  27. data/spec/vcr_cassettes/IdealPostcodes_Address_search_is_sensitive_to_page.yml +369 -0
  28. data/spec/vcr_cassettes/IdealPostcodes_Address_search_raises_an_exception_if_invalid_key.yml +42 -0
  29. data/spec/vcr_cassettes/IdealPostcodes_Address_search_raises_an_exception_if_limit_breached.yml +42 -0
  30. data/spec/vcr_cassettes/IdealPostcodes_Address_search_raises_an_exception_if_no_lookups_remaining.yml +42 -0
  31. data/spec/vcr_cassettes/IdealPostcodes_Address_search_returns_results_in_a_SearchResult_object.yml +273 -0
  32. data/spec/vcr_cassettes/IdealPostcodes_Key_lookup_details_returns_key_details.yml +62 -0
  33. data/spec/vcr_cassettes/IdealPostcodes_Key_lookup_returns_the_availability_status_of_a_key_false_key_.yml +45 -0
  34. data/spec/vcr_cassettes/IdealPostcodes_Key_lookup_returns_the_availability_status_of_a_key_true_key_.yml +45 -0
  35. data/spec/vcr_cassettes/IdealPostcodes_Postcode_find_by_location_is_sensitive_to_limit_parameter.yml +52 -0
  36. data/spec/vcr_cassettes/IdealPostcodes_Postcode_find_by_location_is_sensitive_to_radius_parament.yml +133 -0
  37. data/spec/vcr_cassettes/IdealPostcodes_Postcode_find_by_location_returns_an_array_of_postcodes_and_locations.yml +84 -0
  38. data/spec/vcr_cassettes/IdealPostcodes_Postcode_find_by_location_returns_an_empty_array_if_no_results_are_found.yml +43 -0
  39. data/spec/vcr_cassettes/IdealPostcodes_Postcode_lookup_raises_an_exception_if_invalid_key.yml +42 -0
  40. data/spec/vcr_cassettes/IdealPostcodes_Postcode_lookup_raises_an_exception_if_key_has_run_out_of_balance.yml +42 -0
  41. data/spec/vcr_cassettes/IdealPostcodes_Postcode_lookup_raises_an_exception_if_limit_has_been_reached.yml +42 -0
  42. data/spec/vcr_cassettes/IdealPostcodes_Postcode_lookup_returns_a_list_of_addresses_for_a_postcode.yml +268 -0
  43. data/spec/vcr_cassettes/IdealPostcodes_Postcode_lookup_returns_an_empty_array_if_postcode_does_not_exist.yml +42 -0
  44. data/spec/vcr_cassettes/IdealPostcodes_key_available_returns_false_if_key_is_unavailable.yml +45 -0
  45. data/spec/vcr_cassettes/IdealPostcodes_key_available_returns_true_if_key_is_available.yml +45 -0
  46. data/spec/vcr_cassettes/IdealPostcodes_key_details_raises_an_exception_if_no_secret_is_provided.yml +45 -0
  47. data/spec/vcr_cassettes/IdealPostcodes_key_details_returns_key_information.yml +62 -0
  48. data/spec/vcr_cassettes/IdealPostcodes_request_generates_a_HTTP_request.yml +268 -0
  49. data/spec/vcr_cassettes/IdealPostcodes_request_raises_authentication_error_if_invalid_key_is_provided.yml +42 -0
  50. data/spec/vcr_cassettes/IdealPostcodes_request_raises_limit_reached_error_if_a_limit_has_been_breached.yml +42 -0
  51. data/spec/vcr_cassettes/IdealPostcodes_request_raises_token_exhausted_error_if_key_balance_is_depleted.yml +42 -0
  52. metadata +117 -52
  53. data/test/test_helper.rb +0 -77
  54. data/test/test_ideal_postcodes.rb +0 -78
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c34b866375b1eb59033c3c9f4139ed49e1f91bd1
4
- data.tar.gz: 48da9070ca6d8cb2af643879f1837d697c3a89af
2
+ SHA256:
3
+ metadata.gz: 631e1f7827c1ebd216ac8da69d07f41e99dfcb542ea8db79160f81fb4e27e2f7
4
+ data.tar.gz: 528c592ee660794a49eaca814817181d7bb46dc67a848dd3015d9ed64023af3a
5
5
  SHA512:
6
- metadata.gz: 11b659c4e56fb6188874468b2c2e8363eb7088ca13672540ced297f762f5a096f53f49f51d6b2144bc6af0a3006596e2be9e550291aff14ef15d28e6ea2e04ce
7
- data.tar.gz: b64a4e49837cfad1410b382600f2af5eff9ee1fbea7a833689f1b34c021c10a75154f445e6363c0e6cc9f50ce79e2082af292f3c995b084b670301b9b12b3195
6
+ metadata.gz: f9f1c1c8405bad2e5f2593609a880f667f5b1b89643540e197aa79d348f1027abcfa7ca0de1c16d485861a6ab9d42471761068435b56be25584ed7cbcc269799
7
+ data.tar.gz: '078849bcd14507cab9dcc8263bcd823dd154d556da85065b8909b37ae5b504c51aa0d8a67b6413c487e82f5b37c4415abdab033ceb98465d450848f6a272ff8e'
@@ -0,0 +1,22 @@
1
+ name: CI
2
+ on:
3
+ push:
4
+ jobs:
5
+ test:
6
+ runs-on: ubuntu-latest
7
+ strategy:
8
+ matrix:
9
+ ruby:
10
+ - '2.5'
11
+ - '2.6'
12
+ - '2.7'
13
+ - '3.0'
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - name: Set up Ruby
17
+ uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby }}
20
+ bundler-cache: true
21
+ - name: Run tests
22
+ run: bundle exec rake
data/CHANGELOG.md ADDED
@@ -0,0 +1,20 @@
1
+ # Changelog
2
+
3
+ Any changes, including backwards incompatible changes will be listed here
4
+
5
+ ## 2.0.0 (11/10/2017)
6
+
7
+ - Adds Ruby 2.4.0 support
8
+ - Drops 1.9 support
9
+
10
+ ## 1.1.0 (22/6/2017)
11
+
12
+ - Updated rest-client dependency
13
+
14
+ ## 1.0.0
15
+ - Major rewrite to make way for more resources
16
+ - Breaking change applied to postcode lookup functionality
17
+ - Implemented [addresses resource](https://ideal-postcodes.co.uk/documentation/addresses)
18
+ - Implemented [keys resource](https://ideal-postcodes.co.uk/documentation/keys)
19
+ - Implemented [postcodes resource](https://ideal-postcodes.co.uk/documentation/postcodes). Added location-based postcode searches
20
+ - Swapped out test suite with rspec and vcr
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ideal Postcodes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,57 +1,93 @@
1
- # Ideal-Postcodes.co.uk API Wrapper
1
+ # Ideal Postcodes Ruby Library [![Build Status](https://travis-ci.org/ideal-postcodes/ideal-postcodes-ruby.png)](https://travis-ci.org/ideal-postcodes/ideal-postcodes-ruby)
2
2
 
3
- Get a full list of addresses for any given UK postcode using the Ideal-Postcodes.co.uk API. We use the most accurate addressing database in the UK, Royal Mail's Postcode Address File.
3
+ Ideal Postcodes is a simple JSON API to query UK postcodes and addresses. Find out more at [Ideal-Postcodes.co.uk](https://ideal-postcodes.co.uk/)
4
+
5
+ Our API uses Royal Mail's Postcode Address File and is updated daily. Each method incurs a small charge (typically 2p) - free methods are labelled as such and are based on open data sources.
4
6
 
5
7
  ## Getting Started
6
8
 
7
- __Install it__
9
+ **Install**
8
10
 
9
11
  ```bash
10
12
  gem install ideal_postcodes
11
13
  ```
12
14
 
13
- Alternatively for rails, include this in your gemfile and bundle install
15
+ Alternatively, include this in your gemfile and bundle install
14
16
 
15
17
  ```ruby
16
18
  gem 'ideal_postcodes'
17
19
  ```
18
20
 
19
- __Get an API Key__
21
+ **Create a Key**
20
22
 
21
- Get a key at [Ideal-Postcodes.co.uk](https://ideal-postcodes.co.uk). Try out the service with the test postcode 'ID1 1QD'
23
+ Sign up at [Ideal-Postcodes.co.uk](https://ideal-postcodes.co.uk) and create a key.
22
24
 
23
- __Configuration__
25
+ **Configure**
24
26
 
25
- In order to perform lookups, you'll need to configure the gem by passing in your api key by doing the following:
27
+ Drop in your key when using the library.
26
28
 
27
29
  ```ruby
28
- require 'ideal_postcodes'
29
-
30
30
  IdealPostcodes.api_key = "your_key_goes_here"
31
31
  ```
32
32
 
33
- **For Rails**, the best way to do this is create a file in your initializers folder and drop in your key with this line of code:
33
+ ## Error Handling
34
+
35
+ It's important that you lookout for common exceptions when interacting with the API. The most common exceptions can be caught as shown below.
34
36
 
35
37
  ```ruby
36
- IdealPostcodes.api_key = "<your key goes here>"
38
+ begin
39
+ IdealPostcodes::Postcode.lookup "ID1 1QD"
40
+ rescue IdealPostcodes::AuthenticationError => e
41
+ # Invalid API Key
42
+ rescue IdealPostcodes::TokenExhaustedError => e
43
+ # Token has run out of lookups
44
+ rescue IdealPostcodes::LimitReachedError => e
45
+ # One of your predefinied limits has been reached
46
+ rescue IdealPostcodes::IdealPostcodesError => e
47
+ # API Error
48
+ rescue => e
49
+ # An unexpected error
50
+ end
51
+ ```
52
+
53
+ Possible errors to look out for are listed in the [documentation](https://ideal-postcodes.co.uk/documentation/response-codes).
54
+
55
+ ## Methods
56
+
57
+ The client provides a number of methods to allow you to get specific jobs done quickly and easily. These methods are listed below.
58
+
59
+
60
+
61
+
62
+ ## Get all addresses for a postcode [(docs)](https://ideal-postcodes.co.uk/documentation/postcodes#postcode)
63
+
64
+ ```
65
+ IdealPostcodes::Postcode.lookup postcode
37
66
  ```
38
67
 
39
- __Usage__
68
+ Returns an array of addresses representing all addresses at the specified postcode.
40
69
 
41
- Simply call #lookup class method on IdealPostcodes::Postcode. This will return a Postcode object containing the complete list of addresses if the postcode exists.
70
+ **Arguments**
71
+
72
+ - `postcode` (string). The postcode you want to lookup, case and space insensitive.
73
+
74
+ **Returns**
75
+
76
+ An array of hashes which represent each address at the postcode. Returns an empty array for an invalid postcode.
77
+
78
+ **Example**
42
79
 
43
80
  ```ruby
44
- postcode = IdealPostcodes::Postcode.lookup "ID1 1QD"
81
+ addresses = IdealPostcodes::Postcode.lookup "ID1 1QD"
45
82
 
46
- if postcode.empty?
83
+ if addresses.empty?
47
84
  puts "Your postcode doesn't have a match"
48
85
  else
49
- postcode.addresses
86
+ puts addresses
50
87
  end
51
88
 
52
- # postcode.addresses =>
53
- #
54
- # [
89
+ # addresses =>
90
+ #[
55
91
  # {
56
92
  # :postcode => "ID1 1QD",
57
93
  # :post_town => "LONDON",
@@ -61,44 +97,254 @@ end
61
97
  # :organisation_name => "",
62
98
  # :building_name => "Kingsley Hall",
63
99
  # :udprn => 12345678
64
- # }, ... and so on
100
+ # ... and so on
101
+ ```
102
+
103
+ **Notes**
104
+
105
+ **Data Source:** Royal Mail Postcode Address File. Ordnance Survey.
106
+
107
+ Use the postcode "ID1 1QD" to test this method for free. The complete list of test postcodes is available in the [documentation](https://ideal-postcodes/documentation/postcodes).
108
+
109
+
110
+
111
+
112
+ ## Search for an address [(docs)](https://ideal-postcodes.co.uk/documentation/addresses#query)
113
+
114
+ Perform a search for addresses which match your search term.
115
+
116
+ ```ruby
117
+ IdealPostcodes::Address.search search_term, limit: 20, page: 0
65
118
  ```
66
119
 
67
- __Exceptions__
120
+ **Arguments**
121
+
122
+ - `search_term` (string). The address you wish to search for
123
+ - `options` (hash, optional). Customise your search.
124
+ - `limit` (number). The maximum number of returned results per page
125
+ - `page` (number). Page of results to return (starts at page 0)
126
+
127
+ **Returns**
68
128
 
69
- The wrapper will raise an exception for anything other than a 200 response or an empty 404 response (which means no addresses at postcode).
129
+ Returns a search result object with the following attributes.
130
+
131
+ - `addresses` (Array). An array of hashes which represent each address at the postcode. The array is ordered by how close the search term and address match.
132
+ - `limit` (Number). The maximum number of returned results per page.
133
+ - `page` (Number). The returned page of results.
134
+
135
+
136
+ **Example**
70
137
 
71
138
  ```ruby
72
- begin
73
- IdealPostcodes::Postcode.lookup "ID1 1QD"
74
- rescue IdealPostcodes::AuthenticationError => e
75
- # Invalid API Key
76
- rescue IdealPostcodes::TokenExhaustedError => e
77
- # Token has run out of lookups
78
- rescue IdealPostcodes::LimitReachedError => e
79
- # One of your predefinied limits has been reached
80
- rescue IdealPostcodes::IdealPostcodesError => e
81
- # API Error
82
- rescue => e
83
- # An unexpected error
84
- end
139
+ IdealPostcodes::Address.search "10 Downing Street London"
140
+
141
+ r.limit # => 10
142
+ r.page # => 0
143
+ r.addresses
144
+
145
+ #[
146
+ # {
147
+ # :line_1=>"Prime Minister & First Lord Of The Treasury",
148
+ # :line_2=>"10 Downing Street",
149
+ # :line_3=>"",
150
+ # :post_town=>"LONDON",
151
+ # :postcode=>"SW1A 2AA",
152
+ # :organisation_name=>"Prime Minister & First Lord Of The Treasury",
153
+ # :premise=>"10",
154
+ # :latitude=>51.5035398826274
155
+ # :longitude=>-0.127695242183412,
156
+ # :thoroughfare=>"Downing Street",
157
+ # :district=>"Westminster",
158
+ # :ward=>"St James's",
159
+ # :building_number=>"10",
160
+ # :udprn=>23747771,
161
+ # ... and so on
85
162
  ```
86
163
 
87
- ## Registering
164
+ **Notes**
165
+
166
+ Data source: Royal Mail Postcode Address File, Ordnance Survey.
167
+
168
+ Use the address "ID1 1QD" to test integration for free. The complete list of test methods is available in the [documentation](https://ideal-postcodes/documentation/addresses).
88
169
 
89
- PAF is licensed from the Royal Mail and is, unfortunately, not free to use. Ideal Postcodes aims to be simple to use and fairly priced to use for web and mobile developers.
90
170
 
91
- We charge _2p_ per [external](https://ideal-postcodes.co.uk/termsandconditions#external) lookup.
92
171
 
93
- ## Documentation
94
172
 
95
- More documentation can be found [here](https://ideal-postcodes.co.uk/documentation/ruby-wrapper)
173
+ ## Get nearby postcode for a given geolocation [(docs)](https://ideal-postcodes.co.uk/documentation/postcodes#lonlat)
174
+
175
+ Retrieve the nearest postcodes for a given geolocation. Free to use.
176
+
177
+ ```ruby
178
+ IdealPostcodes::Postcode.find_by_location longitude: lon, latitude: lat
179
+ ```
180
+
181
+ **Arguments**
182
+
183
+ - `location` (Hash)
184
+ - `longitude` (number, required)
185
+ - `latitude` (number, required)
186
+ - `limit` (number, optional) Maximum number of results to return
187
+ - `radius` (number, optional) search radius (in metres)
188
+
189
+ **Returns**
190
+
191
+ An array of hashes which represent the nearest postcodes to the specified location. Ordered by distance from location.
192
+
193
+ **Example**
194
+
195
+ ```ruby
196
+ postcodes = IdealPostcodes::Postcode.find_by_location longitude: 0.629834, latitude: 51.79232
197
+
198
+ # postcodes =>
199
+ #[
200
+ # {
201
+ # :postcode=>"CM8 1EF",
202
+ # :northings=>213679,
203
+ # :eastings=>581461,
204
+ # :longitude=>0.629834723775309,
205
+ # :latitude=>51.7923246977375,
206
+ # :distance=>0.52506633},
207
+ # {
208
+ # :postcode=>"CM8 1EU",
209
+ # :northings=>213650,
210
+ # :eastings=>581507,
211
+ # :longitude=>0.630485817275861,
212
+ # :latitude=>51.7920493205979,
213
+ # :distance=>54.12525282
214
+ # },
215
+ ```
216
+
217
+ **Notes**
218
+
219
+ Data source: Ordnance Survey. Free to use.
220
+
221
+
222
+
223
+ ## Retrieve an address using UDPRN [(docs)](https://ideal-postcodes.co.uk/documentation/addresses#address)
224
+
225
+ Retrieve the specific address for a specific UDPRN.
226
+
227
+ ```ruby
228
+ IdealPostcodes::Address.lookup udprn
229
+ ```
230
+
231
+ **Arguments**
232
+
233
+ - `udprn` (string | number). A number which uniquely identifies the address.
234
+
235
+ **Returns**
236
+
237
+ Returns a hash representing the matching address. Returns `nil` if no matching address is found.
238
+
239
+ **Example**
240
+
241
+ ```ruby
242
+ IdealPostcodes::Address.lookup 23747771
243
+
244
+ #{
245
+ # :line_1=>"Prime Minister & First Lord Of The Treasury",
246
+ # :line_2=>"10 Downing Street",
247
+ # :line_3=>"",
248
+ # :post_town=>"LONDON",
249
+ # :postcode=>"SW1A 2AA",
250
+ # :organisation_name=>"Prime Minister & First Lord Of The Treasury",
251
+ # :premise=>"10",
252
+ # :latitude=>51.5035398826274
253
+ # :longitude=>-0.127695242183412,
254
+ # :thoroughfare=>"Downing Street",
255
+ # :district=>"Westminster",
256
+ # :ward=>"St James's",
257
+ # :building_number=>"10",
258
+ # :udprn=>23747771,
259
+ # ... and so on
260
+ ```
261
+
262
+ **Notes**
263
+
264
+ Data source: Royal Mail Postcode Address File, Ordnance Survey.
265
+
266
+ Use the address `0` to test integration for free. The complete list of test methods is available in the [documentation](https://ideal-postcodes/documentation/addresses).
267
+
268
+ ## Utility Methods
269
+
270
+ Listed below are free utility methods, e.g. finding the status of your key.
271
+
272
+ ### Find out if your key is in a usable state [(docs)](https://ideal-postcodes.co.uk/documentation/keys#key)
273
+
274
+ Find out if your key is in a usable state. E.g. it has a positive balance, it is currently under your defined usage limits, etc.
275
+
276
+ ```
277
+ IdealPostcodes.key_available
278
+ ```
279
+
280
+ **Arguments**
281
+
282
+ None.
283
+
284
+ **Returns**
285
+
286
+ - availability (Boolean). Returns true if key can be used. False if something is preventing the key from making lookups e.g. insufficient balance, reached limits, etc.
287
+
288
+ **Example**
289
+
290
+ ```ruby
291
+ IdealPostcodes.key_available # => true, you're clear to make lookups
292
+ ```
293
+
294
+ ### Retrieve private key information [(docs)](https://ideal-postcodes.co.uk/documentation/keys#details)
295
+
296
+ This method reveals private information about your key such as the lookup balance, whitelisted URLs, etc. Note: a secret key is required to invoke this method.
297
+
298
+ ```
299
+ IdealPostcodes.key_details
300
+ ```
301
+
302
+ **Arguments**
303
+
304
+ None.
305
+
306
+ **Returns**
307
+
308
+ Returns a hash containing pertinent private information about your key.
309
+
310
+ **Example**
311
+
312
+ ```ruby
313
+ IdealPostcodes.key_details
314
+
315
+ # {
316
+ # :lookups_remaining=>9678,
317
+ # :daily_limit=>{
318
+ # :limit=>100,
319
+ # :consumed=>1
320
+ # },
321
+ # :individual_limit=>{
322
+ # :limit=>15
323
+ # },
324
+ # :allowed_urls=>["foo.com"],
325
+ # :notifications=>{
326
+ # :emails=>["bar@baz.com"],
327
+ # :enabled=>true},
328
+ # :automated_topups=>{
329
+ # :enabled=>true
330
+ # }
331
+ # }
332
+ ```
333
+
334
+ If you intend to use this method, you must pass your secret key (which can be found on your [account page](https://ideal-postcodes.co.uk/account)) along with your API key when instantiating the client. E.g.
335
+
336
+ ```ruby
337
+ IdealPostcodes.apply_secret "your secret key"
338
+ ```
339
+
340
+ Do not share your secret key and avoid commiting this key to your codebase.
96
341
 
97
342
  ## Testing
98
343
 
99
344
  ```
100
- bundle exec rake test
345
+ bundle exec rake
101
346
  ```
102
347
 
103
348
  ## License
104
- MIT
349
+
350
+ MIT
data/Rakefile CHANGED
@@ -1,8 +1,15 @@
1
- require 'rake'
2
- require 'rake/testtask'
1
+ begin
2
+ require 'rspec/core/rake_task'
3
3
 
4
- Rake::TestTask.new do |t|
5
- t.libs << "test"
6
- t.test_files = FileList['test/test*.rb']
7
- t.verbose = true
4
+ RSpec::Core::RakeTask.new(:spec) do |t|
5
+ t.rspec_opts = "--format documentation"
6
+ end
7
+
8
+ task :default => :spec
9
+ rescue LoadError
10
+ # no rspec available
11
+ end
12
+
13
+ task :eject_tapes do
14
+ `rm spec/vcr_cassettes/*.yml`
8
15
  end
@@ -6,20 +6,20 @@ spec = Gem::Specification.new do |s|
6
6
  s.name = 'ideal_postcodes'
7
7
  s.version = IdealPostcodes::VERSION
8
8
  s.summary = 'Wrapper for the Ideal-Postcodes.co.uk API'
9
- s.description = 'Ideal Postcodes is a simple postcode lookup API for UK addresses. See https://ideal-postcodes.co.uk'
10
- s.authors = ['Chris Blanchard']
11
- s.email = ['cablanchard@gmail.com']
12
- s.homepage = 'https://ideal-postcodes.co.uk/documentation'
9
+ s.description = 'Ideal Postcodes is a simple postcode lookup API for UK addresses. See https://ideal-postcodes.co.uk'
10
+ s.authors = ['Ideal Postcodes']
11
+ s.email = ['support@ideal-postcodes.co.uk']
12
+ s.homepage = 'https://ideal-postcodes.co.uk/'
13
+ s.licenses = ['MIT']
13
14
 
14
- s.add_dependency('rest-client', '~> 1.6')
15
- s.add_dependency('multi_json', '~> 1.7.9')
15
+ s.add_dependency('rest-client', '>= 1.8', '< 3.0')
16
16
 
17
- s.add_development_dependency('mocha', '~> 0.14.0')
18
- s.add_development_dependency('test-unit')
19
- s.add_development_dependency('shoulda', '~> 3.5.0')
20
- s.add_development_dependency('rake')
17
+ s.add_development_dependency 'rake', '~> 10.1'
18
+ s.add_development_dependency 'vcr', '~> 2.9'
19
+ s.add_development_dependency 'rspec', '~> 3.1'
20
+ s.add_development_dependency 'webmock', '~> 2.3.1'
21
21
 
22
22
  s.files = `git ls-files`.split("\n")
23
- s.test_files = `git ls-files -- test/*`.split("\n")
23
+ s.test_files = `git ls-files -- spec/*`.split("\n")
24
24
  s.require_paths = ['lib']
25
25
  end
@@ -1,2 +1,2 @@
1
1
  # Allows to reference by ideal_postcodes
2
- require 'idealpostcodes'
2
+ require 'idealpostcodes'
@@ -0,0 +1,31 @@
1
+ module IdealPostcodes
2
+ module Address
3
+ class SearchResult
4
+ attr_reader :page, :limit, :addresses
5
+ def initialize(response)
6
+ @page = response[:result][:page]
7
+ @limit = response[:result][:limit]
8
+ @addresses = response[:result][:hits]
9
+ end
10
+ end
11
+
12
+ def self.lookup(udprn)
13
+ begin
14
+ response = IdealPostcodes.request :get, "addresses/#{udprn}"
15
+ address = response[:result]
16
+ rescue IdealPostcodes::IdealPostcodesError => error
17
+ raise error unless error.response_code == 4044
18
+ address = nil
19
+ end
20
+ address
21
+ end
22
+
23
+ def self.search(search_term, options = {})
24
+ query = { query: search_term }
25
+ query[:limit] = options[:limit] unless options[:limit].nil?
26
+ query[:page] = options[:page] unless options[:page].nil?
27
+ response = IdealPostcodes.request :get, 'addresses', query
28
+ SearchResult.new response
29
+ end
30
+ end
31
+ end
@@ -1,33 +1,38 @@
1
1
  module IdealPostcodes
2
- class IdealPostcodesError < StandardError
3
- attr_reader :message
4
- attr_reader :http_code
5
- attr_reader :http_body
6
- attr_reader :response_code
2
+ class IdealPostcodesError < StandardError
3
+ attr_reader :message
4
+ attr_reader :http_code
5
+ attr_reader :http_body
6
+ attr_reader :response_code
7
7
 
8
- def initialize(message = nil, http_code = nil, http_body = nil, response_code = nil)
9
- @message = message
10
- @http_code = http_code
11
- @http_body = http_body
12
- @response_code = response_code
13
- end
8
+ def initialize(
9
+ message = nil,
10
+ http_code = nil,
11
+ http_body = nil,
12
+ response_code = nil
13
+ )
14
+ @message = message
15
+ @http_code = http_code
16
+ @http_body = http_body
17
+ @response_code = response_code
18
+ end
14
19
 
15
- def to_s
16
- status = @http_code.nil? ? "" : "#{@http_code} error."
17
- ideal_code = @response_code.nil? ? "" : "(#{@response_code})"
18
- "#{status} error. (#{ideal_code}) #{message}"
19
- end
20
- end
20
+ def to_s
21
+ status = @http_code.nil? ? '' : "#{@http_code} error."
22
+ ideal_code = @response_code.nil? ? '' : "(#{@response_code})"
23
+ "#{status} error. (#{ideal_code}) #{message}"
24
+ end
25
+ end
21
26
 
22
- class AuthenticationError< IdealPostcodesError
23
- end
27
+ class AuthenticationError < IdealPostcodesError
28
+ end
24
29
 
25
- class TokenExhaustedError < IdealPostcodesError
26
- end
30
+ class TokenExhaustedError < IdealPostcodesError
31
+ end
27
32
 
28
- class LimitReachedError < IdealPostcodesError
29
- end
33
+ class LimitReachedError < IdealPostcodesError
34
+ end
30
35
 
31
- class ResourceNotFoundError < IdealPostcodesError
32
- end
33
- end
36
+ class ResourceNotFoundError < IdealPostcodesError
37
+ end
38
+ end
@@ -0,0 +1,14 @@
1
+ module IdealPostcodes
2
+ module Key
3
+ def self.lookup(api_key)
4
+ response = IdealPostcodes.request :get, "keys/#{api_key}"
5
+ response[:result]
6
+ end
7
+
8
+ def self.lookup_details(api_key, secret)
9
+ response =
10
+ IdealPostcodes.request :get, "keys/#{api_key}", { user_token: secret }
11
+ response[:result]
12
+ end
13
+ end
14
+ end