recortas 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
- data/README.markdown +39 -0
- data/Rakefile +15 -0
- data/lib/recortas.rb +111 -0
- data/recortas.gemspec +41 -0
- metadata +59 -0
data/.gitignore
ADDED
data/README.markdown
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# recortas A ruby wrapper for the [cort.as](http://cort.as) service
|
|
2
|
+
|
|
3
|
+
recortas is a basic ruby wrapper for the [soitu.es](http://soitu.es/) [cort.as](http://cort.as/) service.
|
|
4
|
+
|
|
5
|
+
Currently only have two methods: one for decoding and one for encoding an URI. Support for statistics is planned
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
To use recortas you only have to require it in your script
|
|
10
|
+
|
|
11
|
+
require 'rubygems'
|
|
12
|
+
require 'recortas'
|
|
13
|
+
|
|
14
|
+
**Encoding an URI**
|
|
15
|
+
|
|
16
|
+
Recortas.encode(uri)
|
|
17
|
+
|
|
18
|
+
Returns a string with the shortened URI. Raises CortasError if the +uri+ cannot be shortened
|
|
19
|
+
|
|
20
|
+
begin
|
|
21
|
+
short = Recortas.encode('blalal')
|
|
22
|
+
rescue CortasError => e
|
|
23
|
+
puts e # Error returned from cort.as service
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
**Decoding an existing URI**
|
|
27
|
+
|
|
28
|
+
Recortas.decode(uri)
|
|
29
|
+
|
|
30
|
+
Returns a string with the original URI. The uri parameter must be a valid cort.as uri (i.e. must start with 'http://cort.as')
|
|
31
|
+
|
|
32
|
+
If an error occurs, an exception of class CortasError is raised.
|
|
33
|
+
|
|
34
|
+
begin
|
|
35
|
+
Recortas.decode('http://www.google.com')
|
|
36
|
+
rescue CortasError => e
|
|
37
|
+
puts e # Not a valid cort.as URI
|
|
38
|
+
end
|
|
39
|
+
|
data/Rakefile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
begin
|
|
2
|
+
require 'jeweler'
|
|
3
|
+
Jeweler::Tasks.new do |gemspec|
|
|
4
|
+
gemspec.name = "recortas"
|
|
5
|
+
gemspec.version = "0.1.0"
|
|
6
|
+
gemspec.summary = "A ruby wrapper for the http://cort.as service"
|
|
7
|
+
gemspec.description = "A ruby wrapper for the http://cort.as service"
|
|
8
|
+
gemspec.email = "antarticonorte@gmail.com"
|
|
9
|
+
gemspec.homepage = "http://github.com/afgomez/recortas"
|
|
10
|
+
gemspec.authors = ["Alejandro Fernández"]
|
|
11
|
+
end
|
|
12
|
+
Jeweler::GemcutterTasks.new
|
|
13
|
+
rescue LoadError
|
|
14
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
|
15
|
+
end
|
data/lib/recortas.rb
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# = recortas A ruby wrapper for the http://cort.as service
|
|
2
|
+
#
|
|
3
|
+
# recortas is a basic ruby wrapper for the http://soitu.es http://cort.as service.
|
|
4
|
+
#
|
|
5
|
+
# Currently only have two methods: one for decoding and one for encoding an URI.
|
|
6
|
+
# Support for statistics is planned
|
|
7
|
+
#
|
|
8
|
+
# == Encoding an URI
|
|
9
|
+
#
|
|
10
|
+
# Recortas.encode(uri)
|
|
11
|
+
#
|
|
12
|
+
# Returns a string with the shortened URI
|
|
13
|
+
#
|
|
14
|
+
# == Decoding an cort.as URI
|
|
15
|
+
#
|
|
16
|
+
# Recortas.decode(uri)
|
|
17
|
+
#
|
|
18
|
+
# Returns a string with the original URI. The uri parameter must be a valid cort.as uri (i.e. must start with 'http://cort.as')
|
|
19
|
+
#
|
|
20
|
+
# If an error occurs, an exception of class CortasError is raised.
|
|
21
|
+
#
|
|
22
|
+
# begin
|
|
23
|
+
# Recortas.decode('http://www.google.com')
|
|
24
|
+
# rescue CortasError => e
|
|
25
|
+
# puts e # Not a valid cort.as URI
|
|
26
|
+
# end
|
|
27
|
+
#
|
|
28
|
+
# Author:: Alejandro Fernández Gómez, antarticonorte@gmail.com
|
|
29
|
+
# Version:: 0.1
|
|
30
|
+
# License:: BSD
|
|
31
|
+
#
|
|
32
|
+
# Redistribution and use in source and binary forms, with or without
|
|
33
|
+
# modification, are permitted provided that the following conditions are met:
|
|
34
|
+
# * Redistributions of source code must retain the above copyright
|
|
35
|
+
# notice, this list of conditions and the following disclaimer.
|
|
36
|
+
# * Redistributions in binary form must reproduce the above copyright
|
|
37
|
+
# notice, this list of conditions and the following disclaimer in the
|
|
38
|
+
# documentation and/or other materials provided with the distribution.
|
|
39
|
+
# * Neither the name of the <organization> nor the
|
|
40
|
+
# names of its contributors may be used to endorse or promote products
|
|
41
|
+
# derived from this software without specific prior written permission.
|
|
42
|
+
#
|
|
43
|
+
# THIS SOFTWARE IS PROVIDED BY Alejandro Fernández Gómez ''AS IS'' AND ANY
|
|
44
|
+
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
45
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
46
|
+
# DISCLAIMED. IN NO EVENT SHALL Alejandro Fernández Gómez BE LIABLE FOR ANY
|
|
47
|
+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
48
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
49
|
+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
50
|
+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
51
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
52
|
+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
53
|
+
|
|
54
|
+
require 'net/http'
|
|
55
|
+
require 'rexml/document'
|
|
56
|
+
|
|
57
|
+
# = CortasError
|
|
58
|
+
# Exception raised when:
|
|
59
|
+
# * A URI cannot be encoded with Recortas.encode
|
|
60
|
+
# * An invalid URI is passed to Recortas.decode
|
|
61
|
+
class CortasError < ArgumentError; end
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class Recortas
|
|
65
|
+
|
|
66
|
+
# Get the original URI shortened with cort.as as a string
|
|
67
|
+
#
|
|
68
|
+
# Usage example
|
|
69
|
+
# original = Recortas.decode('http://cort.as/1') # Returns 'http://www.yahoo.com/'
|
|
70
|
+
#
|
|
71
|
+
# Raises CortasError when +uri+ is not a cort.as URI
|
|
72
|
+
|
|
73
|
+
def self.decode(uri)
|
|
74
|
+
|
|
75
|
+
raise CortasError, "Not a valid cort.as URI" unless uri =~ /^http:\/\/cort\.as/i
|
|
76
|
+
|
|
77
|
+
xml_data = Net::HTTP.get_response(URI.parse("#{uri}.xml")).body
|
|
78
|
+
xml_doc = REXML::Document.new(xml_data)
|
|
79
|
+
|
|
80
|
+
if xml_doc.root.children[0].text == 'ok'
|
|
81
|
+
xml_doc.root.children[1].text
|
|
82
|
+
else
|
|
83
|
+
raise URIError, REXML::XPath.first(xml_doc, '//errorLong').text
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Sends an URI to http://cort.as to be shortened
|
|
88
|
+
#
|
|
89
|
+
# Usage example
|
|
90
|
+
# short = Recortas.encode('http://www.yahoo.com') # Returns 'http://cort.as/1'
|
|
91
|
+
#
|
|
92
|
+
# Raises CortasError if the +uri+ cannot be shortened
|
|
93
|
+
#
|
|
94
|
+
# begin
|
|
95
|
+
# short = Recortas.encode('blalal')
|
|
96
|
+
# rescue CortasError => e
|
|
97
|
+
# puts e # Error returned from cort.as service
|
|
98
|
+
# end
|
|
99
|
+
|
|
100
|
+
def self.encode(uri)
|
|
101
|
+
|
|
102
|
+
xml_data = Net::HTTP.get_response(URI.parse("http://www.soitu.es/cortas/encode.pl?r=xml&u=#{uri}")).body
|
|
103
|
+
xml_doc = REXML::Document.new(xml_data)
|
|
104
|
+
|
|
105
|
+
if xml_doc.root.children[0].text == 'ok'
|
|
106
|
+
REXML::XPath.first(xml_doc, '//urlCortas').text
|
|
107
|
+
else
|
|
108
|
+
raise CortasError, REXML::XPath.first(xml_doc, '//errorLong').text
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
data/recortas.gemspec
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Generated by jeweler
|
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
|
4
|
+
# -*- encoding: utf-8 -*-
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |s|
|
|
7
|
+
s.name = %q{recortas}
|
|
8
|
+
s.version = "0.1.0"
|
|
9
|
+
|
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
|
+
s.authors = ["Alejandro Fern\303\241ndez"]
|
|
12
|
+
s.date = %q{2010-01-09}
|
|
13
|
+
s.description = %q{A ruby wrapper for the http://cort.as service}
|
|
14
|
+
s.email = %q{antarticonorte@gmail.com}
|
|
15
|
+
s.extra_rdoc_files = [
|
|
16
|
+
"README.markdown"
|
|
17
|
+
]
|
|
18
|
+
s.files = [
|
|
19
|
+
".gitignore",
|
|
20
|
+
"README.markdown",
|
|
21
|
+
"Rakefile",
|
|
22
|
+
"lib/recortas.rb",
|
|
23
|
+
"recortas.gemspec"
|
|
24
|
+
]
|
|
25
|
+
s.homepage = %q{http://github.com/afgomez/recortas}
|
|
26
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
|
27
|
+
s.require_paths = ["lib"]
|
|
28
|
+
s.rubygems_version = %q{1.3.5}
|
|
29
|
+
s.summary = %q{A ruby wrapper for the http://cort.as service}
|
|
30
|
+
|
|
31
|
+
if s.respond_to? :specification_version then
|
|
32
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
|
33
|
+
s.specification_version = 3
|
|
34
|
+
|
|
35
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
|
36
|
+
else
|
|
37
|
+
end
|
|
38
|
+
else
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
metadata
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: recortas
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- "Alejandro Fern\xC3\xA1ndez"
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2010-01-09 00:00:00 +01:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description: A ruby wrapper for the http://cort.as service
|
|
17
|
+
email: antarticonorte@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files:
|
|
23
|
+
- README.markdown
|
|
24
|
+
files:
|
|
25
|
+
- .gitignore
|
|
26
|
+
- README.markdown
|
|
27
|
+
- Rakefile
|
|
28
|
+
- lib/recortas.rb
|
|
29
|
+
- recortas.gemspec
|
|
30
|
+
has_rdoc: true
|
|
31
|
+
homepage: http://github.com/afgomez/recortas
|
|
32
|
+
licenses: []
|
|
33
|
+
|
|
34
|
+
post_install_message:
|
|
35
|
+
rdoc_options:
|
|
36
|
+
- --charset=UTF-8
|
|
37
|
+
require_paths:
|
|
38
|
+
- lib
|
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: "0"
|
|
44
|
+
version:
|
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - ">="
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: "0"
|
|
50
|
+
version:
|
|
51
|
+
requirements: []
|
|
52
|
+
|
|
53
|
+
rubyforge_project:
|
|
54
|
+
rubygems_version: 1.3.5
|
|
55
|
+
signing_key:
|
|
56
|
+
specification_version: 3
|
|
57
|
+
summary: A ruby wrapper for the http://cort.as service
|
|
58
|
+
test_files: []
|
|
59
|
+
|