Skip to content

Commit 3b93e95

Browse files
authored
Merge pull request #33 from ipinfo/umar/isEU
added isEU
2 parents d0045d0 + 71d7623 commit 3b93e95

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,40 @@ This file must follow the same layout as seen [here](https://github.com/ipinfo/j
175175

176176
More language files can be found [here](https://country.io/data)
177177

178+
#### EU Country Lookup
179+
180+
This library provides a system to lookup if a country is a member of the European Union (EU) through
181+
ISO2 country codes.
182+
183+
By default, [here](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/eu.json) is the file containing all the EU members.
184+
If you wish to provide a different file, just use the following system in the builder:
185+
186+
```java
187+
import io.ipinfo.api.IPinfo;
188+
import io.ipinfo.api.errors.RateLimitedException;
189+
import io.ipinfo.api.model.IPResponse;
190+
191+
public class Main {
192+
public static void main(String... args) {
193+
IPinfo ipInfo = new IPinfo.Builder()
194+
.setToken("YOUR TOKEN")
195+
.setEUCountryFile(new File("path/to/file.json"))
196+
.build();
197+
198+
try {
199+
IPResponse response = ipInfo.lookupIP("8.8.8.8");
200+
201+
// Print out whether the country is a member of the EU
202+
System.out.println(response.isEU());
203+
} catch (RateLimitedException ex) {
204+
// Handle rate limits here.
205+
}
206+
}
207+
}
208+
```
209+
210+
This file must follow the same layout as seen [here](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/eu.json)
211+
178212
#### Location Information
179213

180214
This library provides an easy way to get the latitude and longitude of an IP Address:

src/main/java/io/ipinfo/api/IPinfo.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ public static String cacheKey(String k) {
360360
public static class Builder {
361361
private File countryFile =
362362
new File(this.getClass().getClassLoader().getResource("en_US.json").getFile());
363+
private File euCountryFile =
364+
new File(this.getClass().getClassLoader().getResource("eu.json").getFile());
363365
private OkHttpClient client = new OkHttpClient.Builder().build();
364366
private String token = "";
365367
private Cache cache = new SimpleCache(Duration.ofDays(1));
@@ -379,23 +381,32 @@ public Builder setCountryFile(File file) {
379381
return this;
380382
}
381383

384+
public Builder setEUCountryFile(File file) {
385+
this.euCountryFile = file;
386+
return this;
387+
}
388+
382389
public Builder setCache(Cache cache) {
383390
this.cache = cache;
384391
return this;
385392
}
386393

387394
public IPinfo build() {
388395
Type type = new TypeToken<Map<String, String>>(){}.getType();
396+
Type euCountriesType = new TypeToken<List<String>>(){}.getType();
389397
Gson gson = new Gson();
390398
Map<String, String> map;
399+
List<String> euList;
391400

392401
try {
393402
map = Collections.unmodifiableMap(gson.fromJson(new FileReader(countryFile), type));
403+
euList = Collections.unmodifiableList(gson.fromJson(new FileReader(euCountryFile), euCountriesType));
394404
} catch (Exception e) {
395405
map = Collections.unmodifiableMap(new HashMap<>());
406+
euList = Collections.unmodifiableList(new ArrayList<>());
396407
}
397408

398-
return new IPinfo(client, new Context(map), token, cache);
409+
return new IPinfo(client, new Context(map, euList), token, cache);
399410
}
400411
}
401412

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package io.ipinfo.api.context;
22

3+
import java.util.List;
34
import java.util.Map;
45

56
public class Context {
67
private final Map<String, String> countryMap;
8+
private final List<String> euCountries;
79

8-
public Context(Map<String, String> countryMap) {
10+
public Context(Map<String, String> countryMap, List<String> euCountries) {
911
this.countryMap = countryMap;
12+
this.euCountries = euCountries;
1013
}
1114

1215
public String getCountryName(String countryCode) {
1316
return countryMap.get(countryCode);
1417
}
18+
19+
public boolean isEU(String countryCode) {
20+
return euCountries.contains(countryCode);
21+
}
1522
}

src/main/java/io/ipinfo/api/model/IPResponse.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public String getCountryName() {
9595
return context.getCountryName(getCountryCode());
9696
}
9797

98+
public Boolean isEU() {
99+
return context.isEU(getCountryCode());
100+
}
101+
98102
public String getLocation() {
99103
return loc;
100104
}

src/main/resources/eu.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["IE","AT","LT","LU","LV","DE","DK","SE","SI","SK","CZ","CY","NL","FI","FR","MT","ES","IT","EE","PL","PT","HU","HR","GR","RO","BG","BE"]

src/test/java/io/ipinfo/IPinfoTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public void testGoogleDNS() {
3333
() -> assertEquals(response.getRegion(), "California"),
3434
() -> assertEquals(response.getCountryCode(), "US"),
3535
() -> assertEquals(response.getCountryName(), "United States"),
36+
() -> assertFalse(response.isEU()),
3637
() -> assertEquals(response.getTimezone(), "America/Los_Angeles"),
3738
() -> assertFalse(response.getPrivacy().getProxy()),
3839
() -> assertFalse(response.getPrivacy().getVpn()),

0 commit comments

Comments
 (0)