Created
May 14, 2015 11:59
-
-
Save eyalzek/a521fd52c7d08a271cc3 to your computer and use it in GitHub Desktop.
randgif
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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