svenaas-trim 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -0
- data/lib/trim/version.rb +1 -1
- data/lib/trim.rb +23 -2
- data/test/unit/trim_test.rb +84 -16
- metadata +13 -4
data/Rakefile
CHANGED
data/lib/trim/version.rb
CHANGED
data/lib/trim.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
$:.unshift File.dirname(__FILE__)
|
2
2
|
|
3
3
|
require 'trim/version.rb'
|
4
|
-
|
5
4
|
require 'open-uri'
|
5
|
+
require 'libxml'
|
6
6
|
|
7
7
|
module Trim
|
8
8
|
|
@@ -19,11 +19,32 @@ module Trim
|
|
19
19
|
# Returns a trimmed URL based on +url+, or nil if any error occurs.
|
20
20
|
#
|
21
21
|
# trimmed_url = Trim.trim_simple('http://sample.com/some/long/url')
|
22
|
+
#
|
23
|
+
# Utilizes the tr.im API method trim_simple
|
22
24
|
#
|
23
25
|
def Trim::trim_simple(url)
|
24
26
|
response = nil
|
25
27
|
open("http://api.tr.im/api/trim_simple?url=#{url}") {|http| response = http.read }
|
26
28
|
response.strip!.size > 0 ? response : nil
|
27
29
|
end
|
28
|
-
|
30
|
+
|
31
|
+
#
|
32
|
+
# Returns a trimmed URL based on +url+, or nil if any error occurs.
|
33
|
+
#
|
34
|
+
# trimmed_url = Trim.trim_url('http://sample.com/some/long/url')
|
35
|
+
#
|
36
|
+
# Utilizes the tr.im API method trim_url.xml
|
37
|
+
#
|
38
|
+
def Trim::trim_url(url)
|
39
|
+
xml_response = nil
|
40
|
+
open("http://api.tr.im/api/trim_url.xml?url=#{url}") {|http| xml_response = http.read }
|
41
|
+
get_url_from_xml_response(xml_response)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def Trim::get_url_from_xml_response(xml_response)
|
46
|
+
parser = LibXML::XML::Parser.string xml_response
|
47
|
+
urls = parser.parse.find('url')
|
48
|
+
urls.size > 0 ? urls.first.content : nil
|
49
|
+
end
|
29
50
|
end
|
data/test/unit/trim_test.rb
CHANGED
@@ -5,28 +5,96 @@ require 'uri'
|
|
5
5
|
|
6
6
|
class TrimTest < Test::Unit::TestCase
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
8
|
+
context "A valid URL" do
|
9
|
+
|
10
|
+
setup do
|
11
|
+
@long_url = 'http://www.google.com/'
|
12
|
+
end
|
13
|
+
|
14
|
+
context "A trim_simple request" do
|
15
|
+
should "provide a working shortened URL" do
|
16
|
+
trimmed_url = Trim.trim_simple @long_url
|
17
|
+
response = Net::HTTP.get_response(URI.parse(trimmed_url))
|
18
|
+
assert response.kind_of?(Net::HTTPRedirection)
|
19
|
+
assert_equal @long_url, response['location']
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "A trim_url request" do
|
24
|
+
should "provide a working shortened URL" do
|
25
|
+
trimmed_url = Trim.trim_url @long_url
|
26
|
+
response = Net::HTTP.get_response(URI.parse(trimmed_url))
|
27
|
+
assert response.kind_of?(Net::HTTPRedirection)
|
28
|
+
assert_equal @long_url, response['location']
|
29
|
+
end
|
18
30
|
end
|
31
|
+
|
19
32
|
end
|
20
33
|
|
21
|
-
|
22
|
-
|
34
|
+
context "An invalid URL" do
|
35
|
+
|
36
|
+
setup do
|
23
37
|
@long_url = 'www'
|
24
38
|
end
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
39
|
+
|
40
|
+
context "A trim_simple request" do
|
41
|
+
should "return nil" do
|
42
|
+
trimmed_url = Trim.trim @long_url
|
43
|
+
assert_nil trimmed_url
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "A trim_url request" do
|
48
|
+
should "return nil" do
|
49
|
+
trimmed_url = Trim.trim @long_url
|
50
|
+
assert_nil trimmed_url
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
context "An empty URL" do
|
57
|
+
|
58
|
+
setup do
|
59
|
+
@long_url = ''
|
60
|
+
end
|
61
|
+
|
62
|
+
context "A trim_simple request" do
|
63
|
+
should "return nil" do
|
64
|
+
trimmed_url = Trim.trim @long_url
|
65
|
+
assert_nil trimmed_url
|
66
|
+
end
|
29
67
|
end
|
68
|
+
|
69
|
+
context "A trim_url request" do
|
70
|
+
should "return nil" do
|
71
|
+
trimmed_url = Trim.trim @long_url
|
72
|
+
assert_nil trimmed_url
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
context "A nil URL" do
|
79
|
+
|
80
|
+
setup do
|
81
|
+
@long_url = nil
|
82
|
+
end
|
83
|
+
|
84
|
+
context "A trim_simple request" do
|
85
|
+
should "return nil" do
|
86
|
+
trimmed_url = Trim.trim @long_url
|
87
|
+
assert_nil trimmed_url
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context "A trim_url request" do
|
92
|
+
should "return nil" do
|
93
|
+
trimmed_url = Trim.trim @long_url
|
94
|
+
assert_nil trimmed_url
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
30
98
|
end
|
31
99
|
|
32
100
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: svenaas-trim
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sven Aas
|
@@ -9,10 +9,19 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-03-
|
12
|
+
date: 2009-03-02 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: libxml-ruby
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
16
25
|
description:
|
17
26
|
email: sven.aas@gmail.com
|
18
27
|
executables: []
|