Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created January 31, 2016 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/3e17d1f33f5588371117 to your computer and use it in GitHub Desktop.
Save komasaru/3e17d1f33f5588371117 to your computer and use it in GitHub Desktop.
Ruby script to shorten a url by TinyURL.(ver.2)
#!/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