Skip to content

Commit

Permalink
remove isMobile and isSpider from device parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
sjiang committed Dec 4, 2012
1 parent bbf29c5 commit 38a3cdc
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 52 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ua_parser Java Library
======================

This is the Java implementation of [ua-parser](https://github.com/tobie/ua-parser).
This is the Java implementation of [ua-parser](https://github.com/tobie/ua-parser).
The implementation uses the shared regex patterns and overrides from regexes.yaml.

Build:
Expand Down Expand Up @@ -31,8 +31,6 @@ import ua_parser.Client;
System.out.println(c.os.minor); // => "1"

System.out.println(c.device.family); // => "iPhone"
System.out.println(c.device.isMobile); // => true
System.out.println(c.device.isSpider); // => false
```

Author:
Expand Down
22 changes: 6 additions & 16 deletions src/main/java/ua_parser/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@
*/
public class Device {
public final String family;
public final boolean isMobile, isSpider;

public Device(String family, boolean isMobile, boolean isSpider) {
public Device(String family) {
this.family = family;
this.isMobile = isMobile;
this.isSpider = isSpider;
}

public static Device fromMap(Map<String, Object> m) {
return new Device((String) m.get("family"), (Boolean) m.get("is_mobile"),
(Boolean) m.get("is_spider"));
return new Device((String) m.get("family"));
}

@Override
Expand All @@ -44,23 +40,17 @@ public boolean equals(Object other) {
if (!(other instanceof Device)) return false;

Device o = (Device) other;
return ((this.family != null && this.family.equals(o.family)) || this.family == o.family) &&
this.isMobile == o.isMobile &&
this.isSpider == o.isSpider;
return (this.family != null && this.family.equals(o.family)) || this.family == o.family;
}

@Override
public int hashCode() {
int h = family == null ? 0 : family.hashCode();
h += isMobile ? 1429 : 2713;
h += isSpider ? 1187 : 953;
return h;
return family == null ? 0 : family.hashCode();
}

@Override
public String toString() {
return String.format("{family: %s, is_mobile: %s, is_spider: %s}",
family == null ? null : '"' + family + '"',
isMobile, isSpider);
return String.format("{family: %s}",
family == null ? null : '"' + family + '"');
}
}
24 changes: 4 additions & 20 deletions src/main/java/ua_parser/DeviceParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,28 @@
*/
public class DeviceParser {
List<DevicePattern> patterns;
private final Set<String> mobileUAFamilies, mobileOSFamilies;
private final UserAgentParser uaParser;

public DeviceParser(List<DevicePattern> patterns, UserAgentParser uaParser,
Set<String> mobileUAFamilies, Set<String> mobileOSFamilies) {
public DeviceParser(List<DevicePattern> patterns) {
this.patterns = patterns;
this.uaParser = uaParser;
this.mobileUAFamilies = mobileUAFamilies;
this.mobileOSFamilies = mobileOSFamilies;
}

public Device parse(String agentString) {
return parse(agentString, uaParser.parse(agentString).family);
}

public Device parse(String agentString, String userAgentFamily) {
String device = null;
for (DevicePattern p : patterns) {
if ((device = p.match(agentString)) != null) {
break;
}
}

String osFamily = device == null ? "Other" : device;
userAgentFamily = userAgentFamily == null ? "Other" : userAgentFamily;

return new Device(device,
mobileUAFamilies.contains(userAgentFamily) || mobileOSFamilies.contains(osFamily),
(device != null && device.equals("Spider")));
return new Device(device);
}

public static DeviceParser fromList(List<Map> configList, UserAgentParser uaParser,
Set<String> mobileUAFamilies, Set<String> mobileOSFamilies) {
public static DeviceParser fromList(List<Map> configList) {
List<DevicePattern> configPatterns = new ArrayList<DevicePattern>();
for (Map<String,String> configMap : configList) {
configPatterns.add(DeviceParser.patternFromMap(configMap));
}
return new DeviceParser(configPatterns, uaParser, mobileUAFamilies, mobileOSFamilies);
return new DeviceParser(configPatterns);
}

protected static DevicePattern patternFromMap(Map<String, String> configMap) {
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/ua_parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Parser(InputStream regexYaml) {
public Client parse(String agentString) {
UserAgent ua = parseUserAgent(agentString);
OS os = parseOS(agentString);
Device device = deviceParser.parse(agentString, (ua == null ? null : ua.family));
Device device = deviceParser.parse(agentString);
return new Client(ua, os, device);
}

Expand Down Expand Up @@ -86,11 +86,6 @@ private void initialize(InputStream regexYaml) {
if (deviceParserConfigs == null) {
throw new IllegalArgumentException("device_parsers is missing from yaml");
}
List<String> mobileUAFamiliesList = regexConfig.get("mobile_user_agent_families");
List<String> mobileOSFamiliesList = regexConfig.get("mobile_os_families");
Set<String> mobileUAFamilies = (mobileUAFamiliesList == null ? Collections.EMPTY_SET : new HashSet<String>(mobileUAFamiliesList));
Set<String> mobileOSFamilies = (mobileOSFamiliesList == null ? Collections.EMPTY_SET : new HashSet<String>(mobileOSFamiliesList));

deviceParser = DeviceParser.fromList(deviceParserConfigs, uaParser, mobileUAFamilies, mobileOSFamilies);
deviceParser = DeviceParser.fromList(deviceParserConfigs);
}
}
6 changes: 2 additions & 4 deletions src/test/java/ua_parser/DeviceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ public class DeviceTest extends DataTest<Device> {
protected Device getRandomInstance(long seed, StringGenerator g) {
random.setSeed(seed);
String family = g.getString(256);
boolean isMobile = random.nextBoolean(),
isSpider = random.nextBoolean();
return new Device(family, isMobile, isSpider);
return new Device(family);
}

@Override
protected Device getBlankInstance() {
return new Device(null, false, false);
return new Device(null);
}
}
4 changes: 2 additions & 2 deletions src/test/java/ua_parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ public void testParseAll() {

Client expected1 = new Client(new UserAgent("Firefox", "3", "5", "5"),
new OS("Mac OS X", "10", "4", null, null),
new Device(null, false, false));
new Device(null));
Client expected2 = new Client(new UserAgent("Mobile Safari", "5", "1", null),
new OS("iOS", "5", "1", "1", null),
new Device("iPhone", true, false));
new Device("iPhone"));

assertThat(parser.parse(agentString1), is(expected1));
assertThat(parser.parse(agentString2), is(expected2));
Expand Down

0 comments on commit 38a3cdc

Please sign in to comment.