firstclasspostcodes 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/images/cover.png +0 -0
- data/.github/workflows/gem.yml +3 -1
- data/README.md +179 -0
- data/lib/firstclasspostcodes/configuration.rb +1 -1
- data/lib/firstclasspostcodes/version.rb +1 -1
- data/spec/firstclasspostcodes/integration_spec.rb +30 -0
- data/spec/firstclasspostcodes_spec.rb +3 -3
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c7ef5f006a609e8cc2a1a7e640df790a62ffcc58f2902bda292f656713864b6
|
4
|
+
data.tar.gz: 7807d503f9aa8aaa3b0de5645afa654229a27c4b8d15076390a82a3023ccfdf2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e27e22f9d6121d354e2fe10b4fff29a436fb5df7f19179fe97378b9341ccb645fbb6ad24e7c5c77b9b65e76a9d04e360629e647a67319170d2858af1fd4b0c5
|
7
|
+
data.tar.gz: 49a18424d3dc1dafbd502569209702fd91c7212c3a96534d590c87dd6d91956c2ed2777f396420eb85cc52b194495625ced803bb09ee1443984d599957459f0c
|
Binary file
|
data/.github/workflows/gem.yml
CHANGED
data/README.md
ADDED
@@ -0,0 +1,179 @@
|
|
1
|
+
![Cover](/.github/images/cover.png)
|
2
|
+
|
3
|
+
# Firstclasspostcodes
|
4
|
+
The Firstclasspostcodes Ruby library provides convenient access to the Firstclasspostcodes API from applications written in the Ruby language. It includes pre-defined methods and helpers to help you easily integrate the library into any application.
|
5
|
+
|
6
|
+
The library also provides other features. For example:
|
7
|
+
|
8
|
+
* Easy configuration path for fast setup and use.
|
9
|
+
* Helpers for listing and formatting addresses.
|
10
|
+
* Built-in methods for easily interacting with the Firstclasspostcodes API.
|
11
|
+
|
12
|
+
## Documentation
|
13
|
+
See [Ruby API docs](https://docs.firstclasspostcodes.com/ruby/getting-started) for detailed usage and examples.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
You don't need this source code unless you want to modify the gem. If you just want to use the package, just run:
|
17
|
+
|
18
|
+
```
|
19
|
+
gem install firstclasspostcodes
|
20
|
+
```
|
21
|
+
|
22
|
+
Or alternatively, install from the GitHub package registry:
|
23
|
+
|
24
|
+
```
|
25
|
+
gem install firstclasspostcodes --source "https://rubygems.pkg.github.com/firstclasspostcodes"
|
26
|
+
```
|
27
|
+
|
28
|
+
If you want to build the gem from source:
|
29
|
+
|
30
|
+
```
|
31
|
+
gem build firstclasspostcodes.gemspec
|
32
|
+
```
|
33
|
+
|
34
|
+
## Requirements
|
35
|
+
|
36
|
+
* Ruby 2.3+
|
37
|
+
* An API key from [https://firstclasspostcodes.com](https://firstclasspostcodes.com)
|
38
|
+
|
39
|
+
## Bundler
|
40
|
+
|
41
|
+
If you are installing via bundler, you should be sure to use the https rubygems source in your Gemfile, as any gems fetched over http could potentially be compromised in transit and alter the code of gems fetched securely over https:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
source 'https://rubygems.org'
|
45
|
+
|
46
|
+
gem 'rails'
|
47
|
+
gem 'firstclasspostcodes'
|
48
|
+
```
|
49
|
+
|
50
|
+
We also host this gem on the [GitHub Package Registry](https://github.com/firstclasspostcodes/firstclasspostcodes-ruby/packages/108443):
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
source "https://rubygems.pkg.github.com/firstclasspostcodes" do
|
54
|
+
gem "firstclasspostcodes", "0.1.0"
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
## Usage
|
59
|
+
Once you have configured the library, initialise a client and start calling the API.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
require 'firstclasspostcodes'
|
63
|
+
require 'json'
|
64
|
+
|
65
|
+
Firstclasspostcodes.configure do |c|
|
66
|
+
c.api_key = 'sw34567ujbvcd'
|
67
|
+
end
|
68
|
+
|
69
|
+
client = Firstclasspostcodes::Client.new
|
70
|
+
|
71
|
+
response = client.get_postcode('SW13 6HY')
|
72
|
+
|
73
|
+
puts JSON.pretty_generate(response)
|
74
|
+
```
|
75
|
+
|
76
|
+
## Configuration
|
77
|
+
The library can be configured with several options depending on the requirements of your setup:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
require 'firstclasspostcodes'
|
81
|
+
|
82
|
+
API_KEY = 'werthj75rtfgds'
|
83
|
+
|
84
|
+
Firstclasspostcodes.configure do |c|
|
85
|
+
# The API Key to be used when sending requests to the
|
86
|
+
# Firstclasspostcodes API
|
87
|
+
c.api_key = API_KEY
|
88
|
+
|
89
|
+
# The host to send API requests to. This is typically changed
|
90
|
+
# to use the mock service for testing purposes
|
91
|
+
c.host = "api.firstclasspostcodes.com"
|
92
|
+
|
93
|
+
# The default content type is json, but can be changed to "geo+json"
|
94
|
+
# to return responses as GeoJSON content type
|
95
|
+
c.content = "json"
|
96
|
+
|
97
|
+
# Typically, always HTTPS, but useful to change for testing
|
98
|
+
# purposes
|
99
|
+
c.protocol = "https"
|
100
|
+
|
101
|
+
# The base path is "/data", but useful to change for testing
|
102
|
+
# purposes
|
103
|
+
c.base_path = "/data"
|
104
|
+
|
105
|
+
# The default logger being used with the library. This defaults
|
106
|
+
# to the Rails logger (if it is available)
|
107
|
+
c.logger = Logger.new(STDOUT)
|
108
|
+
|
109
|
+
# Output helpful debug statements to STDOUT
|
110
|
+
c.debug = false
|
111
|
+
|
112
|
+
# The default request timeout for the library.
|
113
|
+
c.timeout = 30
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+
## Events
|
118
|
+
You can subscribe to events using an initialized client, using the Ruby block pattern.
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
require 'firstclasspostcodes'
|
122
|
+
require 'json'
|
123
|
+
|
124
|
+
client = Firstclasspostcodes::Client.new
|
125
|
+
|
126
|
+
client.on(:request) do |request_params|
|
127
|
+
puts JSON.pretty_generate(request_params)
|
128
|
+
end
|
129
|
+
```
|
130
|
+
|
131
|
+
| Event name | Description |
|
132
|
+
|:-----|:-----|
|
133
|
+
| `request` | Triggered before a request is sent. The request object to be sent is passed to the event handler. |
|
134
|
+
| `response` | Triggered with the parsed JSON response body upon a successful reques. |
|
135
|
+
| `error` | Triggered with a client error when the request fails. |
|
136
|
+
| `operation:{name}` | Triggered by an operation with the parameter object. |
|
137
|
+
|
138
|
+
**Note:** `{name}` is replaced with the operation name of the method, as defined inside the OpenAPI specification.
|
139
|
+
|
140
|
+
## Debugging
|
141
|
+
Debugging through logging can be enabled inside the configuration (demonstrated below). A custom logger can also be assigned if required.
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
require 'firstclasspostcodes'
|
145
|
+
require 'logger'
|
146
|
+
|
147
|
+
custom_logger = Logger.new(STDOUT)
|
148
|
+
custom_logger.level = Logger::DEBUG
|
149
|
+
|
150
|
+
Firstclasspostcodes.configure do |c|
|
151
|
+
c.debug = true
|
152
|
+
c.logger = custom_logger
|
153
|
+
end
|
154
|
+
```
|
155
|
+
|
156
|
+
## Integration / Testing
|
157
|
+
We provide a mock service of our API as a docker container [available here](https://github.com/firstclasspostcodes/firstclasspostcodes-mock). Once the container is running, the library can be easily configured to use it:
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
require "firstclasspostcodes"
|
161
|
+
require "uri"
|
162
|
+
|
163
|
+
MOCK_API_URL = "http://localhost:3000"
|
164
|
+
|
165
|
+
MOCK_API_KEY = "111111111111"
|
166
|
+
|
167
|
+
uri = URI.parse(MOCK_API_URL)
|
168
|
+
|
169
|
+
Firstclasspostcodes.configure do |c|
|
170
|
+
# The mock API key is always 111111111111 ("12x1")
|
171
|
+
c.api_key = MOCK_API_KEY
|
172
|
+
|
173
|
+
# Alter the configuration to use the mock service
|
174
|
+
c.base_path = uri.path
|
175
|
+
c.protocol = uri.scheme
|
176
|
+
c.host = uri.host
|
177
|
+
c.host = "#{uri.host}:#{uri.port}" if uri.port
|
178
|
+
end
|
179
|
+
```
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
require_relative "../support/events_examples.rb"
|
7
|
+
|
8
|
+
DATA_PATH = "/data/.postcodes"
|
9
|
+
|
10
|
+
describe Firstclasspostcodes::Client do
|
11
|
+
subject { Firstclasspostcodes::Client.new }
|
12
|
+
|
13
|
+
let(:fixtures) { subject.request(method: :get, path: DATA_PATH) }
|
14
|
+
|
15
|
+
let(:fixture) { fixtures.sample }
|
16
|
+
|
17
|
+
describe "#get_postcode" do
|
18
|
+
it "retrieves the postcode correctly" do
|
19
|
+
response = subject.get_postcode(fixture[:postcode])
|
20
|
+
expect(response[:postcode]).to eq(fixture[:postcode])
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#get_lookup" do
|
25
|
+
it "retrieves postcodes by geolocation correctly" do
|
26
|
+
response = subject.get_lookup(fixture)
|
27
|
+
expect(response[0][:postcode]).to eq(fixture[:postcode])
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -95,9 +95,9 @@ describe Firstclasspostcodes do
|
|
95
95
|
let(:config) { Firstclasspostcodes::Configuration.new }
|
96
96
|
|
97
97
|
it "defaults to 0" do
|
98
|
-
expect(Firstclasspostcodes::Configuration.default.timeout).to eq(
|
99
|
-
expect(config.timeout).to eq(
|
100
|
-
expect(config.to_request_params[:timeout]).to eq(
|
98
|
+
expect(Firstclasspostcodes::Configuration.default.timeout).to eq(30)
|
99
|
+
expect(config.timeout).to eq(30)
|
100
|
+
expect(config.to_request_params[:timeout]).to eq(30)
|
101
101
|
end
|
102
102
|
|
103
103
|
it "can be customized" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: firstclasspostcodes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Firstclasspostcodes
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-01-
|
11
|
+
date: 2020-01-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -61,12 +61,14 @@ extensions: []
|
|
61
61
|
extra_rdoc_files: []
|
62
62
|
files:
|
63
63
|
- ".dependabot/config.yml"
|
64
|
+
- ".github/images/cover.png"
|
64
65
|
- ".github/workflows/gem.yml"
|
65
66
|
- ".gitignore"
|
66
67
|
- ".rspec"
|
67
68
|
- ".rubocop.yml"
|
68
69
|
- Gemfile
|
69
70
|
- LICENSE
|
71
|
+
- README.md
|
70
72
|
- Rakefile
|
71
73
|
- firstclasspostcodes.gemspec
|
72
74
|
- lib/firstclasspostcodes.rb
|
@@ -82,6 +84,7 @@ files:
|
|
82
84
|
- lib/firstclasspostcodes/version.rb
|
83
85
|
- spec/firstclasspostcodes/client_spec.rb
|
84
86
|
- spec/firstclasspostcodes/events_spec.rb
|
87
|
+
- spec/firstclasspostcodes/integration_spec.rb
|
85
88
|
- spec/firstclasspostcodes/operations/get_lookup_spec.rb
|
86
89
|
- spec/firstclasspostcodes/operations/get_postcode_spec.rb
|
87
90
|
- spec/firstclasspostcodes/operations/methods/format_address_spec.rb
|