forked from rafmagana/mush
/
bitly
executable file
·67 lines (47 loc) · 1.28 KB
/
bitly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env ruby
STDOUT.sync = true
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
require 'rubygems'
require 'optparse'
require 'mush'
ORIGINAL_ARGV = ARGV.dup
#the defaults
options = {}
opts = OptionParser.new do |opts|
opts.banner = <<-EOF
Usage:
bitly -l login -k apikey -u url
Options:
EOF
opts.on("-lLOGIN", "--login LOGIN", "The login to access the bit.ly API") do |login|
options[:login] = login
end
opts.on("-kAPIKEY", "--apikey APIKEY", "API Key") do |apikey|
options[:apikey] = apikey
end
opts.on("-uURL", "--url URL", "The long URL to be shortened") do |url|
options[:url] = url
end
opts.on("-v", "--version", "Print the version number and exit") do
options[:version] = true
end
end
opts.parse!
if options[:version]
abort("Version " + Mush.version)
end
options[:url] = ARGV.first if ARGV.length == 1
#validate
unless options[:login]
abort("You must specify a login to access the Bit.ly API")
end
unless options[:apikey]
abort("You must specify an API Key to access the Bit.ly API")
end
unless options[:url]
abort("Please provide a valid URL, including http://")
end
bitly = Mush::Services::Bitly.new
bitly.login = options[:login]
bitly.apikey = options[:apikey]
puts bitly.shorten options[:url]