<commit_before>'use strict';

var $ = require('jquery-deferred');
var _ = require('underscore');
var Imgur = require('./Imgur');

module.exports = {

    /**
     * generic request parser,
     * override this in subclasses
     * @param {object} reqData  - the hipchat request body
     * @returns {jquery-deferred} - promise
     *          resolves with generic hipchat message
     *          should reject with hipchat error message in subclasses
     */
    parseReq : function (reqData) {

        var def = $.Deferred();
        var sender = this.getSenderHandle(reqData);
        def.resolve({
            color: 'green',
            message_prefix: 'Message Prefix:',
            message: 'oh hey, ' + sender + '. it\'s a generic bot.',
            message_format: "text"
        });

        return def.promise();
    },

    /**
     * parses chat message to return gif based on text after slug
     * @param {object} reqData - the hipchat request body
     * @param {string} slug (optional) - the slug to remove from the message, default is "/gif"
     * @returns {jquery-deferred} - promise
     *           resolves with hipchat response message containing gif
     *           rejects with hipchat error response if no gifs found
     */
    parseGifReq : function (data, slug) {

        var msg = data.item.message.message;
        var query = _.last(msg.split(slug || '/gif')) + ' ext:gif';

        return this.findGIF(query, data);
    },

    /**
     * searches imgur for a gif
     * @param {string} query
     * @param {object} reqData - the hipchat request body
     * @returns {jquery-deferred} - promise
     *           resolves with hipchat response message containing gif
     *           rejects with hipchat error response if no gifs found with that query
     */
    findGIF : function (query, reqData) {

        var imgur = new Imgur(process.env.IMGUR_ID);
        var def = $.Deferred();
        var handle = this.getSenderHandle(reqData);

        imgur.getRandomFromSearch(encodeURIComponent(query))
            .done(function(resp) {
                def.resolve({
                    color: "green",
                    message_prefix: "GIF:",
                    message: resp.link,
                    message_format: "text"
                });
            })
            .fail(function(err){
                def.reject({
                    color: "red",
                    message_prefix: "Whoops:",
                    message: 'Sorry, ' + handle + '. I couldn\'t find a GIF with that query. I suck.',<commit_msg>allowing imgur to search for regular images instead of gifs<commit_after>'use strict';

var $ = require('jquery-deferred');
var _ = require('underscore');
var Imgur = require('./Imgur');

module.exports = {

    /**
     * generic request parser,
     * override this in subclasses
     * @param {object} reqData  - the hipchat request body
     * @returns {jquery-deferred} - promise
     *          resolves with generic hipchat message
     *          should reject with hipchat error message in subclasses
     */
    parseReq : function (reqData) {

        var def = $.Deferred();
        var sender = this.getSenderHandle(reqData);
        def.resolve({
            color: 'green',
            message_prefix: 'Message Prefix:',
            message: 'oh hey, ' + sender + '. it\'s a generic bot.',
            message_format: "text"
        });

        return def.promise();
    },

    /**
     * parses chat message to return gif based on text after slug
     * @param {object} reqData - the hipchat request body
     * @param {string} slug (optional) - the slug to remove from the message, default is "/gif"
     * @returns {jquery-deferred} - promise
     *           resolves with hipchat response message containing gif
     *           rejects with hipchat error response if no gifs found
     */
    parseGifReq : function (data, slug) {

        var msg = data.item.message.message;
        var query = _.last(msg.split(slug || '/gif')).trim() + ' ext:gif';
        return this.findImg(query, data);
    },

    /**
     * searches imgur for an img
     * @param {string} query
     * @param {object} reqData - the hipchat request body
     * @returns {jquery-deferred} - promise
     *           resolves with hipchat response message containing img
     *           rejects with hipchat error response if no imgs found with that query
     */
    findImg : function (query, reqData) {

        var imgur = new Imgur(process.env.IMGUR_ID);
        var def = $.Deferred();
        var handle = this.getSenderHandle(reqData);
        imgur.getRandomFromSearch(encodeURIComponent(query))
            .done(function(resp) {
                def.resolve({
                    color: "green",
                    message_prefix: "GIF:",
                    message: resp.link,
                    message_format: "text"
                });
            })
            .fail(function(err){
                def.reject({
                    color: "red",
                    message_prefix: "Whoops:",
                    message: 'Sorry, ' + handle + '. I couldn\'t find a GIF with that query. I suck.',
{'fraction_non_alphanumeric': 0.10773694390715667, 'fraction_numerical': 0.0, 'mean_word_length': 1.8427707531610775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5484609', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1369, 'n_words': 436}