Created
January 31, 2016 06:48
-
-
Save komasaru/3e17d1f33f5588371117 to your computer and use it in GitHub Desktop.
Ruby script to shorten a url by TinyURL.(ver.2)
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 'net/http' | |
require 'open-uri' | |
class TinyurlShorten | |
SERVER = "tinyurl.com" | |
PATH = "/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 = Net::HTTP.get(SERVER, "#{PATH}?#{query}") | |
puts "URL(SHORT): #{url_short}" # => http://tinyurl.com/j3nceqk | |
end | |
end | |
exit 0 unless __FILE__ == $0 | |
exit 0 unless arg = ARGV.shift | |
TinyurlShorten.new(arg).shorten |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment