-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcombiningServices.js
122 lines (103 loc) · 5 KB
/
combiningServices.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* This Original Work is licensed under the European Union Public Licence
* (EUPL) v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
/**
@example combiningServices.js
Example of using the 51Degrees geo-location Cloud alongside 51Degrees device detection to determine the country and device for a given longitude, latitude and User-Agent.
This example is available in full on [GitHub](https://github.com/51Degrees/location-node/blob/main/examples/combiningServices.js).
To run this example, you will need to create a **resource key**.
The resource key is used as short-hand to store the particular set of
properties you are interested in as well as any associated license keys
that entitle you to increased request limits and/or paid-for properties.
You will also need to install the fiftyone.devicedetection package with
`npm install fiftyone.devicedetection`.
You can create a resource key using the 51Degrees [Configurator](https://configure.51degrees.com).
*/
const FiftyOneDegreesGeoLocation = require((process.env.directory || __dirname) + '/../');
let FiftyOneDegreesDeviceDetection = null;
try {
FiftyOneDegreesDeviceDetection = require('fiftyone.devicedetection');
} catch (e) {
console.log('DeviceDetection is not included in package.json to avoid an ' +
'unnecessary package dependency. If you wish to run this example ' +
'then execute "npm install fiftyone.devicedetection" and try again.');
}
let localResourceKey;
// Check if there is a resource key in the global variable and use it if
// there is one.
try {
localResourceKey = resourceKey;
} catch (e) {
if (e instanceof ReferenceError) {
console.log(e);
}
}
// You need to create a resource key at https://configure.51degrees.com and
// paste it into the code, replacing !!YOUR_RESOURCE_KEY!!.
// Make sure to include the isMobile and country properties as they
// are used by this example.
if (typeof localResourceKey === 'undefined' ||
localResourceKey.substr(0, 2) === '!!') {
localResourceKey = process.env.RESOURCE_KEY || '!!YOUR_RESOURCE_KEY!!';
}
if (localResourceKey.substr(0, 2) === '!!') {
console.log('You need to create a resource key at ' +
'https://configure.51degrees.com and paste it into the code, ' +
'replacing !!YOUR_RESOURCE_KEY!!.');
console.log('Make sure to include the ismobile property ' +
'as it is used by this example.');
} else if (FiftyOneDegreesDeviceDetection) {
const pipeline = new FiftyOneDegreesDeviceDetection.DeviceDetectionPipelineBuilder({
resourceKey: localResourceKey
})
.add(new FiftyOneDegreesGeoLocation.GeoLocationCloud({
locationProvider: 'fiftyonedegrees'
}))
.build();
// Logging of errors and other messages. Valid logs types are info, debug, warn, error
pipeline.on('error', console.error);
const getProperties = async function (latitude, longitude, userAgent) {
// Create a flow data element and process the latitude, longitude
// and User-Agent.
const flowData = pipeline.createFlowData();
// Add the longitude and latitude as evidence
flowData.evidence.add('query.51D_Pos_latitude', latitude);
flowData.evidence.add('query.51D_Pos_longitude', longitude);
flowData.evidence.add('header.user-agent', userAgent);
await flowData.process();
const country = flowData.location.country;
const isMobile = flowData.device.ismobile;
if (country.hasValue) {
console.log(`Which country is the location [${latitude},${longitude}] in? ${country.value}`);
} else {
// Echo out why the value isn't meaningful
console.log(country.noValueMessage);
}
if (isMobile.hasValue) {
console.log(`Does the User-Agent '${userAgent}' represent a mobile device? ${isMobile.value}`);
} else {
// Echo out why the value isn't meaningful
console.log(isMobile.noValueMessage);
}
};
const mobileUserAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2 like Mac OS X) AppleWebKit/604.4.7 (KHTML, like Gecko) Mobile/15C114';
getProperties('51.458048', '-0.9822207999999999', mobileUserAgent);
}