-
Notifications
You must be signed in to change notification settings - Fork 372
/
GoogleUrlShortener.js
111 lines (97 loc) · 3.2 KB
/
GoogleUrlShortener.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
"use strict";
/*global require*/
var defaultValue = require("terriajs-cesium/Source/Core/defaultValue").default;
var defined = require("terriajs-cesium/Source/Core/defined").default;
var DeveloperError = require("terriajs-cesium/Source/Core/DeveloperError")
.default;
var loadJson = require("../Core/loadJson");
var loadWithXhr = require("../Core/loadWithXhr");
var RuntimeError = require("terriajs-cesium/Source/Core/RuntimeError").default;
var TerriaError = require("../Core/TerriaError");
var i18next = require("i18next").default;
var GoogleUrlShortener = function(options) {
if (!defined(options) || !defined(options.terria)) {
throw new DeveloperError("options.terria is required.");
}
this.terria = options.terria;
this.url = defaultValue(
options.url,
"https://www.googleapis.com/urlshortener/v1/url"
);
};
Object.defineProperties(GoogleUrlShortener.prototype, {
isUsable: {
get: function() {
var key = this.terria.configParameters.googleUrlShortenerKey;
return defined(key) && key !== null;
}
}
});
GoogleUrlShortener.prototype.shorten = function(url) {
if (!this.isUsable) {
throw new DeveloperError(
"GoogleUrlShortener is not usable because Terria.configPrameters.googleUrlShortenerKey is not defined."
);
}
return loadWithXhr({
url:
this.url + "?key=" + this.terria.configParameters.googleUrlShortenerKey,
method: "POST",
data: JSON.stringify({ longUrl: url }),
headers: { "Content-Type": "application/json" },
responseType: "json"
}).then(function(result) {
var hashIndex = result.id.lastIndexOf("/");
if (hashIndex === -1 || hashIndex >= result.id.length) {
throw new RuntimeError(
i18next.t("models.googleUrlShortener.unexpectedResult")
);
} else {
return result.id.substring(hashIndex + 1);
}
});
};
/**
* Expands the URL associated with a given token.
*
* @param {String} token The token for which to get the expanded URL.
* @return {Promise|Object} A promise that resolves to the expanded URL. If the token does not exist, the promise resolves to undefined.
*/
GoogleUrlShortener.prototype.expand = function(token) {
var that = this;
var corsProxy = this.terria.corsProxy;
if (!this.isUsable) {
throw new DeveloperError(
"GoogleUrlShortener is not usable because Terria.configPrameters.googleUrlShortenerKey is not defined."
);
}
var url = corsProxy.getURLProxyIfNecessary(
that.url +
"?key=" +
that.terria.configParameters.googleUrlShortenerKey +
"&shortUrl=http://goo.gl/" +
token
);
return loadJson(url)
.then(function(json) {
return json.longUrl;
})
.otherwise(function() {
that.terria.error.raiseEvent(
new TerriaError({
title: i18next.t("models.googleUrlShortener.urlNotLocatedTitle"),
message: i18next.t("models.googleUrlShortener.urlNotLocatedTitle", {
appName: that.terria.appName,
email:
'<a href="mailto:' +
that.terria.supportEmail +
'">' +
that.terria.supportEmail +
"</a>."
})
})
);
return undefined;
});
};
module.exports = GoogleUrlShortener;