Ruby script to shorten a url by TinyURL.
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/local/bin/ruby | |
# coding:utf-8 | |
#************************************* | |
# TinyURL API で ロング URL を短縮する | |
#************************************* | |
# | |
require 'cgi' | |
require 'open-uri' | |
class TinyurlShorten | |
URL_BASE = "http://tinyurl.com/api-create.php" | |
def initialize(arg) | |
@url_long = arg | |
end | |
def shorten | |
puts "URL(LONG) : #{@url_long}" | |
query = "url=#{URI.encode(@url_long)}" | |
url_short = open("#{URL_BASE}?#{query}") { |f| f.read } | |
puts "URL(SHORT): #{url_short}" # => http://tinyurl.com/j3nceqk | |
end | |
end | |
if __FILE__ == $0 | |
exit 0 unless arg = ARGV.shift | |
TinyurlShorten.new(arg).shorten | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment