Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active April 19, 2018 06:03
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/dd2a73954811406c68f3 to your computer and use it in GitHub Desktop.
Save komasaru/dd2a73954811406c68f3 to your computer and use it in GitHub Desktop.
Ruby script to shorten a url by TinyURL.
#!/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