Skip to content

Commit 3972939

Browse files
authored
Merge pull request #36 from ipinfo/umar/flag-currency-continent
added flag currency and continent
2 parents 646d0d6 + 91aa6b7 commit 3972939

File tree

11 files changed

+986
-6
lines changed

11 files changed

+986
-6
lines changed

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,59 @@ public class Main {
207207
}
208208
```
209209

210-
This file must follow the same layout as seen [here](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/eu.json)
210+
#### Internationalization
211+
212+
This library provides a system to lookup if a country is a member of the European Union (EU), emoji and unicode of country's flag, code and symbol of country's currency and continent code and name through ISO2 country codes.
213+
214+
Following are the file that are loaded by default:
215+
- [eu.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/eu.json)
216+
- [flags.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/flags.json)
217+
- [currency.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/currency.json)
218+
- [continent.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/continent.json)
219+
220+
If you wish to provide a different file, just use the following system in the builder:
221+
222+
```java
223+
import io.ipinfo.api.IPinfo;
224+
import io.ipinfo.api.errors.RateLimitedException;
225+
import io.ipinfo.api.model.IPResponse;
226+
227+
public class Main {
228+
public static void main(String... args) {
229+
IPinfo ipInfo = new IPinfo.Builder()
230+
.setToken("YOUR TOKEN")
231+
.setEUCountryFile(new File("path/to/file.json"))
232+
.setCountryFlagFile(new File("path/to/file.json"))
233+
.setCountryCurrencyFile(new File("path/to/file.json"))
234+
.setContinentFile(new File("path/to/file.json"))
235+
.build();
236+
237+
try {
238+
IPResponse response = ipInfo.lookupIP("8.8.8.8");
239+
240+
// Print out whether the country is a member of the EU
241+
System.out.println(response.isEU());
242+
243+
// CountryFlag{emoji='🇺🇸',unicode='U+1F1FA U+1F1F8'}
244+
System.out.println(response.getCountryFlag());
245+
246+
// CountryCurrency{code='USD',symbol='$'}
247+
System.out.println(response.getCountryCurrency());
248+
249+
// Continent{code='NA',name='North America'}
250+
System.out.println(response.getContinent());
251+
} catch (RateLimitedException ex) {
252+
// Handle rate limits here.
253+
}
254+
}
255+
}
256+
```
257+
258+
The files must follow the same layout as seen
259+
- [eu.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/eu.json)
260+
- [flags.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/flags.json)
261+
- [currency.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/currency.json)
262+
- [continent.json](https://github.com/ipinfo/java-ipinfo/blob/master/src/main/resources/continent.json)
211263

212264
#### Location Information
213265

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import io.ipinfo.api.context.Context;
99
import io.ipinfo.api.errors.RateLimitedException;
1010
import io.ipinfo.api.model.ASNResponse;
11+
import io.ipinfo.api.model.Continent;
12+
import io.ipinfo.api.model.CountryCurrency;
13+
import io.ipinfo.api.model.CountryFlag;
1114
import io.ipinfo.api.model.IPResponse;
1215
import io.ipinfo.api.model.MapResponse;
1316
import io.ipinfo.api.request.ASNRequest;
@@ -362,6 +365,12 @@ public static class Builder {
362365
new File(this.getClass().getClassLoader().getResource("en_US.json").getFile());
363366
private File euCountryFile =
364367
new File(this.getClass().getClassLoader().getResource("eu.json").getFile());
368+
private File countryFlagFile =
369+
new File(this.getClass().getClassLoader().getResource("flags.json").getFile());
370+
private File countryCurrencyFile =
371+
new File(this.getClass().getClassLoader().getResource("currency.json").getFile());
372+
private File continentFile =
373+
new File(this.getClass().getClassLoader().getResource("continent.json").getFile());
365374
private OkHttpClient client = new OkHttpClient.Builder().build();
366375
private String token = "";
367376
private Cache cache = new SimpleCache(Duration.ofDays(1));
@@ -386,6 +395,21 @@ public Builder setEUCountryFile(File file) {
386395
return this;
387396
}
388397

398+
public Builder setCountryFlagFile(File file) {
399+
this.countryFlagFile = file;
400+
return this;
401+
}
402+
403+
public Builder setCountryCurrencyFile(File file) {
404+
this.countryCurrencyFile = file;
405+
return this;
406+
}
407+
408+
public Builder setContinentFile(File file) {
409+
this.continentFile = file;
410+
return this;
411+
}
412+
389413
public Builder setCache(Cache cache) {
390414
this.cache = cache;
391415
return this;
@@ -394,19 +418,31 @@ public Builder setCache(Cache cache) {
394418
public IPinfo build() {
395419
Type type = new TypeToken<Map<String, String>>(){}.getType();
396420
Type euCountriesType = new TypeToken<List<String>>(){}.getType();
421+
Type countriesFlagsType = new TypeToken<Map<String, CountryFlag>>(){}.getType();
422+
Type countriesCurrencyType = new TypeToken<Map<String, CountryCurrency>>(){}.getType();
423+
Type continentType = new TypeToken<Map<String, Continent>>(){}.getType();
397424
Gson gson = new Gson();
398425
Map<String, String> map;
426+
Map<String, CountryFlag> countriesFlags;
427+
Map<String, CountryCurrency> countriesCurrencies;
428+
Map<String, Continent> continents;
399429
List<String> euList;
400430

401431
try {
402432
map = Collections.unmodifiableMap(gson.fromJson(new FileReader(countryFile), type));
433+
countriesFlags = Collections.unmodifiableMap(gson.fromJson(new FileReader(countryFlagFile), countriesFlagsType));
434+
countriesCurrencies = Collections.unmodifiableMap(gson.fromJson(new FileReader(countryCurrencyFile), countriesCurrencyType));
435+
continents = Collections.unmodifiableMap(gson.fromJson(new FileReader(continentFile), continentType));
403436
euList = Collections.unmodifiableList(gson.fromJson(new FileReader(euCountryFile), euCountriesType));
404437
} catch (Exception e) {
405438
map = Collections.unmodifiableMap(new HashMap<>());
439+
countriesFlags = Collections.unmodifiableMap(new HashMap<>());
440+
countriesCurrencies = Collections.unmodifiableMap(new HashMap<>());
441+
continents = Collections.unmodifiableMap(new HashMap<>());
406442
euList = Collections.unmodifiableList(new ArrayList<>());
407443
}
408444

409-
return new IPinfo(client, new Context(map, euList), token, cache);
445+
return new IPinfo(client, new Context(map, euList, countriesFlags, countriesCurrencies, continents), token, cache);
410446
}
411447
}
412448

src/main/java/io/ipinfo/api/context/Context.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,47 @@
33
import java.util.List;
44
import java.util.Map;
55

6+
import io.ipinfo.api.model.Continent;
7+
import io.ipinfo.api.model.CountryCurrency;
8+
import io.ipinfo.api.model.CountryFlag;
9+
610
public class Context {
711
private final Map<String, String> countryMap;
12+
private final Map<String, CountryFlag> countriesFlags;
13+
private final Map<String, CountryCurrency> countriesCurrencies;
14+
private final Map<String, Continent> continents;
815
private final List<String> euCountries;
916

10-
public Context(Map<String, String> countryMap, List<String> euCountries) {
11-
this.countryMap = countryMap;
12-
this.euCountries = euCountries;
13-
}
17+
public Context(
18+
Map<String, String> countryMap,
19+
List<String> euCountries,
20+
Map<String, CountryFlag> countriesFlags,
21+
Map<String, CountryCurrency> countriesCurrencies,
22+
Map<String, Continent> continents)
23+
{
24+
this.countryMap = countryMap;
25+
this.euCountries = euCountries;
26+
this.countriesFlags = countriesFlags;
27+
this.countriesCurrencies = countriesCurrencies;
28+
this.continents = continents;
29+
}
1430

1531
public String getCountryName(String countryCode) {
1632
return countryMap.get(countryCode);
1733
}
1834

35+
public CountryFlag getCountryFlag(String countryCode) {
36+
return countriesFlags.get(countryCode);
37+
}
38+
39+
public CountryCurrency getCountryCurrency(String countryCode) {
40+
return countriesCurrencies.get(countryCode);
41+
}
42+
43+
public Continent getContinent(String countryCode) {
44+
return continents.get(countryCode);
45+
}
46+
1947
public boolean isEU(String countryCode) {
2048
return euCountries.contains(countryCode);
2149
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.ipinfo.api.model;
2+
3+
public class Continent {
4+
private final String code;
5+
private final String name;
6+
7+
public Continent(
8+
String code,
9+
String name
10+
) {
11+
this.code = code;
12+
this.name = name;
13+
}
14+
15+
public String getCode() {
16+
return code;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return "Continent{" +
26+
"code='" + code + '\'' +
27+
",name='" + name + '\'' +
28+
'}';
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.ipinfo.api.model;
2+
3+
public class CountryCurrency {
4+
private final String code;
5+
private final String symbol;
6+
7+
public CountryCurrency(
8+
String code,
9+
String symbol
10+
) {
11+
this.code = code;
12+
this.symbol = symbol;
13+
}
14+
15+
public String getCode() {
16+
return code;
17+
}
18+
19+
public String getSymbol() {
20+
return symbol;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return "CountryCurrency{" +
26+
"code='" + code + '\'' +
27+
",symbol='" + symbol + '\'' +
28+
'}';
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.ipinfo.api.model;
2+
3+
public class CountryFlag {
4+
private final String emoji;
5+
private final String unicode;
6+
7+
public CountryFlag(
8+
String emoji,
9+
String unicode
10+
) {
11+
this.emoji = emoji;
12+
this.unicode = unicode;
13+
}
14+
15+
public String getEmoji() {
16+
return emoji;
17+
}
18+
19+
public String getUnicode() {
20+
return unicode;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return "CountryFlag{" +
26+
"emoji='" + emoji + '\'' +
27+
",unicode='" + unicode + '\'' +
28+
'}';
29+
}
30+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ public Boolean isEU() {
112112
return context.isEU(getCountryCode());
113113
}
114114

115+
public CountryFlag getCountryFlag() {
116+
return context.getCountryFlag(getCountryCode());
117+
}
118+
119+
public CountryCurrency getCountryCurrency() {
120+
return context.getCountryCurrency(getCountryCode());
121+
}
122+
123+
public Continent getContinent() {
124+
return context.getContinent(getCountryCode());
125+
}
126+
115127
public String getLocation() {
116128
return loc;
117129
}

0 commit comments

Comments
 (0)