postcodeinfo-client-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,225 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe PostcodeInfo::Postcode do
4
+ let(:api_client){ double('api client', make_request: nil) }
5
+ let(:postcode){ described_class.new('SN51NB', api_client) }
6
+
7
+ describe 'constructing a Postcode' do
8
+ let(:postcode){ 'SN51NB' }
9
+
10
+ context 'that is valid' do
11
+ context 'and does not contain whitespace' do
12
+ let(:postcode){ 'YO165RB' }
13
+
14
+ it 'does not raise an error' do
15
+ expect{described_class.new(postcode, api_client)}.to_not raise_error
16
+ end
17
+ end
18
+ context 'and contains whitespace' do
19
+ let(:postcode){ 'N2 8AS'}
20
+
21
+ it 'does not raise an error' do
22
+ expect{described_class.new(postcode, api_client)}.to_not raise_error
23
+ end
24
+ end
25
+
26
+ it 'does not make an API request' do
27
+ expect( api_client ).to_not receive(:make_request)
28
+ described_class.new(postcode, api_client)
29
+ end
30
+
31
+ it 'stores a normalised version of the given postcode ' do
32
+ expect(described_class.new(" AB12 4DE\n", api_client).normalised).to eq('ab124de')
33
+ end
34
+ end
35
+
36
+ context 'that is invalid' do
37
+ let(:postcode){ 'd0nk 3y5' }
38
+
39
+ it 'does not raise an error' do
40
+ expect{described_class.new(postcode, api_client)}.to_not raise_error
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ describe 'valid?' do
47
+ let(:postcode){ described_class.new('SN51NB', api_client) }
48
+
49
+ context 'when the instance variable is nil' do
50
+ before do
51
+ postcode.send(:instance_variable_set, '@valid', nil)
52
+ end
53
+
54
+ it 'performs a lookup on the API' do
55
+ expect( api_client ).to receive(:valid?)
56
+ postcode.valid?
57
+ end
58
+
59
+ it 'stores the return value from the API in the instance variable' do
60
+ allow(api_client).to receive(:valid?).and_return('foo')
61
+ expect(postcode.valid?).to eq('foo')
62
+ end
63
+ end
64
+
65
+ context 'when the instance variable has a value' do
66
+ before do
67
+ postcode.send(:instance_variable_set, '@valid', 'bar')
68
+ end
69
+
70
+ it 'does not perform a lookup on the API' do
71
+ expect( api_client ).to_not receive(:valid?)
72
+ postcode.valid?
73
+ end
74
+
75
+ it 'returns the existing value' do
76
+ expect(postcode.valid?).to eq('bar')
77
+ end
78
+ end
79
+ end
80
+
81
+
82
+ describe 'addresses' do
83
+ let(:postcode){ described_class.new('SN51NB', api_client) }
84
+
85
+ context 'when the instance variable is nil' do
86
+ before do
87
+ postcode.send(:instance_variable_set, '@addresses', nil)
88
+ end
89
+
90
+ it 'performs a lookup on the API' do
91
+ expect( api_client ).to receive(:addresses)
92
+ postcode.addresses
93
+ end
94
+
95
+ it 'stores the return value from the API in the instance variable' do
96
+ allow(api_client).to receive(:addresses).and_return('foo')
97
+ expect(postcode.addresses).to eq('foo')
98
+ end
99
+ end
100
+
101
+ context 'when the instance variable has a value' do
102
+ before do
103
+ postcode.send(:instance_variable_set, '@addresses', 'bar')
104
+ end
105
+
106
+ it 'does not perform a lookup on the API' do
107
+ expect( api_client ).to_not receive(:addresses)
108
+ postcode.addresses
109
+ end
110
+
111
+ it 'returns the existing value' do
112
+ expect(postcode.addresses).to eq('bar')
113
+ end
114
+ end
115
+ end
116
+
117
+ [:longitude, :latitude].each do |method|
118
+ describe method do
119
+ let(:postcode){ PostcodeInfo::Postcode.new('SN51NB', api_client) }
120
+ before do
121
+ postcode.send(:instance_variable_set, "@#{method}", method.to_s)
122
+ end
123
+
124
+ context 'when the @info instance variable is nil' do
125
+ before do
126
+ postcode.send(:instance_variable_set, '@info', nil)
127
+ end
128
+
129
+ it 'performs a lookup on the API' do
130
+ expect( postcode ).to receive(:lookup_info!)
131
+ postcode.send(method)
132
+ end
133
+ end
134
+
135
+ context 'when the @info instance variable has a value' do
136
+ before do
137
+ postcode.send(:instance_variable_set, '@info', 'info')
138
+ end
139
+
140
+ it 'does not perform a lookup on the API' do
141
+ expect( postcode ).to_not receive(:lookup_info!)
142
+ postcode.send(method)
143
+ end
144
+ end
145
+
146
+ it "returns the value of @#{method}" do
147
+ allow(postcode).to receive(:lookup_info!)
148
+ expect(postcode.send(method)).to eq(method.to_s)
149
+ end
150
+ end
151
+ end
152
+
153
+ describe "lookup_info!" do
154
+ before do
155
+ allow(api_client).to receive(:info).and_return('info')
156
+ end
157
+
158
+ it "gets info from the client" do
159
+ expect(api_client).to receive(:info)
160
+ allow(postcode).to receive(:parse_info!)
161
+ postcode.lookup_info!
162
+ end
163
+
164
+ it "stores the info returned from the client" do
165
+ allow(postcode).to receive(:parse_info!)
166
+ postcode.lookup_info!
167
+ expect(postcode.send(:instance_variable_get, '@info')).to eq('info')
168
+ end
169
+
170
+ it 'parses the info' do
171
+ expect(postcode).to receive(:parse_info!)
172
+ postcode.lookup_info!
173
+ end
174
+ end
175
+
176
+ describe "parse_info!" do
177
+ let(:stored_info){ {key: 'value', local_authority:'local authority'} }
178
+
179
+ before do
180
+ postcode.send(:instance_variable_set, '@info', stored_info )
181
+ end
182
+
183
+ it 'sets coordinates with @info' do
184
+ expect(postcode).to receive(:set_coordinates!).with(stored_info)
185
+ postcode.send(:parse_info!)
186
+ end
187
+
188
+ it 'stores the local_authority in @local_authority' do
189
+ postcode.send(:parse_info!)
190
+ expect(postcode.local_authority).to eq('local authority')
191
+ end
192
+
193
+ it 'sets the valid flag' do
194
+ expect{postcode.send(:parse_info!)}.to change{postcode.send(:instance_variable_get, '@valid')}.to(true)
195
+ end
196
+ end
197
+
198
+ describe 'set_coordinates!' do
199
+ context 'when the given info has a :centre key' do
200
+ let(:info){ {centre: {latitude: 1.0, longitude: 256.8}} }
201
+
202
+ it 'stores the latitude sub-key as @latitude' do
203
+ postcode.send(:set_coordinates!, info)
204
+ expect(postcode.send(:instance_variable_get, '@latitude')).to eq(1.0)
205
+ end
206
+
207
+ it 'stores the longitude sub-key as @longitude' do
208
+ postcode.send(:set_coordinates!, info)
209
+ expect(postcode.send(:instance_variable_get, '@longitude')).to eq(256.8)
210
+ end
211
+ end
212
+
213
+ context 'when the given info does not have :centre' do
214
+ let(:info){ {} }
215
+
216
+ it 'does not change @latitude' do
217
+ expect{ postcode.send(:set_coordinates!, info) }.to_not change{ postcode.send(:instance_variable_get, '@latitude') }
218
+ end
219
+ it 'does not change @longitude' do
220
+ expect{ postcode.send(:set_coordinates!, info) }.to_not change{ postcode.send(:instance_variable_get, '@longitude') }
221
+ end
222
+
223
+ end
224
+ end
225
+ end
@@ -0,0 +1,22 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+
5
+
6
+ SimpleCov.configure do
7
+ load_adapter 'test_frameworks'
8
+ end
9
+
10
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
11
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
12
+
13
+ require 'rspec'
14
+ require 'postcodeinfo-client-ruby'
15
+
16
+ # Requires supporting files with custom matchers and macros, etc,
17
+ # in ./support/ and its subdirectories.
18
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
19
+
20
+ RSpec.configure do |config|
21
+
22
+ end
metadata ADDED
@@ -0,0 +1,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postcodeinfo-client-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Al Davidson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rdoc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.12'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.12'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: jeweler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.0.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.0.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: |-
126
+ Provides a convenient interface for looking up postcodes in an instance of the MoJ postcodeinfo API (https://://github.com/ministryofjustice/postcodeinfo).
127
+ Lets you check if a postcode is valid, and ookup:
128
+ * all addresses with that postcode
129
+ * the latitude/longitude of its centre point
130
+ * the name & GSS code of the local authority under which it resides.
131
+ email: alistair.davidson@digital.justice.gov.uk
132
+ executables: []
133
+ extensions: []
134
+ extra_rdoc_files:
135
+ - LICENSE.MD
136
+ - README.md
137
+ files:
138
+ - ".document"
139
+ - ".rspec"
140
+ - ".ruby-gemset"
141
+ - ".ruby-version"
142
+ - ".simplecov"
143
+ - Gemfile
144
+ - Gemfile.lock
145
+ - LICENSE.MD
146
+ - README.md
147
+ - Rakefile
148
+ - VERSION
149
+ - lib/postcodeinfo-client-ruby.rb
150
+ - lib/postcodeinfo/client.rb
151
+ - lib/postcodeinfo/exceptions.rb
152
+ - lib/postcodeinfo/postcode.rb
153
+ - postcodeinfo-client-ruby.gemspec
154
+ - spec/client_spec.rb
155
+ - spec/postcode_spec.rb
156
+ - spec/spec_helper.rb
157
+ homepage: https://github.com/ministryofjustice/postcodeinfo-client-ruby
158
+ licenses:
159
+ - Open Government License
160
+ metadata: {}
161
+ post_install_message:
162
+ rdoc_options: []
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ requirements: []
176
+ rubyforge_project:
177
+ rubygems_version: 2.4.3
178
+ signing_key:
179
+ specification_version: 4
180
+ summary: Client for postcodeinfo API (https://://github.com/ministryofjustice/postcodeinfo)
181
+ test_files: []