forked from googleworkspace/apps-script-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utilities.gs
68 lines (63 loc) · 2.29 KB
/
Utilities.gs
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
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Contains utility methods used by the library.
*/
/**
* Builds a complete URL from a base URL and a map of URL parameters.
* @param {string} url The base URL.
* @param {Object.<string, string>} params The URL parameters and values.
* @returns {string} The complete URL.
* @private
*/
function buildUrl_(url, params) {
var paramString = Object.keys(params).map(function(key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
}).join('&');
return url + (url.indexOf('?') >= 0 ? '&' : '?') + paramString;
}
/**
* Validates that all of the values in the object are non-empty. If an empty value is found,
* and error is thrown using the key as the name.
* @param {Object.<string, string>} params The values to validate.
* @private
*/
function validate_(params) {
Object.keys(params).forEach(function(name) {
var value = params[name];
if (isEmpty_(value)) {
throw Utilities.formatString('%s is required.', name);
}
});
}
/**
* Returns true if the given value is empty, false otherwise. An empty value is one of
* null, undefined, a zero-length string, a zero-length array or an object with no keys.
* @param {?} value The value to test.
* @returns {boolean} True if the value is empty, false otherwise.
* @private
*/
function isEmpty_(value) {
return value == null || value == undefined ||
((_.isObject(value) || _.isString(value)) && _.isEmpty(value));
}
/**
* Gets the time in seconds, rounded down to the nearest second.
* @param {Date} date The Date object to convert.
* @returns {Number} The number of seconds since the epoch.
* @private
*/
function getTimeInSeconds_(date) {
return Math.floor(date.getTime() / 1000);
}