Commit 580374fb authored by Deepak's avatar Deepak

Added outwardCodeLookup, OutcodeReverseGeocoding, NearestOutcode

parent 5c15e23d
......@@ -11,7 +11,7 @@ import com.mashape.unirest.http.exceptions.UnirestException;
*/
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;
......@@ -33,7 +33,7 @@ public class LimitPostcode {
}
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();
}
}
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;
}
}
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;
}
}
......@@ -54,7 +54,7 @@ public class Postcode {
public JSONObject asJson() throws Exception {
try {
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) {
return JsonFetcher.postURLToJson(new URL(LOOKUP_URL.toString()), json);
} else {
......
......@@ -99,4 +99,16 @@ public class PostcodeLookup {
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));
}
}
......@@ -127,4 +127,51 @@ public class PostcodeLookupTest {
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment