Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
postcodes-io-java
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
James Coyle
postcodes-io-java
Commits
a4d1e035
Commit
a4d1e035
authored
Aug 14, 2016
by
Deepak Prabhakar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Made PostcodeLookup JSON fetchable for single and multiple postcode.
parent
a5902013
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
206 additions
and
2 deletions
+206
-2
pom.xml
pom.xml
+11
-0
src/main/java/com/postcode/io/initializers/PostcodeLookup.java
...ain/java/com/postcode/io/initializers/PostcodeLookup.java
+60
-2
src/test/java/com/postcode/io/initializers/PostcodeLookupTest.java
...java/com/postcode/io/initializers/PostcodeLookupTest.java
+25
-0
src/test/resources/postcodeLookupViaPOST.json
src/test/resources/postcodeLookupViaPOST.json
+110
-0
No files found.
pom.xml
View file @
a4d1e035
...
...
@@ -61,6 +61,12 @@
<version>
4.12
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.skyscreamer
</groupId>
<artifactId>
jsonassert
</artifactId>
<version>
1.3.0
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.eluder.coveralls
</groupId>
<artifactId>
coveralls-maven-plugin
</artifactId>
...
...
@@ -81,5 +87,10 @@
<artifactId>
json
</artifactId>
<version>
20160212
</version>
</dependency>
<dependency>
<groupId>
com.mashape.unirest
</groupId>
<artifactId>
unirest-java
</artifactId>
<version>
1.4.9
</version>
</dependency>
</dependencies>
</project>
src/main/java/com/postcode/io/initializers/PostcodeLookup.java
View file @
a4d1e035
...
...
@@ -3,6 +3,13 @@ package com.postcode.io.initializers;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
org.codehaus.plexus.util.StringUtils
;
import
org.json.JSONArray
;
import
org.json.JSONObject
;
import
com.postcode.io.PostCode
;
import
com.postcode.io.json.JsonFetcher
;
/**
* Lookup a postcode
*
...
...
@@ -15,11 +22,14 @@ public class PostcodeLookup {
private
String
postcode
;
private
String
[]
postcodes
;
private
String
url
;
private
PostcodeLookup
(
URL
url
,
String
postcode
)
{
private
PostcodeLookup
(
URL
url
,
String
postcode
,
String
[]
postcodes
)
{
this
.
url
=
url
.
toString
();
this
.
postcode
=
postcode
;
this
.
postcodes
=
postcodes
;
}
public
String
getPostcode
()
{
...
...
@@ -40,17 +50,65 @@ public class PostcodeLookup {
private
String
postcode
;
private
String
[]
postcodes
;
private
JSONObject
json
;
public
Builder
()
{
}
/**
* Use this to create {@link PostCode}<br/>
* Pass this to {@link PostCode#generate}
*
* @return
* @throws MalformedURLException
*/
public
PostcodeLookup
build
()
throws
MalformedURLException
{
return
new
PostcodeLookup
(
new
URL
(
LOOKUP_URL
.
toString
().
concat
(
postcode
)),
postcode
);
return
new
PostcodeLookup
(
new
URL
(
LOOKUP_URL
.
toString
().
concat
(
postcode
)),
postcode
,
postcodes
);
}
public
JSONObject
asJson
()
throws
Exception
{
if
(!
StringUtils
.
isEmpty
(
postcode
))
{
return
JsonFetcher
.
urlToJson
(
new
URL
(
LOOKUP_URL
.
toString
().
concat
(
postcode
)));
}
else
if
(
json
!=
null
)
{
return
JsonFetcher
.
postURLToJson
(
new
URL
(
LOOKUP_URL
.
toString
()),
json
);
}
else
{
throw
new
IllegalArgumentException
();
}
}
/**
* Use if information required is for only one {@link PostCode}
*
* @param postcode
* @return
*/
public
Builder
postcode
(
String
postcode
)
{
this
.
postcode
=
postcode
;
return
this
;
}
/**
* Use if information required is for multiple {@link PostCode}
*
* @param postcodes
* @return
*/
public
Builder
postcodes
(
String
[]
postcodes
)
{
this
.
postcodes
=
postcodes
;
createJsonPostcodes
(
postcodes
);
return
this
;
}
private
void
createJsonPostcodes
(
String
[]
postcodes
)
{
JSONObject
json
=
new
JSONObject
();
JSONArray
jsonArray
=
new
JSONArray
();
for
(
String
string
:
postcodes
)
{
jsonArray
.
put
(
string
);
}
this
.
json
=
json
.
put
(
"postcodes"
,
jsonArray
);
}
}
}
src/test/java/com/postcode/io/initializers/PostcodeLookupTest.java
View file @
a4d1e035
...
...
@@ -2,9 +2,15 @@ package com.postcode.io.initializers;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
java.io.File
;
import
java.net.MalformedURLException
;
import
org.json.JSONException
;
import
org.junit.Test
;
import
org.skyscreamer.jsonassert.JSONAssert
;
import
org.skyscreamer.jsonassert.JSONCompareMode
;
import
com.postcode.io.json.JsonFetcher
;
/**
* @author Deepak
...
...
@@ -19,4 +25,23 @@ public class PostcodeLookupTest {
assertEquals
(
postcodeLookup
.
getUrl
(),
"http://api.postcodes.io/postcodes/bs347np"
);
}
@Test
public
void
testPostCodeLookupAsJsonForSinglePostcode
()
throws
JSONException
,
MalformedURLException
,
Exception
{
JSONAssert
.
assertEquals
(
JsonFetcher
.
urlToJson
(
new
File
(
System
.
getProperty
(
"user.dir"
).
concat
(
"/src/test/resources/postcodeLookup.json"
))
.
toURI
().
toURL
()),
new
PostcodeLookup
.
Builder
().
postcode
(
"bs347np"
).
asJson
(),
JSONCompareMode
.
STRICT
);
}
@Test
public
void
testPostCodeLookupAsJsonForMultiplePostcodes
()
throws
JSONException
,
MalformedURLException
,
Exception
{
JSONAssert
.
assertEquals
(
JsonFetcher
.
urlToJson
(
new
File
(
System
.
getProperty
(
"user.dir"
).
concat
(
"/src/test/resources/postcodeLookupViaPOST.json"
))
.
toURI
().
toURL
()),
new
PostcodeLookup
.
Builder
().
postcodes
(
new
String
[]
{
"OX49 5NU"
,
"M32 0JG"
,
"NE30 1DP"
}).
asJson
(),
JSONCompareMode
.
LENIENT
);
}
}
\ No newline at end of file
src/test/resources/postcodeLookupViaPOST.json
0 → 100644
View file @
a4d1e035
{
"status"
:
200
,
"result"
:
[
{
"query"
:
"M32 0JG"
,
"result"
:
{
"postcode"
:
"M32 0JG"
,
"quality"
:
1
,
"eastings"
:
379988
,
"northings"
:
395476
,
"country"
:
"England"
,
"nhs_ha"
:
"North West"
,
"longitude"
:
-2.30283674284007
,
"latitude"
:
53.4556572899372
,
"parliamentary_constituency"
:
"Stretford and Urmston"
,
"european_electoral_region"
:
"North West"
,
"primary_care_trust"
:
"Trafford"
,
"region"
:
"North West"
,
"lsoa"
:
"Trafford 003C"
,
"msoa"
:
"Trafford 003"
,
"incode"
:
"0JG"
,
"outcode"
:
"M32"
,
"admin_district"
:
"Trafford"
,
"parish"
:
"Trafford, unparished area"
,
"admin_county"
:
null
,
"admin_ward"
:
"Gorse Hill"
,
"ccg"
:
"NHS Trafford"
,
"nuts"
:
"Greater Manchester South West"
,
"codes"
:
{
"admin_district"
:
"E08000009"
,
"admin_county"
:
"E99999999"
,
"admin_ward"
:
"E05000829"
,
"parish"
:
"E43000163"
,
"ccg"
:
"E38000187"
,
"nuts"
:
"UKD34"
}
}
},
{
"query"
:
"OX49 5NU"
,
"result"
:
{
"postcode"
:
"OX49 5NU"
,
"quality"
:
1
,
"eastings"
:
464440
,
"northings"
:
195671
,
"country"
:
"England"
,
"nhs_ha"
:
"South Central"
,
"longitude"
:
-1.06986930435083
,
"latitude"
:
51.656143706615
,
"parliamentary_constituency"
:
"Henley"
,
"european_electoral_region"
:
"South East"
,
"primary_care_trust"
:
"Oxfordshire"
,
"region"
:
"South East"
,
"lsoa"
:
"South Oxfordshire 011B"
,
"msoa"
:
"South Oxfordshire 011"
,
"incode"
:
"5NU"
,
"outcode"
:
"OX49"
,
"admin_district"
:
"South Oxfordshire"
,
"parish"
:
"Brightwell Baldwin"
,
"admin_county"
:
"Oxfordshire"
,
"admin_ward"
:
"Chalgrove"
,
"ccg"
:
"NHS Oxfordshire"
,
"nuts"
:
"Oxfordshire"
,
"codes"
:
{
"admin_district"
:
"E07000179"
,
"admin_county"
:
"E10000025"
,
"admin_ward"
:
"E05009735"
,
"parish"
:
"E04008109"
,
"ccg"
:
"E38000136"
,
"nuts"
:
"UKJ14"
}
}
},
{
"query"
:
"NE30 1DP"
,
"result"
:
{
"postcode"
:
"NE30 1DP"
,
"quality"
:
1
,
"eastings"
:
435958
,
"northings"
:
568671
,
"country"
:
"England"
,
"nhs_ha"
:
"North East"
,
"longitude"
:
-1.43926900515621
,
"latitude"
:
55.0113051910514
,
"parliamentary_constituency"
:
"Tynemouth"
,
"european_electoral_region"
:
"North East"
,
"primary_care_trust"
:
"North Tyneside"
,
"region"
:
"North East"
,
"lsoa"
:
"North Tyneside 016C"
,
"msoa"
:
"North Tyneside 016"
,
"incode"
:
"1DP"
,
"outcode"
:
"NE30"
,
"admin_district"
:
"North Tyneside"
,
"parish"
:
"North Tyneside, unparished area"
,
"admin_county"
:
null
,
"admin_ward"
:
"Tynemouth"
,
"ccg"
:
"NHS North Tyneside"
,
"nuts"
:
"Tyneside"
,
"codes"
:
{
"admin_district"
:
"E08000022"
,
"admin_county"
:
"E99999999"
,
"admin_ward"
:
"E05001130"
,
"parish"
:
"E43000176"
,
"ccg"
:
"E38000127"
,
"nuts"
:
"UKC22"
}
}
}
]
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment