diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..54b7261 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.rb diff --git a/Gemfile b/Gemfile index a93b4de..663f8dc 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,9 @@ +source 'https://rubygems.org' gem 'thin' gem 'sinatra' +gem 'faraday' +gem 'faraday_middleware' gem 'json' +gem 'awesome_print' diff --git a/Gemfile.lock b/Gemfile.lock index 1be42a3..2d0ebfe 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,8 +1,15 @@ GEM + remote: https://rubygems.org/ specs: + awesome_print (1.6.1) daemons (1.2.2) eventmachine (1.0.7) + faraday (0.9.1) + multipart-post (>= 1.2, < 3) + faraday_middleware (0.9.1) + faraday (>= 0.7.4, < 0.10) json (1.8.2) + multipart-post (2.0.0) rack (1.6.0) rack-protection (1.5.3) rack @@ -20,6 +27,9 @@ PLATFORMS ruby DEPENDENCIES + awesome_print + faraday + faraday_middleware json sinatra thin diff --git a/app/meme_bot.rb b/app/meme_bot.rb new file mode 100644 index 0000000..31d0b92 --- /dev/null +++ b/app/meme_bot.rb @@ -0,0 +1,68 @@ +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 diff --git a/app/workers/animations.rb b/app/workers/animations.rb deleted file mode 100644 index 211277c..0000000 --- a/app/workers/animations.rb +++ /dev/null @@ -1,7 +0,0 @@ -class Animations - - def find string - "looking for a gif related to: #{string}" - end - -end diff --git a/app/workers/get_giphy.rb b/app/workers/get_giphy.rb new file mode 100644 index 0000000..6b218b8 --- /dev/null +++ b/app/workers/get_giphy.rb @@ -0,0 +1,48 @@ +require 'faraday' +require 'faraday_middleware' + +class GetGiphy + + def random search="" + url = "random?api_key=#{api_key}&rating=pg&tag=#{clean_search(search)}" + response = connection.get(url) + + response.body['data'] + end + + def search search="" + url = "search?api_key=#{api_key}&rating=pg&q=#{clean_search(search)}&limit=1&offset=#{rand(0..5)}" + response = connection.get(url) + + response.body['data'] + end + + def translate search="" + url = "translate?api_key=#{api_key}&rating=pg&tag=#{search}" + response = connection.get(url) + + response.body['data'] + end + + private + + def clean_search search + search.gsub(/ */, '+') + end + + def connection + Faraday.new(url: giphy_url) do |c| + c.use FaradayMiddleware::ParseJson, content_type: 'application/json' + c.adapter Faraday.default_adapter + end + end + + def giphy_url + "http://api.giphy.com/v1/gifs/" + end + + def api_key + "dc6zaTOxFJmzC" + end + +end diff --git a/app/workers/random_salutation.rb b/app/workers/random_salutation.rb index 6f73ebc..e51c7e2 100644 --- a/app/workers/random_salutation.rb +++ b/app/workers/random_salutation.rb @@ -1,15 +1,23 @@ class RandomSalutation def short - 'Hi there!' + [ + 'Hi there!', + 'Hello', + 'Greetings!' + ].shuffle.first end def medium - "Well hello! So nice to see you here." + [ + "Well hello! So nice to see you here.", + ].shuffle.first end def long - "My, what a wonderful day. Wouldn't you agree? I mean, it's even better now that you are here! Amirite?" + [ + "My, what a wonderful day. Wouldn't you agree? I mean, it's even better now that you are here! Amirite?", + ].shuffle.first end end diff --git a/config.ru b/config.ru index 9496cf6..e948589 100644 --- a/config.ru +++ b/config.ru @@ -3,11 +3,14 @@ require 'bundler' require 'thin' require 'sinatra' +require 'awesome_print' + $LOAD_PATH.unshift( File.join( File.dirname(__FILE__), 'app' ) ) Bundler.require -require './meme_bot' +require './config' +require './app/meme_bot' #\ -p 3000 run Sinatra::Application diff --git a/meme_bot.rb b/meme_bot.rb deleted file mode 100644 index e9235b0..0000000 --- a/meme_bot.rb +++ /dev/null @@ -1,23 +0,0 @@ -require 'json' -require 'workers/random_salutation' -require 'workers/animations' - - -get '/?' do - content_type :json - greeting = RandomSalutation.new - { message: greeting.short }.to_json -end - -get '/greet/:length/?' do - content_type :json - greeting = RandomSalutation.new - { message: greeting.send(params[:length]) }.to_json -end - -post '/animate/?' do - content_type :json - gif = Animations.new - @json = JSON.parse(request.body.read) - { message: gif.find( @json['search'] ) }.to_json -end