49 lines
978 B
Ruby
49 lines
978 B
Ruby
|
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
|