Skip to content

Instantly share code, notes, and snippets.

@eyalzek
Created May 14, 2015 11:59
Show Gist options
  • Save eyalzek/a521fd52c7d08a271cc3 to your computer and use it in GitHub Desktop.
Save eyalzek/a521fd52c7d08a271cc3 to your computer and use it in GitHub Desktop.
randgif
#!/usr/bin/env ruby -wKU
require 'net/http'
require 'json'
require 'launchy'
def main(query)
uri = URI('http://api.giphy.com/v1/gifs/search')
params = {q: query, api_key: 'dc6zaTOxFJmzC'}
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
body = JSON.parse(res.body)['data'].sample
begin
Launchy.open(body['images']['original']['url'])
rescue
puts 'no gifs found for \'#{query.gsub("+", " ")}\''
end
end
unless ARGV.nil?
main(ARGV.join('+'))
else
puts 'no arguments passed'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment