caleb-shorturl 0.8.7 → 0.8.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ require "rubygems"
2
+ require "shorturl"
3
+
4
+
5
+ def usage
6
+ puts "Usage: #$0 <url> [<service>]"
7
+ puts "Valid services:"
8
+ ShortURL.valid_services.map { |s| s.to_s }.sort.each { |s| puts "\t#{s}" }
9
+ end
10
+
11
+ def main
12
+ begin
13
+ case ARGV.length
14
+ when 0: usage
15
+ when 1: puts ShortURL.shorten(ARGV[0])
16
+ else puts ShortURL.shorten(ARGV[0], ARGV[1].to_sym)
17
+ end
18
+ rescue InvalidService
19
+ puts "Invalid service"
20
+ usage
21
+ end
22
+ end
23
+
24
+
25
+ if $0 == __FILE__
26
+ main
27
+ end
@@ -0,0 +1,217 @@
1
+ # shorturl.rb
2
+ #
3
+ # Created by Vincent Foley on 2005-06-02
4
+ #
5
+
6
+ require "net/http"
7
+ require "cgi"
8
+ require "uri"
9
+
10
+ class InvalidService < Exception
11
+ end
12
+
13
+ class Service
14
+ attr_accessor :port, :code, :method, :action, :field, :block
15
+
16
+ # Intialize the service with a hostname (required parameter) and you
17
+ # can override the default values for the HTTP port, expected HTTP
18
+ # return code, the form method to use, the form action, the form
19
+ # field which contains the long URL, and the block of what to do
20
+ # with the HTML code you get.
21
+ def initialize(hostname) # :yield: service
22
+ @hostname = hostname
23
+ @port = 80
24
+ @code = 200
25
+ @method = :post
26
+ @action = "/"
27
+ @field = "url"
28
+ @block = lambda { |body| }
29
+
30
+ if block_given?
31
+ yield self
32
+ end
33
+ end
34
+
35
+ # Now that our service is set up, call it with all the parameters to
36
+ # (hopefully) return only the shortened URL.
37
+ def call(url)
38
+ Net::HTTP.start(@hostname, @port) { |http|
39
+ response = case @method
40
+ when :post then http.post(@action, "#{@field}=#{url}")
41
+ when :get then http.get("#{@action}?#{@field}=#{CGI.escape(url)}")
42
+ end
43
+ if response.code == @code.to_s
44
+ @block.call(response.read_body)
45
+ end
46
+ }
47
+ end
48
+ end
49
+
50
+ class ShortURL
51
+ # Hash table of all the supported services. The key is a symbol
52
+ # representing the service (usually the hostname minus the .com,
53
+ # .net, etc.) The value is an instance of Service with all the
54
+ # parameters set so that when +instance+.call is invoked, the
55
+ # shortened URL is returned.
56
+ @@services = {
57
+ :rubyurl => Service.new("rubyurl.com") { |s|
58
+ s.action = "/rubyurl/remote"
59
+ s.field = "website_url"
60
+ s.block = lambda { |body| URI.extract(body).grep(/rubyurl/)[0] }
61
+ },
62
+
63
+ :tinyurl => Service.new("tinyurl.com") { |s|
64
+ s.action = "/api-create.php"
65
+ s.method = :get
66
+ s.block = lambda { |body| URI.extract(body).grep(/tinyurl/)[0] }
67
+ },
68
+
69
+ :shorl => Service.new("shorl.com") { |s|
70
+ s.action = "/create.php"
71
+ s.block = lambda { |body| URI.extract(body)[2] }
72
+ },
73
+
74
+ :snipurl => Service.new("snipurl.com") { |s|
75
+ s.action = "/index.php"
76
+ s.field = "link"
77
+ s.block = lambda { |body|
78
+ line = body.split("\n").grep(/txt/)[0]
79
+ short_url = URI.extract(line)[1][0..-2] # Remove trailing '
80
+ }
81
+ },
82
+
83
+ :metamark => Service.new("metamark.net") { |s|
84
+ s.action = "/add"
85
+ s.field = "long_url"
86
+ s.block = lambda { |body| URI.extract(body).grep(/xrl.us/)[0] }
87
+ },
88
+
89
+ :makeashorterlink => Service.new("makeashorterlink.com") { |s|
90
+ s.action = "/index.php"
91
+ s.block = lambda { |body| URI.extract(body).grep(/makeashorterlink/)[0] }
92
+ },
93
+
94
+ :skinnylink => Service.new("skinnylink.com") { |s|
95
+ s.block = lambda { |body| URI.extract(body).grep(/skinnylink/)[0] }
96
+ },
97
+
98
+ :linktrim => Service.new("linktrim.com") { |s|
99
+ s.method = :get
100
+ s.action = "/lt/generate"
101
+ s.block = lambda { |body| URI.extract(body).grep(/\/linktrim/)[1] }
102
+ },
103
+
104
+ :shorterlink => Service.new("shorterlink.com") { |s|
105
+ s.method = :get
106
+ s.action = "/add_url.html"
107
+ s.block = lambda { |body| URI.extract(body).grep(/shorterlink/)[0] }
108
+ },
109
+
110
+ :minilink => Service.new("minilink.org") { |s|
111
+ s.method = :get
112
+ s.block = lambda { |body| URI.extract(body)[-1] }
113
+ },
114
+
115
+ :lns => Service.new("ln-s.net") { |s|
116
+ s.method = :get
117
+ s.action = "/home/api.jsp"
118
+ s.block = lambda { |body| URI.extract(body)[0] }
119
+ },
120
+
121
+ :fyad => Service.new("fyad.org") { |s|
122
+ s.method = :get
123
+ s.block = lambda { |body| URI.extract(body).grep(/fyad.org/)[2] }
124
+ },
125
+
126
+ :d62 => Service.new("d62.net") { |s|
127
+ s.method = :get
128
+ s.block = lambda { |body| URI.extract(body)[0] }
129
+ },
130
+
131
+ :shiturl => Service.new("shiturl.com") { |s|
132
+ s.method = :get
133
+ s.action = "/make.php"
134
+ s.block = lambda { |body| URI.extract(body).grep(/shiturl/)[0] }
135
+ },
136
+
137
+ :littlink => Service.new("littlink.com") { |s|
138
+ s.block = lambda { |body| URI.extract(body).grep(/littlink/)[0] }
139
+ },
140
+
141
+ :clipurl => Service.new("clipurl.com") { |s|
142
+ s.action = "/create.asp"
143
+ s.block = lambda { |body| URI.extract(body).grep(/clipurl/)[0] }
144
+ },
145
+
146
+ :shortify => Service.new("shortify.com") { |s|
147
+ s.method = :get
148
+ s.action = "/shorten.php"
149
+ s.block = lambda { |body| URI.extract(body).grep(/shortify/)[0] }
150
+ },
151
+
152
+ :orz => Service.new("0rz.net") { |s|
153
+ s.action = "/create.php"
154
+ s.block = lambda { |body| URI.extract(body).grep(/0rz/)[0] }
155
+ },
156
+
157
+ :moourl => Service.new("moourl.com") { |s|
158
+ s.code = 302
159
+ s.action = "/create/"
160
+ s.method = :get
161
+ s.field = "source"
162
+ s.block = lambda { |body| body.gsub('Location/woot/?moo=','http://moourl.com/') }
163
+ },
164
+
165
+ :urltea => Service.new("urltea.com") { |s|
166
+ s.method = :get
167
+ s.action = "/create/"
168
+ s.block = lambda { |body| URI.extract(body).grep(/urltea/)[6] }
169
+ }
170
+ }
171
+
172
+ # Array containing symbols representing all the implemented URL
173
+ # shortening services
174
+ @@valid_services = @@services.keys
175
+
176
+ # Returns @@valid_services
177
+ def self.valid_services
178
+ @@valid_services
179
+ end
180
+
181
+ # Main method of ShortURL, its usage is quite simple, just give an
182
+ # url to shorten and an optional service. If no service is
183
+ # selected, RubyURL.com will be used. An invalid service symbol
184
+ # will raise an ArgumentError exception
185
+ #
186
+ # Valid +service+ values:
187
+ #
188
+ # * <tt>:rubyurl</tt>
189
+ # * <tt>:tinyurl</tt>
190
+ # * <tt>:shorl</tt>
191
+ # * <tt>:snipurl</tt>
192
+ # * <tt>:metamark</tt>
193
+ # * <tt>:makeashorterlink</tt>
194
+ # * <tt>:skinnylink</tt>
195
+ # * <tt>:linktrim</tt>
196
+ # * <tt>:shorterlink</tt>
197
+ # * <tt>:minlink</tt>
198
+ # * <tt>:lns</tt>
199
+ # * <tt>:fyad</tt>
200
+ # * <tt>:d62</tt>
201
+ # * <tt>:shiturl</tt>
202
+ # * <tt>:littlink</tt>
203
+ # * <tt>:clipurl</tt>
204
+ # * <tt>:shortify</tt>
205
+ # * <tt>:orz</tt>
206
+ #
207
+ # call-seq:
208
+ # ShortURL.shorten("http://mypage.com") => Uses RubyURL
209
+ # ShortURL.shorten("http://mypage.com", :tinyurl)
210
+ def self.shorten(url, service = :rubyurl)
211
+ if valid_services.include? service
212
+ @@services[service].call(url)
213
+ else
214
+ raise InvalidService
215
+ end
216
+ end
217
+ end
@@ -0,0 +1,75 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{shorturl}
8
+ s.version = "0.8.9"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Robby Russell"]
12
+ s.date = %q{2009-09-09}
13
+ s.default_executable = %q{shorturl}
14
+ s.description = %q{Shortens URLs using services such as RubyURL, urlTea, and TinyURL}
15
+ s.email = %q{robby@planetargon.com}
16
+ s.executables = ["shorturl"]
17
+ s.extra_rdoc_files = [
18
+ "ChangeLog",
19
+ "README"
20
+ ]
21
+ s.files = [
22
+ "ChangeLog",
23
+ "MIT-LICENSE",
24
+ "README",
25
+ "Rakefile",
26
+ "TODO",
27
+ "VERSION.yml",
28
+ "bin/shorturl",
29
+ "doc/classes/InvalidService.html",
30
+ "doc/classes/Service.html",
31
+ "doc/classes/Service.src/M000003.html",
32
+ "doc/classes/Service.src/M000004.html",
33
+ "doc/classes/ShortURL.html",
34
+ "doc/classes/ShortURL.src/M000001.html",
35
+ "doc/classes/ShortURL.src/M000002.html",
36
+ "doc/created.rid",
37
+ "doc/files/ChangeLog.html",
38
+ "doc/files/MIT-LICENSE.html",
39
+ "doc/files/README.html",
40
+ "doc/files/TODO.html",
41
+ "doc/files/lib/shorturl_rb.html",
42
+ "doc/fr_class_index.html",
43
+ "doc/fr_file_index.html",
44
+ "doc/fr_method_index.html",
45
+ "doc/index.html",
46
+ "doc/rdoc-style.css",
47
+ "examples/shorten.rb",
48
+ "lib/shorturl.rb",
49
+ "shorturl.gemspec",
50
+ "test/tc_service.rb",
51
+ "test/tc_shorturl.rb",
52
+ "test/ts_all.rb"
53
+ ]
54
+ s.homepage = %q{http://github.com/robbyrussell/shorturl}
55
+ s.rdoc_options = ["--title", "ShortURL Documentation", "--main", "README", "-S", "-N", "--all"]
56
+ s.require_paths = ["lib"]
57
+ s.rubygems_version = %q{1.3.5}
58
+ s.summary = %q{Shortens URLs using services such as RubyURL, urlTea, and TinyURL}
59
+ s.test_files = [
60
+ "test/tc_service.rb",
61
+ "test/tc_shorturl.rb",
62
+ "test/ts_all.rb",
63
+ "examples/shorten.rb"
64
+ ]
65
+
66
+ if s.respond_to? :specification_version then
67
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
68
+ s.specification_version = 3
69
+
70
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
71
+ else
72
+ end
73
+ else
74
+ end
75
+ end
@@ -0,0 +1,50 @@
1
+ # tc_service.rb
2
+ #
3
+ # Created by Vincent Foley on 2005-06-01
4
+
5
+ $test_lib_dir = File.join(File.dirname(__FILE__), "..", "lib")
6
+ $:.unshift($test_lib_dir)
7
+
8
+ require "test/unit"
9
+ require "shorturl"
10
+
11
+
12
+ class TestService < Test::Unit::TestCase
13
+
14
+ def test_call
15
+ service = Service.new("oasdasobf")
16
+ assert_raise(SocketError) { service.call(nil) }
17
+
18
+ service = Service.new("tinyurl.com") { |s|
19
+ s.code = 404
20
+ s.action = "/create.php"
21
+ s.block = lambda { |body|
22
+ URI.extract(body).grep(/tinyurl/)[-1]
23
+ }
24
+ }
25
+ assert_nil service.call("http://www.google.com")
26
+ end
27
+
28
+ def test_initialize
29
+ service = Service.new("rubyurl.com")
30
+ assert_equal(service.port, 80)
31
+ assert_equal(service.code, 200)
32
+ assert_equal(service.method, :post)
33
+ assert_equal(service.action, "/")
34
+ assert_equal(service.field, "url")
35
+
36
+ service = Service.new("rubyurl.com") { |s|
37
+ s.port = 8080
38
+ s.code = 302
39
+ s.method = :get
40
+ s.action = "/create.php"
41
+ s.field = "link"
42
+ }
43
+ assert_equal(service.port, 8080)
44
+ assert_equal(service.code, 302)
45
+ assert_equal(service.method, :get)
46
+ assert_equal(service.action, "/create.php")
47
+ assert_equal(service.field, "link")
48
+ end
49
+
50
+ end
@@ -0,0 +1,35 @@
1
+ # tc_shortcut.rb
2
+ #
3
+ # Created by Vincent Foley on 2005-06-01
4
+
5
+ $test_lib_dir = File.join(File.dirname(__FILE__), "..", "lib")
6
+ $:.unshift($test_lib_dir)
7
+
8
+ require "test/unit"
9
+ require "shorturl"
10
+
11
+ class String
12
+ def url?
13
+ self[0..6].downcase == "http://"
14
+ end
15
+ end
16
+
17
+ class TestShortURL < Test::Unit::TestCase
18
+ def setup
19
+ @url = "http://groups.google.com/group/comp.lang.ruby/"
20
+ end
21
+
22
+ def test_shorten
23
+ # Default service (RubyURL)
24
+ assert ShortURL.shorten(@url).url?
25
+
26
+ # All the services (I can't test exact URLs since they seem to
27
+ # # change semi regularly)
28
+ # ShortURL.valid_services.each do |service|
29
+ # assert ShortURL.shorten(@url, service).url?
30
+ # end
31
+
32
+ # An invalid service
33
+ assert_raise(InvalidService) { ShortURL.shorten(@url, :foobar) }
34
+ end
35
+ end
metadata CHANGED
@@ -1,33 +1,61 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caleb-shorturl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.7
4
+ version: 0.8.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robby Russell
8
- autorequire: shorturl
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-16 00:00:00 -07:00
12
+ date: 2009-09-09 00:00:00 -07:00
13
13
  default_executable: shorturl
14
14
  dependencies: []
15
15
 
16
- description:
16
+ description: Shortens URLs using services such as RubyURL, urlTea, and TinyURL
17
17
  email: robby@planetargon.com
18
18
  executables:
19
19
  - shorturl
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
+ - ChangeLog
23
24
  - README
24
- - TODO
25
- - MIT-LICENSE
25
+ files:
26
26
  - ChangeLog
27
- files: []
28
-
29
- has_rdoc: true
30
- homepage: http://github.com/robbyrussell/shorturl/
27
+ - MIT-LICENSE
28
+ - README
29
+ - Rakefile
30
+ - TODO
31
+ - VERSION.yml
32
+ - bin/shorturl
33
+ - doc/classes/InvalidService.html
34
+ - doc/classes/Service.html
35
+ - doc/classes/Service.src/M000003.html
36
+ - doc/classes/Service.src/M000004.html
37
+ - doc/classes/ShortURL.html
38
+ - doc/classes/ShortURL.src/M000001.html
39
+ - doc/classes/ShortURL.src/M000002.html
40
+ - doc/created.rid
41
+ - doc/files/ChangeLog.html
42
+ - doc/files/MIT-LICENSE.html
43
+ - doc/files/README.html
44
+ - doc/files/TODO.html
45
+ - doc/files/lib/shorturl_rb.html
46
+ - doc/fr_class_index.html
47
+ - doc/fr_file_index.html
48
+ - doc/fr_method_index.html
49
+ - doc/index.html
50
+ - doc/rdoc-style.css
51
+ - examples/shorten.rb
52
+ - lib/shorturl.rb
53
+ - shorturl.gemspec
54
+ - test/tc_service.rb
55
+ - test/tc_shorturl.rb
56
+ - test/ts_all.rb
57
+ has_rdoc: false
58
+ homepage: http://github.com/robbyrussell/shorturl
31
59
  post_install_message:
32
60
  rdoc_options:
33
61
  - --title
@@ -56,7 +84,10 @@ requirements: []
56
84
  rubyforge_project:
57
85
  rubygems_version: 1.2.0
58
86
  signing_key:
59
- specification_version: 2
87
+ specification_version: 3
60
88
  summary: Shortens URLs using services such as RubyURL, urlTea, and TinyURL
61
89
  test_files:
90
+ - test/tc_service.rb
91
+ - test/tc_shorturl.rb
62
92
  - test/ts_all.rb
93
+ - examples/shorten.rb