<commit_before>require 'httparty'
require 'awesome_print'

class Djif < Sinatra::Base
  register Sinatra::Reloader

  get '/' do
    erb :application
  end

  post '/slack' do
    content_type :json

    trigger = params[:trigger_word]
    search = params[:text].split(trigger)[1]
    gif_url = get_gif_url(search)

    json_for(gif_url, search)
  end

  post '/hipchat' do
    content_type :json

    search = params[:search]
    gif_url = get_gif_url(search)

    json_for(gif_url, search)
  end

  def json_for(gif_url, search)
    text = gif_url || "No Gif found for “#{search.strip}”"
    { text: text }.to_json
  end

  def get_gif_url(search = '')
    search.strip!
    search = URI::encode(search)

    limit = 25
    offset = 0

    response = HTTParty.get("http://api.giphy.com/v1/gifs/random?api_key=#{ENV['GIPHY_API_KEY']}&tag=#{search}")
    gif = response['data']

    return nil unless gif && gif.any?
    gif['image_original_url']
  end
end<commit_msg>/search is more reliable than /random<commit_after>require 'httparty'
require 'awesome_print'

class Djif < Sinatra::Base
  register Sinatra::Reloader

  get '/' do
    erb :application
  end

  post '/slack' do
    content_type :json

    trigger = params[:trigger_word]
    search = params[:text].split(trigger)[1]
    gif_url = get_gif_url(search)

    json_for(gif_url, search)
  end

  post '/hipchat' do
    content_type :json

    search = params[:search]
    gif_url = get_gif_url(search)

    json_for(gif_url, search)
  end

  def json_for(gif_url, search)
    text = gif_url || "No Gif found for “#{search.strip}”"
    { text: text }.to_json
  end

  def get_gif_url(search = '')
    search.strip!
    search = URI::encode(search)

    limit = 25
    offset = 0

    response = HTTParty.get("http://api.giphy.com/v1/gifs/search?api_key=#{ENV['GIPHY_API_KEY']}&q=#{search}&limit=#{limit}&offset=#{offset}")
    gifs = response['data']
    return nil unless gifs.any?

    gif = gifs.sample
    gif['images']['original']['url']
  end
end
{'fraction_non_alphanumeric': 0.13505461767626614, 'fraction_numerical': 0.004965243296921549, 'mean_word_length': 3.380434782608696, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3530267', 'n_tokens_mistral': 759, 'n_tokens_neox': 722, 'n_words': 155}