shorturl 0.8.6 → 0.8.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/ChangeLog +14 -6
  2. data/lib/shorturl.rb +79 -62
  3. metadata +2 -2
data/ChangeLog CHANGED
@@ -1,20 +1,28 @@
1
+ 0.8.7:
2
+ - Added support for ur1.ca (thanks to raneksi)
3
+ - Added support for bit.ly (thanks to raneksi)
4
+ - Fixed support for snipurl.com (thanks to raneksi)
5
+ - Fixed support for shortify (thanks to raneksi)
6
+ - Fixed support for moourl.com (thanks to raneksi)
7
+ - Removed support for makeashorterlink.com (thanks to raneksi)
8
+
1
9
  0.8.4:
2
10
  - Fixed support for RubyURL.com
3
11
  - Added support for urlTea.com
4
12
  - Added support for moourl.com
5
-
13
+
6
14
  0.7.0:
7
15
  - Added shortify.com, 0rz.net
8
16
  - Modified unit tests
9
17
  - Started using CVS on RubyForge
10
-
18
+
11
19
  0.6.0:
12
20
  - Changed the method for getting the URL for ln-s.net to use their
13
21
  API. So basically, this means the new additional argument attribute
14
22
  in the Service class is now useless, because no service uses it. I've
15
23
  removed it along with the Hash#to_html_args method.
16
24
  - Added d62.net, shiturl.com, littlink.com and clipurl.com
17
-
25
+
18
26
  0.5.0:
19
27
  - Added two services: fyad.org and ln-s.net (thanks to Daniel
20
28
  Dipaolo)
@@ -39,10 +47,10 @@
39
47
  0.2.1:
40
48
  - Added makeashorterlink.com and skinnylink.com
41
49
  - Refactored get_short_url
42
-
50
+
43
51
  0.2.0:
44
52
  - Added shorl.com, snipurl.com and metamark.net
45
-
53
+
46
54
  0.1.0:
47
55
  - Refactored the tinyurl and rubyurl methods
48
56
  - URI.extract to get URLs instead of regular expressions
@@ -50,6 +58,6 @@
50
58
  of a network error
51
59
  - More thorough unit tests
52
60
  - Made tinyurl, rubyurl and get_short_url private class methods
53
-
61
+
54
62
  0.0.1:
55
63
  - Initial release
@@ -7,11 +7,14 @@ require "net/http"
7
7
  require "cgi"
8
8
  require "uri"
9
9
 
10
+ class ServiceNotAvailable < Exception
11
+ end
12
+
10
13
  class InvalidService < Exception
11
14
  end
12
15
 
13
16
  class Service
14
- attr_accessor :port, :code, :method, :action, :field, :block
17
+ attr_accessor :port, :code, :method, :action, :field, :block, :response_block
15
18
 
16
19
  # Intialize the service with a hostname (required parameter) and you
17
20
  # can override the default values for the HTTP port, expected HTTP
@@ -25,7 +28,6 @@ class Service
25
28
  @method = :post
26
29
  @action = "/"
27
30
  @field = "url"
28
- @block = lambda { |body| }
29
31
 
30
32
  if block_given?
31
33
  yield self
@@ -37,13 +39,15 @@ class Service
37
39
  def call(url)
38
40
  Net::HTTP.start(@hostname, @port) { |http|
39
41
  response = case @method
40
- when :post: http.post(@action, "#{@field}=#{url}")
42
+ when :post: http.post(@action, "#{@field}=#{CGI.escape(url)}")
41
43
  when :get: http.get("#{@action}?#{@field}=#{CGI.escape(url)}")
42
44
  end
43
45
  if response.code == @code.to_s
44
- @block.call(response.read_body)
46
+ @response_block ? @response_block.call(response) : @block.call(response.read_body)
45
47
  end
46
48
  }
49
+ rescue Errno::ECONNRESET => e
50
+ raise ServiceNotAvailable, e.to_s, e.backtrace
47
51
  end
48
52
  end
49
53
 
@@ -71,13 +75,15 @@ class ShortURL
71
75
  s.block = lambda { |body| URI.extract(body)[2] }
72
76
  },
73
77
 
78
+ # SnipURL offers a restful HTTP API but it cannot be used without
79
+ # registration.
74
80
  :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
+ s.action = "/site/index"
82
+ s.field = "url"
83
+
84
+ # As with other services, this is far from elegant and might break
85
+ # any time. Might just be better to do some HTML parsing instead.
86
+ s.block = lambda { |body| URI.extract(body).grep(/http:\/\/snipurl.com/)[0] }
81
87
  },
82
88
 
83
89
  :metamark => Service.new("metamark.net") { |s|
@@ -86,27 +92,6 @@ class ShortURL
86
92
  s.block = lambda { |body| URI.extract(body).grep(/xrl.us/)[0] }
87
93
  },
88
94
 
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
95
  :minilink => Service.new("minilink.org") { |s|
111
96
  s.method = :get
112
97
  s.block = lambda { |body| URI.extract(body)[-1] }
@@ -118,40 +103,16 @@ class ShortURL
118
103
  s.block = lambda { |body| URI.extract(body)[0] }
119
104
  },
120
105
 
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
106
  :shiturl => Service.new("shiturl.com") { |s|
132
107
  s.method = :get
133
108
  s.action = "/make.php"
134
109
  s.block = lambda { |body| URI.extract(body).grep(/shiturl/)[0] }
135
110
  },
136
111
 
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|
112
+ :shortify => Service.new("shortify.wikinote.com") { |s|
147
113
  s.method = :get
148
114
  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] }
115
+ s.block = lambda { |body| URI.extract(body).grep(/shortify/)[-1] }
155
116
  },
156
117
 
157
118
  :moourl => Service.new("moourl.com") { |s|
@@ -159,14 +120,70 @@ class ShortURL
159
120
  s.action = "/create/"
160
121
  s.method = :get
161
122
  s.field = "source"
162
- s.block = lambda { |body| body.gsub('Location/woot/?moo=','http://moourl.com/') }
123
+ s.response_block = lambda { |res| "http://moourl.com/" + res["location"].match(/\?moo=/).post_match }
163
124
  },
164
-
165
- :urltea => Service.new("urltea.com") { |s|
125
+
126
+ :bitly => Service.new("bit.ly") { |s|
166
127
  s.method = :get
167
- s.action = "/create/"
168
- s.block = lambda { |body| URI.extract(body).grep(/urltea/)[6] }
128
+ s.action = "/index.php"
129
+ s.field = "url"
130
+ s.block = lambda { |body|
131
+ body.match(%r{<input id="shortened-url" value="(.*)" />}).captures[0]
132
+ }
133
+ },
134
+
135
+ :ur1 => Service.new("ur1.ca") { |s|
136
+ s.method = :post
137
+ s.action = "/"
138
+ s.field = "longurl"
139
+ s.block = lambda { |body| URI.extract(body).grep(/ur1/)[0] }
169
140
  }
141
+
142
+ # :skinnylink => Service.new("skinnylink.com") { |s|
143
+ # s.block = lambda { |body| URI.extract(body).grep(/skinnylink/)[0] }
144
+ # },
145
+
146
+ # :linktrim => Service.new("linktrim.com") { |s|
147
+ # s.method = :get
148
+ # s.action = "/lt/generate"
149
+ # s.block = lambda { |body| URI.extract(body).grep(/\/linktrim/)[1] }
150
+ # },
151
+
152
+ # :shorterlink => Service.new("shorterlink.com") { |s|
153
+ # s.method = :get
154
+ # s.action = "/add_url.html"
155
+ # s.block = lambda { |body| URI.extract(body).grep(/shorterlink/)[0] }
156
+ # },
157
+
158
+ # :fyad => Service.new("fyad.org") { |s|
159
+ # s.method = :get
160
+ # s.block = lambda { |body| URI.extract(body).grep(/fyad.org/)[2] }
161
+ # },
162
+
163
+ # :d62 => Service.new("d62.net") { |s|
164
+ # s.method = :get
165
+ # s.block = lambda { |body| URI.extract(body)[0] }
166
+ # },
167
+
168
+ # :littlink => Service.new("littlink.com") { |s|
169
+ # s.block = lambda { |body| URI.extract(body).grep(/littlink/)[0] }
170
+ # },
171
+
172
+ # :clipurl => Service.new("clipurl.com") { |s|
173
+ # s.action = "/create.asp"
174
+ # s.block = lambda { |body| URI.extract(body).grep(/clipurl/)[0] }
175
+ # },
176
+
177
+ # :orz => Service.new("0rz.net") { |s|
178
+ # s.action = "/create.php"
179
+ # s.block = lambda { |body| URI.extract(body).grep(/0rz/)[0] }
180
+ # },
181
+
182
+ # :urltea => Service.new("urltea.com") { |s|
183
+ # s.method = :get
184
+ # s.action = "/create/"
185
+ # s.block = lambda { |body| URI.extract(body).grep(/urltea/)[6] }
186
+ # }
170
187
  }
171
188
 
172
189
  # Array containing symbols representing all the implemented URL
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shorturl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.6
4
+ version: 0.8.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robby Russell
@@ -86,6 +86,6 @@ rubyforge_project:
86
86
  rubygems_version: 1.3.5
87
87
  signing_key:
88
88
  specification_version: 3
89
- summary: Shortens URLs using services such as RubyURL, urlTea, and TinyURL
89
+ summary: Shortens URLs using services such as RubyURL, urlTea, bit.ly, moourl.com, and TinyURL
90
90
  test_files:
91
91
  - test/ts_all.rb