<commit_before>/**
 * Copyright 2014 IBM Corp.
 *
 * 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.
 **/

module.exports = function(RED) {
    "use strict";

    function GoogleCalendarQueryNode(n) {
        RED.nodes.createNode(this,n);
        this.google = RED.nodes.getNode(n.google);
        this.calendar = n.calendar || 'primary';

        this.calendars = {};
        if (!this.google || !this.google.credentials.accessToken) {
            this.warn("Missing google credentials");
            return;
        }

        var node = this;
        node.status({fill:"blue",shape:"dot",text:"querying"});
        this.google.request('https://www.googleapis.com/calendar/v3/users/me/calendarList', function(err, data) {
            if (err) {
                node.error("failed to fetch calendar list: " + err.toString());
                node.status({fill:"red",shape:"ring",text:"failed"});
                return;
            }
            if (data.error) {
                node.error("failed to fetch calendar list: " +
                           data.error.message);
                node.status({fill:"red",shape:"ring",text:"failed"});
                return;
            }
            for (var i = 0; i < data.items.length; i++) {
                var cal = data.items[i];
                if (cal.primary) {
                    node.calendars.primary = cal;
                }
                node.calendars[cal.id] = cal;
            }
            node.status({});

            node.on('input', function(msg) {
                node.status({fill:"blue",shape:"dot",text:"querying"});
                var cal = node.calendars[msg.calendar] || node.calendarByName(msg.calendar) || node.calendars[node.calendar] || node.calendarByName(node.calendar);
                if (!cal) {
                    node.status({fill:"red",shape:"ring",text:"invalid calendar"});
                    return;
                }
                var request = {
                    url: 'https://www.googleapis.com/calendar/v3/calendars/'+cal.id+'/events',
                };
                var now = new Date();
                request.qs = {
                    maxResults: 10,
                    orderBy: 'startTime',
                    singleEvents: true,
                    timeMin: now.toISOString()
                };
                if (msg.payload) {
                    request.qs.q = RED.util.ensureString(msg.payload);
                }
                node.google.request(request, function(err, data) {
                    if (err) {
                        node.error("Error: " + err.toString());
                        node.status({fill:"red",shape:"ring",text:"failed"});
                    } else if (data.error) {
                        node.error("Error " + data.error.code + ": " +
                                JSON.stringify(data.error.message));
                        node.status({fill:"red",shape:"ring",text:"failed"});
                    } else {
                        var payload = msg.payload = {};<commit_msg>Fix for issue #73.<commit_after>/**
 * Copyright 2014 IBM Corp.
 *
 * 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.
 **/

module.exports = function(RED) {
    "use strict";

    function GoogleCalendarQueryNode(n) {
        RED.nodes.createNode(this,n);
        this.google = RED.nodes.getNode(n.google);
        this.calendar = n.calendar || 'primary';

        this.calendars = {};
        if (!this.google || !this.google.credentials.accessToken) {
            this.warn("Missing google credentials");
            return;
        }

        var node = this;
        node.status({fill:"blue",shape:"dot",text:"querying"});
        this.google.request('https://www.googleapis.com/calendar/v3/users/me/calendarList', function(err, data) {
            if (err) {
                node.error("failed to fetch calendar list: " + err.toString());
                node.status({fill:"red",shape:"ring",text:"failed"});
                return;
            }
            if (data.error) {
                node.error("failed to fetch calendar list: " +
                           data.error.message);
                node.status({fill:"red",shape:"ring",text:"failed"});
                return;
            }
            for (var i = 0; i < data.items.length; i++) {
                var cal = data.items[i];
                if (cal.primary) {
                    node.calendars.primary = cal;
                }
                node.calendars[cal.id] = cal;
            }
            node.status({});

            node.on('input', function(msg) {
                node.status({fill:"blue",shape:"dot",text:"querying"});
                var cal = node.calendars[msg.calendar] || node.calendarByName(msg.calendar) || node.calendars[node.calendar] || node.calendarByName(node.calendar);
                if (!cal) {
                    node.status({fill:"red",shape:"ring",text:"invalid calendar"});
                    return;
                }
                var request = {
                    url: 'https://www.googleapis.com/calendar/v3/calendars/'+cal.id+'/events',
                };
                var now = new Date();
                request.qs = {
                    maxResults: 10,
                    orderBy: 'startTime',
                    singleEvents: true,
                    showDeleted: false,
                    timeMin: now.toISOString()
                };
                if (msg.payload) {
                    request.qs.q = RED.util.ensureString(msg.payload);
                }
                node.google.request(request, function(err, data) {
                    if (err) {
                        node.error("Error: " + err.toString());
                        node.status({fill:"red",shape:"ring",text:"failed"});
                    } else if (data.error) {
                        node.error("Error " + data.error.code + ": " +
                                JSON.stringify(data.error.message));
                        node.status({fill:"red",shape:"ring",text:"failed"});
                    } else {
                        var payload = msg.payload = {};
{'fraction_non_alphanumeric': 0.13119820199466217, 'fraction_numerical': 0.003933136676499509, 'mean_word_length': 1.5731839537405132, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6770067', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1772, 'n_words': 407}