truncator 0.1.1 → 0.1.2
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.
- checksums.yaml +4 -4
- data/lib/truncator/url_parser.rb +5 -1
- data/lib/truncator/version.rb +1 -1
- data/spec/url_parser_spec.rb +8 -18
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 664e2bb87bc33032c0dd931d417c22d87fc0f241
|
4
|
+
data.tar.gz: e05bfc012e31afaf14ebfddde0776f6a532020e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80c8cd8755b1d0bda2c98ebbaf80522c8196d851121c4ef7b9e51c2d3986b5a1abf28b984dbb262efac38bcd224dcc9394f23c70c92b5e620fde3282c0f760fa
|
7
|
+
data.tar.gz: ab977d4f710eedffc3b9502338f7a40b8ff8a39d601e7d9ec53299ce7edb0ea575d764c055bc15a84ba75bd5281580fe33e0234a4b04c2abd9ce090a16e10dfc
|
data/lib/truncator/url_parser.rb
CHANGED
@@ -9,7 +9,11 @@ module Truncator
|
|
9
9
|
String.separator = SEPARATOR
|
10
10
|
|
11
11
|
def shorten_url(uri, truncation_length = 42)
|
12
|
-
|
12
|
+
begin
|
13
|
+
uri = URI(uri)
|
14
|
+
rescue URI::InvalidURIError
|
15
|
+
return uri.truncate(truncation_length)
|
16
|
+
end
|
13
17
|
|
14
18
|
if not uri.ordinary_hostname?
|
15
19
|
if uri.query
|
data/lib/truncator/version.rb
CHANGED
data/spec/url_parser_spec.rb
CHANGED
@@ -9,7 +9,6 @@ describe Truncator::UrlParser do
|
|
9
9
|
let(:shortened) { Truncator::UrlParser.shorten_url "http://www.foo.com/this/is/a/b/c/d/e/f/string.html" }
|
10
10
|
|
11
11
|
it "should default to truncate at 42 characters" do
|
12
|
-
# shortened.should == "www.foo.com/this/is/a/.../e/f/string.html"
|
13
12
|
shortened.length.should <= 42
|
14
13
|
end
|
15
14
|
end
|
@@ -61,24 +60,8 @@ describe Truncator::UrlParser do
|
|
61
60
|
end
|
62
61
|
end
|
63
62
|
|
64
|
-
# it "should replace path segments with ellipses to shorten the path as much as necessary for various lengths", focus: true do
|
65
|
-
# url = "http://www.foo.com/this/goes/on/and/on/and/on/with/XXX.html"
|
66
|
-
# Truncator::UrlParser.shorten_url(url, 24).should == "www.foo.com/.../XXX.html"
|
67
|
-
# 32.upto(33) { |n| Truncator::UrlParser.shorten_url(url, n).should == "www.foo.com/.../on/with/XXX.html" }
|
68
|
-
# 35.upto(39) { |n| Truncator::UrlParser.shorten_url(url, n).should == "www.foo.com/this/.../with/XXX.html" }
|
69
|
-
# 40.upto(42) { |n| Truncator::UrlParser.shorten_url(url, n).should == "www.foo.com/this/goes/.../with/XXX.html" }
|
70
|
-
# 43.upto(45) { |n| Truncator::UrlParser.shorten_url(url, n).should == "www.foo.com/this/goes/.../on/with/XXX.html" }
|
71
|
-
# end
|
72
63
|
end
|
73
64
|
|
74
|
-
# context "when URL is too long and has at least one sublevel specified as well as a query parameter" do
|
75
|
-
# let(:shortened) { Truncator::UrlParser.shorten_url("http://www.foo.com/this/goes/on/and/on/and/on/and/on/and/ends/with/XXXX.html?q=1&a=2&b=3", 50) }
|
76
|
-
|
77
|
-
# it "should replace path segments with ellipses to shorten the path as much as necessary, and show only the first param, followed by ellipses" do
|
78
|
-
# shortened.should == "www.foo.com/this/goes/.../with/XXXX.html?q=1..."
|
79
|
-
# end
|
80
|
-
# end
|
81
|
-
|
82
65
|
context "when URL is more than 30 chars long and does not have at least one sublevel specified" do
|
83
66
|
let(:shortened) { Truncator::UrlParser.shorten_url("http://www.mass.gov/?pageID=trepressrelease&L=4&L0=Home&L1=Media+%26+Publications&L2=Treasury+Press+Releases&L3=2006&sid=Ctre&b=pressrelease&f=2006_032706&csid=Ctre", 30) }
|
84
67
|
|
@@ -172,6 +155,13 @@ describe Truncator::UrlParser do
|
|
172
155
|
end
|
173
156
|
end
|
174
157
|
end
|
175
|
-
end
|
176
158
|
|
159
|
+
context 'when URL is invalid URL' do
|
160
|
+
let(:dangerous_url) { "http://www.first.army.mil/family/contentdisplayFAFP.asp?ContentID=133&SiteID=\"><script>alert(String.fromCharCode(88,83,83))</script>" }
|
161
|
+
|
162
|
+
it 'should just perform a basic truncation' do
|
163
|
+
Truncator::UrlParser.shorten_url(dangerous_url, 30).should == 'http://www.first.army.mil/f...'
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
177
167
|
end
|