require "sinatra/base"
require "json"

class GifBot < Sinatra::Base
  post "/" do
    return error("I need text and trigger_word parameters") unless params_present?
    search_term = params[:text].gsub(params[:trigger_word], "").strip
    gifs = Giphy.search(search_term)
    random_gif = gifs.sample

    content_type :json
    { text: random_gif.original_image.url }.to_json
  end

  def params_present?
    params.has_key?("text") && params.has_key?("trigger_word")
  end

  def error(error_message)
    { text: error_message }.to_json
  end
end

require "sinatra/base"
require "json"

class GifBot < Sinatra::Base
  post "/" do
    content_type :json

    if params_present? && random_gif
      format_message(random_gif.original_image.url)
    else
      format_message("I couldn't find a gif :cry:")
    end
  end

  def params_present?
    params.has_key?("text") && params.has_key?("trigger_word")
  end

  def format_message(message)
    { text: message }.to_json
  end

  def random_gif
    @random_gif ||= Giphy.search(search_term).sample
  end

  def search_term
    params[:text].gsub(params[:trigger_word], "").strip
  end
end

Return meaningful message when no gif found.
Return meaningful message when no gif found.
