require 'json' require 'workers/random_salutation' require 'workers/get_giphy' get '/?' do content_type :json greeting = RandomSalutation.new { text: greeting.short, }.to_json end get '/greet/:length/?' do content_type :json greeting = RandomSalutation.new { text: greeting.send(params[:length]) }.to_json end post '/random/?' do raise(InvalidTokenError) unless params[:token] == Config.team_key giphy = GetGiphy.new gif = giphy.random( params['text'] ) connection.post do |c| c.url Config.in_hook c.headers['Content-Type'] = 'application/json' c.body = { channel: "##{params['channel_name']}", text: params['text'], attachments: [{ title_link: gif['url'], image_url: gif['image_url'], fallback: 'An amazing random giphy from MemeBot', }] }.to_json end end post '/search/?' do raise(InvalidTokenError) unless params[:token] == Config.team_key giphy = GetGiphy.new gif = giphy.search( params['text'] ) connection.post do |c| c.url Config.in_hook c.headers['Content-Type'] = 'application/json' c.body = { channel: "##{params['channel_name']}", text: params['text'], attachments: [{ title_link: gif['images']['original']['url'], image_url: gif['images']['original']['url'], fallback: 'An amazing random giphy from MemeBot', }] }.to_json end end def connection Faraday.new(url: "https://hooks.slack.com/services/") do |c| c.use FaradayMiddleware::ParseJson, content_type: 'application/json' c.adapter Faraday.default_adapter end end