puny 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ruben Fonseca
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,32 @@
1
+ = puny
2
+
3
+ puny is a Ruby library for the URL shortener service created by SAPO
4
+
5
+ The puny URL service is available at http://puny.sapo.pt and
6
+ it's API at http://services.sapo.pt/Metadata/Service/PunyURL?culture=EN
7
+
8
+ The code is a translation of the Python library wrote by Celso Pinto
9
+ and available at http://github.com/cpinto/punypie/tree/master
10
+
11
+ == Features
12
+
13
+ * shortens and expands URLs
14
+ * no external dependencies
15
+ * Ruby 1.8, 1.9 and JRuby compatible
16
+
17
+ == Install
18
+
19
+ $ gem sources -a http://gems.github.com
20
+ $ sudo gem install rubenfonseca-puny
21
+
22
+ == Example
23
+
24
+ require 'puny'
25
+ puny = Puny.shorten('http://developers.sapo.pt')
26
+ puts puny.puny # see PunyURL for aditional properties
27
+
28
+ puts Puny.expand(puny.puny) # returns the original URL
29
+
30
+ == Copyright
31
+
32
+ Copyright (c) 2009 Ruben Fonseca. See LICENSE for details.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 1
3
+ :minor: 1
4
+ :patch: 0
data/lib/puny.rb ADDED
@@ -0,0 +1,77 @@
1
+ # -*- coding: utf-8 -*-
2
+ # vim:tabstop=4:expandtab:sw=4:softtabstop=4
3
+
4
+ require 'rexml/document'
5
+ require 'open-uri'
6
+ require 'cgi'
7
+
8
+ # == PunyURL
9
+ #
10
+ # Object that holds the different URLs available on a Puny response
11
+ #
12
+ class PunyURL
13
+ attr_accessor :puny, :ascii, :preview, :url
14
+ end
15
+
16
+ # == Puny
17
+ #
18
+ # Holds the methods available on the Puny API
19
+ #
20
+ # (see http://services.sapo.pt/Metadata/Service/PunyURL?culture=EN)
21
+ #
22
+ class Puny
23
+ TO_PUNY = 'http://services.sapo.pt/PunyURL/GetCompressedURLByURL'
24
+ FROM_PUNY = 'http://services.sapo.pt/PunyURL/GetURLByCompressedURL'
25
+
26
+ # Shortens a URL
27
+ #
28
+ # Returns a PunyURL instance with the shortened version of the URL,
29
+ # or <tt>nil</tt> if there was an error
30
+ #
31
+ # Example:
32
+ #
33
+ # require 'puny'
34
+ # puny = Puny.shorten('http://developers.sapo.pt')
35
+ # puts puny.puny
36
+ # puts puny.url
37
+ #
38
+ def self.shorten(url)
39
+ url = TO_PUNY + '?url=' + CGI.escape(url)
40
+ _process_response(open(url))
41
+ end
42
+
43
+ # Expands a puny URL to the original URL
44
+ #
45
+ # Returns a String instance with the original URL or <tt>nil</tt>
46
+ # if there was an error
47
+ #
48
+ # Example:
49
+ #
50
+ # require 'puny'
51
+ # puts Puny.expand('http://b.ot.sl.pt')
52
+ #
53
+ def self.expand(url)
54
+ url = FROM_PUNY + '?url=' + CGI.escape(url)
55
+ p = _process_response(open(url))
56
+ p.url
57
+ end
58
+
59
+ private
60
+
61
+ # Private method to parse SAPO's WS responses
62
+ def self._process_response(body)
63
+ doc = REXML::Document.new(body).root
64
+
65
+ begin
66
+ puny_url = PunyURL.new
67
+ puny_url.url = doc.elements['url'].text
68
+ puny_url.ascii = doc.elements['ascii'].text
69
+ puny_url.preview = doc.elements['preview'].text
70
+ puny_url.puny = doc.elements['puny'].text
71
+
72
+ return puny_url
73
+ rescue
74
+ nil
75
+ end
76
+ end
77
+ end
data/test/puny_test.rb ADDED
@@ -0,0 +1,51 @@
1
+ # -*- coding: utf-8 -*-
2
+ # vim:tabstop=4:expandtab:sw=4:softtabstop=4
3
+
4
+ require 'test_helper'
5
+
6
+ class PunyTest < Test::Unit::TestCase
7
+ def test_to_puny_response_handling
8
+ puny = Puny._process_response('<?xml version="1.0" encoding="utf-8"?>
9
+ <punyURL xmlns="http://services.sapo.pt/Metadata/PunyURL">
10
+ <puny>http://漭.sl.pt</puny>
11
+ <ascii>http://b.ot.sl.pt</ascii>
12
+ <preview>http://b.ot.sl.pt/-</preview>
13
+ <url><![CDATA[http://developers.sapo.pt/]]></url>
14
+ </punyURL>')
15
+
16
+ assert_equal puny.puny, 'http://漭.sl.pt'
17
+ assert_equal puny.ascii, 'http://b.ot.sl.pt'
18
+ assert_equal puny.preview, 'http://b.ot.sl.pt/-'
19
+ assert_equal puny.url, 'http://developers.sapo.pt/'
20
+ end
21
+
22
+ def test_from_puny_response_handling
23
+ puny = Puny._process_response('<?xml version="1.0" encoding="utf-8"?>
24
+ <punyURL xmlns="http://services.sapo.pt/Metadata/PunyURL">
25
+ <puny>http://漭.sl.pt</puny>
26
+ <ascii>http://b.ot.sl.pt</ascii>
27
+ <preview>http://b.ot.sl.pt/-</preview>
28
+ <url><![CDATA[http://developers.sapo.pt/]]></url>
29
+ </punyURL>')
30
+
31
+ assert_equal puny.url, 'http://developers.sapo.pt/'
32
+ end
33
+
34
+ def test_remote_puny_shorten
35
+ puny = Puny.shorten('http://developers.sapo.pt/')
36
+
37
+ assert_equal puny.puny, 'http://漭.sl.pt'
38
+ assert_equal puny.ascii, 'http://b.ot.sl.pt'
39
+ assert_equal puny.preview, 'http://b.ot.sl.pt/-'
40
+ assert_equal puny.url, 'http://developers.sapo.pt/'
41
+ end
42
+
43
+ def test_remote_puny_expand
44
+ assert_equal Puny.expand('http://漭.sl.pt'), 'http://developers.sapo.pt/'
45
+ assert_equal Puny.expand('http://b.ot.sl.pt'), 'http://developers.sapo.pt/'
46
+ end
47
+
48
+ def test_smoke
49
+ assert_equal 100.times { Puny.shorten('http://developers.sapo.pt') }, 100
50
+ end
51
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'puny'
7
+
8
+ class Test::Unit::TestCase
9
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puny
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ruben Fonseca
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-16 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: root@cpan.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - LICENSE
25
+ files:
26
+ - README.rdoc
27
+ - VERSION.yml
28
+ - lib/puny.rb
29
+ - test/puny_test.rb
30
+ - test/test_helper.rb
31
+ - LICENSE
32
+ has_rdoc: true
33
+ homepage: http://github.com/rubenfonseca/puny
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --inline-source
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.5
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: Ruby library for the URL shortener service created by SAPO
61
+ test_files: []
62
+