Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for patchMinor of UserAgent #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/main/java/ua_parser/UserAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,26 @@ public class UserAgent {

public static final UserAgent OTHER = new UserAgent("Other", null, null, null);

public final String family, major, minor, patch;
public final String family, major, minor, patch, patchMinor;

public UserAgent(String family, String major, String minor, String patch, String patchMinor) {
this.family = family;
this.major = major;
this.minor = minor;
this.patch = patch;
this.patchMinor = patchMinor;
}

public UserAgent(String family, String major, String minor, String patch) {
this.family = family;
this.major = major;
this.minor = minor;
this.patch = patch;
this.patchMinor = null;
}

public static UserAgent fromMap(Map<String, String> m) {
return new UserAgent(m.get("family"), m.get("major"), m.get("minor"), m.get("patch"));
return new UserAgent(m.get("family"), m.get("major"), m.get("minor"), m.get("patch"), m.get("patch_minor"));
}

@Override
Expand All @@ -63,11 +72,12 @@ public int hashCode() {

@Override
public String toString() {
return String.format("{\"family\": %s, \"major\": %s, \"minor\": %s, \"patch\": %s}",
return String.format("{\"family\": %s, \"major\": %s, \"minor\": %s, \"patch\": %s, \"patch_minor\": %s}",
family == null ? Constants.EMPTY_STRING : '"' + family + '"',
major == null ? Constants.EMPTY_STRING : '"' + major + '"',
minor == null ? Constants.EMPTY_STRING : '"' + minor + '"',
patch == null ? Constants.EMPTY_STRING : '"' + patch + '"');
patch == null ? Constants.EMPTY_STRING : '"' + patch + '"',
patchMinor == null ? Constants.EMPTY_STRING : '"' + patchMinor + '"');
}

}
10 changes: 8 additions & 2 deletions src/main/java/ua_parser/UserAgentParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public UAPattern(Pattern pattern, String familyReplacement, String v1Replacement
}

public UserAgent match(String agentString) {
String family = null, v1 = null, v2 = null, v3 = null;
String family = null, v1 = null, v2 = null, v3 = null, v4 = null;
Matcher matcher = pattern.matcher(agentString);

if (!matcher.find()) {
Expand Down Expand Up @@ -129,8 +129,14 @@ public UserAgent match(String agentString) {
v3 = group4;
}
}
if (groupCount >= 5) {
String group5 = matcher.group(5);
if (!isBlank(group5)) {
v4 = group5;
}
}
}
return family == null ? null : new UserAgent(family, v1, v2, v3);
return family == null ? null : new UserAgent(family, v1, v2, v3, v4);
}

private boolean isBlank(String value) {
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/ua_parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,21 @@ public void testParsePGTS() {
public void testParseAll() {
String agentString1 = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; fr; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5,gzip(gfe),gzip(gfe)";
String agentString2 = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3";
String agentString3 = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36";

Client expected1 = new Client(new UserAgent("Firefox", "3", "5", "5"),
new OS("Mac OS X", "10", "4", null, null),
new Device("Mac"));
Client expected2 = new Client(new UserAgent("Mobile Safari", "5", "1", null),
new OS("iOS", "5", "1", "1", null),
new Device("iPhone"));
Client expected3 = new Client(new UserAgent("Chrome", "86", "0", "4240", "111"),
new OS("Mac OS X", "10", "15", "6", null),
new Device("Mac"));

assertThat(parser.parse(agentString1), is(expected1));
assertThat(parser.parse(agentString2), is(expected2));
assertThat(parser.parse(agentString3), is(expected3));
}

@Test
Expand Down