Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregor Herdmann committed Apr 7, 2017
1 parent a7595cb commit bdfe2fa
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 152 deletions.
4 changes: 2 additions & 2 deletions app/script/bot/BotService.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ z.bot.BotService = class BotService {
*/
fetch_bot(bot_name) {
return fetch(`${this.url}${bot_name}/`)
.then(response => response.json())
.then(json => json.result);
.then((response) => response.json())
.then(({result}) => result);
}

static get URL() {
Expand Down
45 changes: 17 additions & 28 deletions app/script/connect/ConnectGoogleService.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ z.connect.ConnectGoogleService = class ConnectGoogleService {
this.logger = new z.util.Logger('z.connect.ConnectGoogleService', z.config.LOGGER.OPTIONS);
this.client_id = '481053726221-71f8tbhghg4ug5put5v3j5pluv0di2fc.apps.googleusercontent.com';
this.scopes = 'https://www.googleapis.com/auth/contacts.readonly';
this.url = 'https://www.google.com/m8/feeds/contacts/default/full';
}

/*
Expand All @@ -45,11 +46,9 @@ z.connect.ConnectGoogleService = class ConnectGoogleService {
*/
get_contacts() {
return this._init_library()
.then(() => {
return this._get_access_token();
}).then((access_token) => {
return this._get_contacts(access_token);
}).catch((error) => {
.then(() => this._get_access_token())
.then((access_token) => this._get_contacts(access_token))
.catch((error) => {
return this.logger.error(`Failed to import contacts from Google: ${error.message}`, error);
});
}
Expand All @@ -63,14 +62,14 @@ z.connect.ConnectGoogleService = class ConnectGoogleService {
return new Promise((resolve, reject) => {
this.logger.info('Authenticating with Google for contacts access');

let on_response = (response) => {
if (!(response != null ? response.error : undefined)) {
const on_response = (response) => {
if (!(response !== null ? response.error : undefined)) {
this.logger.info('Received access token from Google', response);
return resolve(response.access_token);
}

this.logger.error('Failed to authenticate with Google', response);
return reject(response != null ? response.error : undefined);
return reject(response !== null ? response.error : undefined);
};

return window.gapi.auth.authorize({client_id: this.client_id, scope: this.scopes, immediate: false}, on_response);
Expand All @@ -84,7 +83,7 @@ z.connect.ConnectGoogleService = class ConnectGoogleService {
_get_access_token() {
return new Promise((resolve, reject) => {
if (window.gapi.auth) {
let auth_token = window.gapi.auth.getToken();
const auth_token = window.gapi.auth.getToken();
if (auth_token) {
this.logger.info('Using cached access token to access Google contacts', auth_token);
return resolve(auth_token.access_token);
Expand All @@ -93,7 +92,7 @@ z.connect.ConnectGoogleService = class ConnectGoogleService {
}

this.logger.warn('Google Auth Client for JavaScript not loaded');
let error = new z.connect.ConnectError(z.connect.ConnectError.prototype.TYPE.GOOGLE_CLIENT);
const error = new z.connect.ConnectError(z.connect.ConnectError.prototype.TYPE.GOOGLE_CLIENT);
Raygun.send(error);
return reject(error);
});
Expand All @@ -105,18 +104,11 @@ z.connect.ConnectGoogleService = class ConnectGoogleService {
@return {Promise} - Resolves with the user's contacts
*/
_get_contacts(access_token) {
return new Promise((resolve, reject) => {
this.logger.info('Fetching address book from Google');
let api_endpoint = 'https://www.google.com/m8/feeds/contacts/default/full';
return $.get(`${api_endpoint}?access_token=${access_token}&alt=json&max-results=15000&v=3.0`)
.always((data_or_jqXHR, textStatus) => {
if (textStatus !== 'error') {
this.logger.info('Received address book from Google', data_or_jqXHR);
return resolve(data_or_jqXHR);
}
this.logger.error('Failed to fetch address book from Google', data_or_jqXHR);
return reject(data_or_jqXHR.responseJSON || new z.service.BackendClientError(data_or_jqXHR.status));
});
return fetch(`${this.url}?access_token=${access_token}&alt=json&max-results=15000&v=3.0`)
.then((response) => response.json())
.then(({result}) => {
this.logger.info('Received address book from Google', result);
return result;
});
}

Expand All @@ -125,10 +117,7 @@ z.connect.ConnectGoogleService = class ConnectGoogleService {
@return {Promise} - Resolves when the authentication library is initialized
*/
_init_library() {
if (window.gapi) {
return Promise.resolve();
}
return this._load_library();
return window.gapi ? Promise.resolve() : this._load_library();
}

/*
Expand All @@ -140,9 +129,9 @@ z.connect.ConnectGoogleService = class ConnectGoogleService {
window.gapi_loaded = resolve;

this.logger.info('Lazy loading Google Auth API');
let script_node = document.createElement('script');
const script_node = document.createElement('script');
script_node.src = 'https://apis.google.com/js/auth.js?onload=gapi_loaded';
let script_element = document.getElementsByTagName('script')[0];
const script_element = document.getElementsByTagName('script')[0];
return script_element.parentNode.insertBefore(script_node, script_element);
});
}
Expand Down
Loading

0 comments on commit bdfe2fa

Please sign in to comment.