Skip to content

Commit 93f442e

Browse files
authored
Merge pull request #14 from IPGeolocation/fix/parsing-issues
Restructured the SDK
2 parents 061c858 + 99683eb commit 93f442e

16 files changed

+1421
-1108
lines changed

README.md

Lines changed: 250 additions & 203 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,37 @@
66

77
<groupId>io.ipgeolocation</groupId>
88
<artifactId>ipgeolocation</artifactId>
9-
<version>1.0.13</version>
9+
<version>1.0.15</version>
1010

1111
<packaging>jar</packaging>
1212
<name>ipgeolocation-java-sdk</name>
1313
<description>Java SDK to lookup IP location, time zone detail, currency and security information using
1414
ipgeolocation.io API
1515
</description>
16-
17-
<!-- Publishing: Project path -->
1816
<url>https://github.com/IPGeolocation/ip-geolocation-api-java-sdk</url>
1917

18+
<developers>
19+
<developer>
20+
<name>ipgeolocation.io</name>
21+
<email>support@ipgeolocation.io</email>
22+
<url>https://ipgeolocation.io</url>
23+
</developer>
24+
</developers>
25+
26+
<licenses>
27+
<license>
28+
<name>MIT License</name>
29+
<url>https://opensource.org/license/mit/</url>
30+
<distribution>repo</distribution>
31+
</license>
32+
</licenses>
33+
34+
<scm>
35+
<connection>scm:git:git://github.com:IPGeolocation/ip-geolocation-api-java-sdk.git</connection>
36+
<developerConnection>scm:git:ssh://github.com:IPGeolocation/ip-geolocation-api-java-sdk.git</developerConnection>
37+
<url>https://github.com/IPGeolocation/ip-geolocation-api-java-sdk/tree/master</url>
38+
</scm>
39+
2040
<properties>
2141
<java.version>1.8</java.version>
2242
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -26,10 +46,17 @@
2646
<dependency>
2747
<groupId>org.json</groupId>
2848
<artifactId>json</artifactId>
29-
<version>20211205</version>
49+
<version>20230618</version>
3050
</dependency>
3151
</dependencies>
3252

53+
<distributionManagement>
54+
<snapshotRepository>
55+
<id>ossrh</id>
56+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
57+
</snapshotRepository>
58+
</distributionManagement>
59+
3360
<build>
3461
<pluginManagement>
3562
<plugins>
@@ -49,15 +76,57 @@
4976
<target>8</target>
5077
</configuration>
5178
</plugin>
79+
<plugin>
80+
<groupId>org.apache.maven.plugins</groupId>
81+
<artifactId>maven-source-plugin</artifactId>
82+
<version>2.2.1</version>
83+
<executions>
84+
<execution>
85+
<id>attach-sources</id>
86+
<goals>
87+
<goal>jar-no-fork</goal>
88+
</goals>
89+
</execution>
90+
</executions>
91+
</plugin>
92+
<plugin>
93+
<groupId>org.apache.maven.plugins</groupId>
94+
<artifactId>maven-javadoc-plugin</artifactId>
95+
<version>2.9.1</version>
96+
<executions>
97+
<execution>
98+
<id>attach-javadocs</id>
99+
<goals>
100+
<goal>jar</goal>
101+
</goals>
102+
</execution>
103+
</executions>
104+
</plugin>
105+
<plugin>
106+
<groupId>org.apache.maven.plugins</groupId>
107+
<artifactId>maven-gpg-plugin</artifactId>
108+
<version>1.5</version>
109+
<executions>
110+
<execution>
111+
<id>sign-artifacts</id>
112+
<phase>verify</phase>
113+
<goals>
114+
<goal>sign</goal>
115+
</goals>
116+
</execution>
117+
</executions>
118+
</plugin>
119+
<plugin>
120+
<groupId>org.sonatype.plugins</groupId>
121+
<artifactId>nexus-staging-maven-plugin</artifactId>
122+
<version>1.6.7</version>
123+
<extensions>true</extensions>
124+
<configuration>
125+
<serverId>ossrh</serverId>
126+
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
127+
<autoReleaseAfterClose>true</autoReleaseAfterClose>
128+
</configuration>
129+
</plugin>
52130
</plugins>
53131
</build>
54-
55-
<distributionManagement>
56-
<repository>
57-
<id>bintray-ipgeolocation-ipgeolocation</id>
58-
<name>ipgeolocation-ipgeolocation</name>
59-
<url>https://api.bintray.com/maven/ipgeolocation/ipgeolocation/io.ipgeolocation/;publish=1</url>
60-
</repository>
61-
</distributionManagement>
62-
63132
</project>

src/main/java/io/ipgeolocation/api/Geolocation.java

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package io.ipgeolocation.api;
22

3-
import java.util.HashMap;
4-
import java.util.Map;
5-
6-
import static java.util.Objects.isNull;
3+
import java.math.BigDecimal;
4+
import java.util.Objects;
5+
import org.json.JSONObject;
76

87
public class Geolocation {
98
private final String domain;
@@ -16,12 +15,13 @@ public class Geolocation {
1615
private final String countryName;
1716
private final String countryCapital;
1817
private final String stateProvince;
18+
private final String stateCode;
1919
private final String district;
2020
private final String city;
2121
private final String zipCode;
22-
private final String latitude;
23-
private final String longitude;
24-
private final Boolean isEU;
22+
private final BigDecimal latitude;
23+
private final BigDecimal longitude;
24+
private final boolean eu;
2525
private final String callingCode;
2626
private final String countryTLD;
2727
private final String languages;
@@ -30,72 +30,73 @@ public class Geolocation {
3030
private final String connectionType;
3131
private final String organization;
3232
private final String asn;
33-
private final String geonameID;
33+
private final long geoNameId;
3434
private GeolocationCurrency currency;
3535
private GeolocationTimezone timezone;
3636
private GeolocationSecurity geolocationSecurity;
3737
private UserAgent userAgent;
38+
private final JSONObject json;
3839

39-
Geolocation(Map<String, Object> json) {
40-
if (isNull(json)) {
41-
throw new IllegalArgumentException("'json' must not be null.");
40+
Geolocation(JSONObject json) {
41+
if (Objects.isNull(json)) {
42+
throw new IllegalArgumentException("'json' must not be null");
4243
}
4344

4445
if (json.isEmpty()) {
45-
throw new IllegalArgumentException("'json' must not be empty.");
46+
throw new IllegalArgumentException("'json' must not be empty");
4647
}
4748

48-
this.domain = (String) json.get("domain");
49-
this.ip = (String) json.get("ip");
50-
this.hostname = (String) json.get("hostname");
51-
this.continentCode = (String) json.get("continent_code");
52-
this.continentName = (String) json.get("continent_name");
53-
this.countryCode2 = (String) json.get("country_code2");
54-
this.countryCode3 = (String) json.get("country_code3");
55-
this.countryName = (String) json.get("country_name");
56-
this.countryCapital = (String) json.get("country_capital");
57-
this.stateProvince = (String) json.get("state_prov");
58-
this.district = (String) json.get("district");
59-
this.city = (String) json.get("city");
60-
this.zipCode = (String) json.get("zipcode");
61-
this.latitude = (String) json.get("latitude");
62-
this.longitude = (String) json.get("longitude");
63-
this.isEU = (Boolean) json.get("is_eu");
64-
this.callingCode = (String) json.get("calling_code");
65-
this.countryTLD = (String) json.get("country_tld");
66-
this.languages = (String) json.get("languages");
67-
this.countryFlag = (String) json.get("country_flag");
68-
this.isp = (String) json.get("isp");
69-
this.connectionType = (String) json.get("connection_type");
70-
this.organization = (String) json.get("organization");
71-
this.asn = (String) json.get("asn");
72-
this.geonameID = (String) json.get("geoname_id");
73-
if (json.get("currency") instanceof HashMap) {
74-
Map<String, Object> currencyJson = (HashMap) json.get("currency");
75-
this.currency = new GeolocationCurrency(currencyJson);
49+
this.domain = json.optString("domain");
50+
this.ip = json.getString("ip");
51+
this.hostname = json.optString("hostname");
52+
this.continentCode = json.optString("continent_code");
53+
this.continentName = json.optString("continent_name");
54+
this.countryCode2 = json.optString("country_code2");
55+
this.countryCode3 = json.optString("country_code3");
56+
this.countryName = json.optString("country_name");
57+
this.countryCapital = json.optString("country_capital");
58+
this.stateProvince = json.optString("state_prov");
59+
this.stateCode = json.optString("state_code");
60+
this.district = json.optString("district");
61+
this.city = json.optString("city");
62+
this.zipCode = json.optString("zipcode");
63+
this.latitude = new BigDecimal(json.optString("latitude", "0.0"));
64+
this.longitude = new BigDecimal(json.optString("longitude", "0.0"));
65+
this.eu = json.optBoolean("is_eu");
66+
this.callingCode = json.optString("calling_code");
67+
this.countryTLD = json.optString("country_tld");
68+
this.languages = json.optString("languages");
69+
this.countryFlag = json.optString("country_flag");
70+
this.isp = json.optString("isp");
71+
this.connectionType = json.optString("connection_type");
72+
this.organization = json.optString("organization");
73+
this.asn = json.optString("asn");
74+
this.geoNameId = Long.parseLong(json.optString("geoname_id", "0"));
75+
76+
if (json.has("currency")) {
77+
this.currency = new GeolocationCurrency(json.getJSONObject("currency"));
7678
}
7779

78-
if (json.get("time_zone") instanceof HashMap) {
79-
Map<String, Object> timezoneJson = (HashMap) json.get("time_zone");
80-
this.timezone = new GeolocationTimezone(timezoneJson);
80+
if (json.has("time_zone")) {
81+
this.timezone = new GeolocationTimezone(json.getJSONObject("time_zone"));
8182
}
8283

83-
if (json.get("security") instanceof HashMap) {
84-
Map<String, Object> securityJson = (HashMap) json.get("security");
85-
this.geolocationSecurity = new GeolocationSecurity(securityJson);
84+
if (json.has("security")) {
85+
this.geolocationSecurity = new GeolocationSecurity(json.getJSONObject("security"));
8686
}
8787

88-
if (json.get("user_agent") instanceof HashMap) {
89-
Map<String, Object> userAgentJson = (HashMap) json.get("user_agent");
90-
this.userAgent = new UserAgent(userAgentJson);
88+
if (json.has("user_agent")) {
89+
this.userAgent = new UserAgent(json.getJSONObject("user_agent"));
9190
}
91+
92+
this.json = json;
9293
}
9394

9495
public String getDomain() {
9596
return domain;
9697
}
9798

98-
public String getIPAddress() {
99+
public String getIP() {
99100
return ip;
100101
}
101102

@@ -131,6 +132,10 @@ public String getStateProvince() {
131132
return stateProvince;
132133
}
133134

135+
public String getStateCode() {
136+
return stateCode;
137+
}
138+
134139
public String getDistrict() {
135140
return district;
136141
}
@@ -143,16 +148,16 @@ public String getZipCode() {
143148
return zipCode;
144149
}
145150

146-
public String getLatitude() {
151+
public BigDecimal getLatitude() {
147152
return latitude;
148153
}
149154

150-
public String getLongitude() {
155+
public BigDecimal getLongitude() {
151156
return longitude;
152157
}
153158

154-
public Boolean isEU() {
155-
return isEU;
159+
public boolean isEU() {
160+
return eu;
156161
}
157162

158163
public String getCallingCode() {
@@ -187,8 +192,8 @@ public String getAsn() {
187192
return asn;
188193
}
189194

190-
public String getGeonameID() {
191-
return geonameID;
195+
public long getGeoNameId() {
196+
return geoNameId;
192197
}
193198

194199
public GeolocationCurrency getCurrency() {
@@ -206,4 +211,9 @@ public GeolocationSecurity getGeolocationSecurity() {
206211
public UserAgent getUserAgent() {
207212
return userAgent;
208213
}
214+
215+
@Override
216+
public String toString() {
217+
return json.toString(2);
218+
}
209219
}

src/main/java/io/ipgeolocation/api/GeolocationCurrency.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
package io.ipgeolocation.api;
22

3-
import java.util.Map;
3+
import java.util.Objects;
4+
import org.json.JSONObject;
45

56
public class GeolocationCurrency {
67
private final String name;
78
private final String code;
89
private final String symbol;
10+
private final JSONObject json;
911

10-
GeolocationCurrency(Map<String, Object> json) {
11-
this.name = (String) json.get("name");
12-
this.code = (String) json.get("code");
13-
this.symbol = (String) json.get("symbol");
12+
GeolocationCurrency(JSONObject json) {
13+
if (Objects.isNull(json)) {
14+
throw new IllegalArgumentException("'json' must not be null");
15+
}
16+
17+
if (json.isEmpty()) {
18+
throw new IllegalArgumentException("'json' must not be empty");
19+
}
20+
21+
this.name = json.getString("name");
22+
this.code = json.getString("code");
23+
this.symbol = json.getString("symbol");
24+
this.json = json;
1425
}
1526

1627
public String getName() {
@@ -24,4 +35,9 @@ public String getCode() {
2435
public String getSymbol() {
2536
return symbol;
2637
}
38+
39+
@Override
40+
public String toString() {
41+
return json.toString(2);
42+
}
2743
}

0 commit comments

Comments
 (0)