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
580374fb
Commit
580374fb
authored
Sep 18, 2016
by
Deepak
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added outwardCodeLookup, OutcodeReverseGeocoding, NearestOutcode
parent
5c15e23d
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
182 additions
and
3 deletions
+182
-3
src/main/java/com/postcode/io/initializers/LimitPostcode.java
...main/java/com/postcode/io/initializers/LimitPostcode.java
+2
-2
src/main/java/com/postcode/io/initializers/OutcodeReverseGeocoding.java
...com/postcode/io/initializers/OutcodeReverseGeocoding.java
+87
-0
src/main/java/com/postcode/io/initializers/OutwardCode.java
src/main/java/com/postcode/io/initializers/OutwardCode.java
+33
-0
src/main/java/com/postcode/io/initializers/Postcode.java
src/main/java/com/postcode/io/initializers/Postcode.java
+1
-1
src/main/java/com/postcode/io/initializers/PostcodeLookup.java
...ain/java/com/postcode/io/initializers/PostcodeLookup.java
+12
-0
src/test/java/com/postcode/io/initializers/PostcodeLookupTest.java
...java/com/postcode/io/initializers/PostcodeLookupTest.java
+47
-0
No files found.
src/main/java/com/postcode/io/initializers/LimitPostcode.java
View file @
580374fb
...
@@ -11,7 +11,7 @@ import com.mashape.unirest.http.exceptions.UnirestException;
...
@@ -11,7 +11,7 @@ import com.mashape.unirest.http.exceptions.UnirestException;
*/
*/
public
class
LimitPostcode
{
public
class
LimitPostcode
{
private
static
final
String
AUTOCOMPLETE
_URL
=
"https://api.postcodes.io/postcodes/"
;
private
static
final
String
POSTCODE_LIMIT
_URL
=
"https://api.postcodes.io/postcodes/"
;
private
static
String
postcode
;
private
static
String
postcode
;
...
@@ -33,7 +33,7 @@ public class LimitPostcode {
...
@@ -33,7 +33,7 @@ public class LimitPostcode {
}
}
public
JSONObject
asJson
()
throws
UnirestException
{
public
JSONObject
asJson
()
throws
UnirestException
{
return
Unirest
.
get
(
AUTOCOMPLETE
_URL
.
concat
(
postcode
).
concat
(
"/autocomplete"
))
return
Unirest
.
get
(
POSTCODE_LIMIT
_URL
.
concat
(
postcode
).
concat
(
"/autocomplete"
))
.
queryString
(
"limit"
,
limit
!=
0
?
limit
:
10
).
asJson
().
getBody
().
getObject
();
.
queryString
(
"limit"
,
limit
!=
0
?
limit
:
10
).
asJson
().
getBody
().
getObject
();
}
}
}
}
src/main/java/com/postcode/io/initializers/OutcodeReverseGeocoding.java
0 → 100644
View file @
580374fb
package
com.postcode.io.initializers
;
import
org.json.JSONObject
;
import
com.mashape.unirest.http.Unirest
;
import
com.mashape.unirest.http.exceptions.UnirestException
;
public
class
OutcodeReverseGeocoding
{
private
static
final
String
OUTWARD_CODE_URL
=
"https://api.postcodes.io/outcodes/"
;
private
static
String
outwardCode
;
private
static
boolean
nearest
=
false
;
private
static
boolean
latlong
=
false
;
private
static
String
latitude
;
private
static
String
longitude
;
private
static
int
limit
;
private
static
int
radius
;
protected
OutcodeReverseGeocoding
(
String
longitude
,
String
latitude
)
{
OutcodeReverseGeocoding
.
longitude
=
longitude
;
OutcodeReverseGeocoding
.
latitude
=
latitude
;
OutcodeReverseGeocoding
.
latlong
=
true
;
}
protected
OutcodeReverseGeocoding
(
String
outwardCode
,
boolean
nearest
)
{
OutcodeReverseGeocoding
.
outwardCode
=
outwardCode
;
OutcodeReverseGeocoding
.
nearest
=
nearest
;
}
/**
* (not required) Limits number of postcodes matches to return. Defaults to 10. Needs to be less
* than 100.
*
* @param limit
* @return
*/
public
OutcodeReverseGeocoding
limit
(
int
limit
)
{
OutcodeReverseGeocoding
.
limit
=
limit
;
return
this
;
}
/**
* (not required) Limits number of postcodes matches to return. Defaults to 5,000m. Needs to be
* less than 25,000m.
*
* @param radius
* @return
*/
public
OutcodeReverseGeocoding
radius
(
int
radius
)
{
OutcodeReverseGeocoding
.
radius
=
radius
;
return
this
;
}
public
JSONObject
asJson
()
throws
UnirestException
{
try
{
if
(
nearest
)
{
return
Unirest
.
get
(
OUTWARD_CODE_URL
.
concat
(
outwardCode
).
concat
(
"/nearest"
))
.
queryString
(
"limit"
,
limit
!=
0
?
limit
:
10
)
.
queryString
(
"radius"
,
radius
!=
0
?
radius
:
5000
).
asJson
().
getBody
().
getObject
();
}
if
(
latlong
)
{
return
Unirest
.
get
(
OUTWARD_CODE_URL
).
queryString
(
"lon"
,
longitude
).
queryString
(
"lat"
,
latitude
)
.
queryString
(
"limit"
,
limit
!=
0
?
limit
:
10
)
.
queryString
(
"radius"
,
radius
!=
0
?
radius
:
5000
).
asJson
().
getBody
().
getObject
();
}
return
Unirest
.
get
(
OUTWARD_CODE_URL
.
concat
(
outwardCode
)).
asJson
().
getBody
().
getObject
();
}
finally
{
clear
();
}
}
private
void
clear
()
{
latitude
=
null
;
longitude
=
null
;
nearest
=
false
;
latlong
=
false
;
limit
=
0
;
radius
=
0
;
}
}
src/main/java/com/postcode/io/initializers/OutwardCode.java
0 → 100644
View file @
580374fb
package
com.postcode.io.initializers
;
import
org.json.JSONObject
;
import
com.mashape.unirest.http.Unirest
;
import
com.mashape.unirest.http.exceptions.UnirestException
;
/**
* @author Deepak
*
*/
public
class
OutwardCode
{
private
static
final
String
OUTWARD_CODE_URL
=
"https://api.postcodes.io/outcodes/"
;
private
static
String
outwardCode
;
protected
OutwardCode
(
String
outwardCode
)
{
OutwardCode
.
outwardCode
=
outwardCode
;
}
public
JSONObject
asJson
()
throws
UnirestException
{
try
{
return
Unirest
.
get
(
OUTWARD_CODE_URL
.
concat
(
outwardCode
)).
asJson
().
getBody
().
getObject
();
}
finally
{
clear
();
}
}
private
void
clear
()
{
outwardCode
=
null
;
}
}
src/main/java/com/postcode/io/initializers/Postcode.java
View file @
580374fb
...
@@ -54,7 +54,7 @@ public class Postcode {
...
@@ -54,7 +54,7 @@ public class Postcode {
public
JSONObject
asJson
()
throws
Exception
{
public
JSONObject
asJson
()
throws
Exception
{
try
{
try
{
if
(!
StringUtils
.
isEmpty
(
postcode
))
{
if
(!
StringUtils
.
isEmpty
(
postcode
))
{
return
JsonFetcher
.
urlToJson
(
new
URL
(
LOOKUP_URL
.
toString
().
concat
(
postcode
))
);
return
Unirest
.
get
(
LOOKUP_URL
.
toString
().
concat
(
postcode
)).
asJson
().
getBody
().
getObject
(
);
}
else
if
(
json
!=
null
)
{
}
else
if
(
json
!=
null
)
{
return
JsonFetcher
.
postURLToJson
(
new
URL
(
LOOKUP_URL
.
toString
()),
json
);
return
JsonFetcher
.
postURLToJson
(
new
URL
(
LOOKUP_URL
.
toString
()),
json
);
}
else
{
}
else
{
...
...
src/main/java/com/postcode/io/initializers/PostcodeLookup.java
View file @
580374fb
...
@@ -99,4 +99,16 @@ public class PostcodeLookup {
...
@@ -99,4 +99,16 @@ public class PostcodeLookup {
return
new
LimitPostcode
(
postcode
);
return
new
LimitPostcode
(
postcode
);
}
}
public
static
OutwardCode
lookupOutwardCode
(
String
outwardCode
)
{
return
new
OutwardCode
(
outwardCode
);
}
public
static
OutcodeReverseGeocoding
nearestOutwardCode
(
String
outwardCode
)
{
return
new
OutcodeReverseGeocoding
(
outwardCode
,
true
);
}
public
static
OutcodeReverseGeocoding
outcodeReverseGeocoding
(
Double
longitude
,
Double
latitude
)
{
return
new
OutcodeReverseGeocoding
(
String
.
valueOf
(
longitude
),
String
.
valueOf
(
latitude
));
}
}
}
src/test/java/com/postcode/io/initializers/PostcodeLookupTest.java
View file @
580374fb
...
@@ -127,4 +127,51 @@ public class PostcodeLookupTest {
...
@@ -127,4 +127,51 @@ public class PostcodeLookupTest {
assertEquals
(
20
,
PostcodeLookup
.
autocomplete
(
"ST4"
).
limit
(
20
).
asJson
().
getJSONArray
(
"result"
).
length
());
assertEquals
(
20
,
PostcodeLookup
.
autocomplete
(
"ST4"
).
limit
(
20
).
asJson
().
getJSONArray
(
"result"
).
length
());
}
}
@Test
public
void
testLookupOutwardCode
()
throws
Exception
{
assertEquals
(
200
,
PostcodeLookup
.
lookupOutwardCode
(
"ST4"
).
asJson
().
get
(
"status"
));
JSONAssert
.
assertEquals
(
Unirest
.
get
(
"https://api.postcodes.io/outcodes/ST4"
).
asJson
().
getBody
().
getObject
(),
PostcodeLookup
.
lookupOutwardCode
(
"ST4"
).
asJson
(),
JSONCompareMode
.
STRICT
);
JSONAssert
.
assertEquals
(
Unirest
.
get
(
"https://api.postcodes.io/outcodes/"
).
asJson
().
getBody
().
getObject
(),
PostcodeLookup
.
lookupOutwardCode
(
""
).
asJson
(),
JSONCompareMode
.
STRICT
);
}
@Test
public
void
testNearestOutwardCode
()
throws
Exception
{
assertEquals
(
200
,
PostcodeLookup
.
nearestOutwardCode
(
"ST4"
).
asJson
().
get
(
"status"
));
JSONAssert
.
assertEquals
(
Unirest
.
get
(
"https://api.postcodes.io/outcodes/ST4/nearest"
).
asJson
().
getBody
().
getObject
(),
PostcodeLookup
.
nearestOutwardCode
(
"ST4"
).
asJson
(),
JSONCompareMode
.
STRICT
);
JSONAssert
.
assertEquals
(
Unirest
.
get
(
"https://api.postcodes.io/outcodes//nearest"
).
asJson
().
getBody
().
getObject
(),
PostcodeLookup
.
nearestOutwardCode
(
""
).
asJson
(),
JSONCompareMode
.
STRICT
);
}
@Test
public
void
testOutcodeReverseGeocoding
()
throws
Exception
{
assertEquals
(
200
,
PostcodeLookup
.
outcodeReverseGeocoding
(
0.637189329739338
,
51.8051006359272
).
asJson
().
get
(
"status"
));
JSONAssert
.
assertEquals
(
Unirest
.
get
(
"https://api.postcodes.io/outcodes/"
).
queryString
(
"lon"
,
0.637189329739338
)
.
queryString
(
"lat"
,
51.8051006359272
).
asJson
().
getBody
().
getObject
(),
PostcodeLookup
.
outcodeReverseGeocoding
(
0.637189329739338
,
51.8051006359272
).
asJson
(),
JSONCompareMode
.
STRICT
);
JSONAssert
.
assertEquals
(
Unirest
.
get
(
"https://api.postcodes.io/outcodes/"
).
queryString
(
"lon"
,
0.637189329739338
)
.
queryString
(
"lat"
,
51.8051006359272
).
queryString
(
"limit"
,
20
).
queryString
(
"radius"
,
10000
)
.
asJson
().
getBody
().
getObject
(),
PostcodeLookup
.
outcodeReverseGeocoding
(
0.637189329739338
,
51.8051006359272
).
limit
(
20
).
radius
(
10000
)
.
asJson
(),
JSONCompareMode
.
STRICT
);
JSONAssert
.
assertEquals
(
Unirest
.
get
(
"https://api.postcodes.io/outcodes/"
).
queryString
(
"lon"
,
0.637189329739338
)
.
queryString
(
"lat"
,
51.8051006359272
).
queryString
(
"limit"
,
20
).
asJson
().
getBody
().
getObject
(),
PostcodeLookup
.
outcodeReverseGeocoding
(
0.637189329739338
,
51.8051006359272
).
limit
(
20
).
asJson
(),
JSONCompareMode
.
STRICT
);
JSONAssert
.
assertEquals
(
Unirest
.
get
(
"https://api.postcodes.io/outcodes/"
).
queryString
(
"lon"
,
0.637189329739338
)
.
queryString
(
"lat"
,
51.8051006359272
).
queryString
(
"radius"
,
10000
).
asJson
().
getBody
().
getObject
(),
PostcodeLookup
.
outcodeReverseGeocoding
(
0.637189329739338
,
51.8051006359272
).
radius
(
10000
).
asJson
(),
JSONCompareMode
.
STRICT
);
}
}
}
\ 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