truncator 0.1.2 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 664e2bb87bc33032c0dd931d417c22d87fc0f241
4
- data.tar.gz: e05bfc012e31afaf14ebfddde0776f6a532020e2
3
+ metadata.gz: a955ce36bed47fb70bdf80ca507b96eefa350305
4
+ data.tar.gz: cd95f97f6ec5bf3a7be1d927fcda8b4df55daee2
5
5
  SHA512:
6
- metadata.gz: 80c8cd8755b1d0bda2c98ebbaf80522c8196d851121c4ef7b9e51c2d3986b5a1abf28b984dbb262efac38bcd224dcc9394f23c70c92b5e620fde3282c0f760fa
7
- data.tar.gz: ab977d4f710eedffc3b9502338f7a40b8ff8a39d601e7d9ec53299ce7edb0ea575d764c055bc15a84ba75bd5281580fe33e0234a4b04c2abd9ce090a16e10dfc
6
+ metadata.gz: 7cf9be753cd247d469b1c478296e32db838c4b7ef468f369f69ed9a0c22bfa6917883560eadffb73c0de964ed7f805cecc3946faed614c4448141d2be464b857
7
+ data.tar.gz: 44f17ec44daed9292f285f68a0d0362b492e26813a27e84ad7d1c3229198326b72a6cf332e73541749f5d7798f47da2773e9c1fd1bcaa9883ee01afbce5c22a4
data/.gitignore CHANGED
@@ -17,4 +17,5 @@ test/version_tmp
17
17
  tmp
18
18
  .rspec
19
19
  .ruby-version
20
+ .ruby-gemset
20
21
  Gemfile.lock
@@ -27,23 +27,6 @@ module Truncator
27
27
  self
28
28
  end
29
29
 
30
- def last_path_with_query
31
- str = "#{self.paths.last}"
32
- if self.query
33
- str += "?#{self.query}"
34
- end
35
- str
36
- end
37
-
38
- def last_path_with_query=(str)
39
- last_path, query = str.split('?')
40
- _paths = self.paths
41
- _paths[-1] = last_path.to_s if _paths.last
42
- self.paths = _paths
43
- self.query = query
44
- self
45
- end
46
-
47
30
  def query_parameters
48
31
  URI.decode_www_form(self.query)
49
32
  end
@@ -31,17 +31,17 @@ module Truncator
31
31
  end
32
32
 
33
33
  if uri.query
34
- if uri.host.invalid_length?(truncation_length) and uri.last_path_with_query.length > truncation_length
35
- uri = truncate_last_path_segment(uri, truncation_length)
36
- elsif uri.special_format.valid_length?(truncation_length + uri.last_path_with_query.length) or not uri.path_blank?
37
34
  return uri.special_format.truncate!(truncation_length)
38
- end
39
35
  else
40
36
  if uri.host.valid_length?(truncation_length)
41
- uri = truncate_by_shortest(uri, truncation_length)
37
+ result = truncate_by_shortest(uri, truncation_length)
38
+ if result
39
+ uri = result
40
+ else
41
+ return uri.special_format.truncate!(truncation_length)
42
+ end
42
43
  else
43
- uri = truncate_all_paths_except_last(uri)
44
- uri = truncate_last_path_segment(uri, truncation_length)
44
+ return uri.special_format.truncate!(truncation_length)
45
45
  end
46
46
  end
47
47
 
@@ -49,22 +49,6 @@ module Truncator
49
49
  end
50
50
 
51
51
  private
52
- def truncate_all_paths_except_last(uri)
53
- uri = uri.dup
54
- paths = uri.paths
55
- if paths.size > 1
56
- uri.paths = [SEPARATOR, paths.last]
57
- end
58
- uri
59
- end
60
-
61
- def truncate_last_path_segment(uri, truncation_length)
62
- uri = uri.dup
63
- last_path_with_query = uri.last_path_with_query
64
- uri.last_path_with_query = last_path_with_query.truncate(truncation_length)
65
- uri
66
- end
67
-
68
52
  def sort_paths_by_length_and_index!(paths)
69
53
  paths.lazy.with_index.sort_by { |a, i| [a.size, i] }.map(&:first)
70
54
  end
@@ -83,13 +67,18 @@ module Truncator
83
67
  end
84
68
 
85
69
  # Truncate the uri via truncating the shortest possible path sequence
70
+ # return nil if can't truncate
86
71
  def truncate_by_shortest(uri, target_length)
87
72
  uri = uri.dup
88
73
  sorted_sequences = sort_paths_by_length_and_index!(paths_sequences_from_uri(uri))
89
74
  truncated_part = find_truncated_sequence(uri, sorted_sequences, target_length)
90
75
 
91
- uri.path = uri.path.sub(truncated_part, SEPARATOR)
92
- uri
76
+ if truncated_part
77
+ uri.path = uri.path.sub(truncated_part, SEPARATOR)
78
+ uri
79
+ else
80
+ nil
81
+ end
93
82
  end
94
83
  end
95
84
  end
@@ -1,3 +1,3 @@
1
1
  module Truncator
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -73,12 +73,26 @@ describe Truncator::UrlParser do
73
73
  context "when the URL contains a really long host name and a long trailing filename" do
74
74
  let(:shortened) { Truncator::UrlParser.shorten_url("http://www128376218.skjdhfskdjfhs.lqdkwjqlkwjdqlqwkjd.com/some/path/1234567890123456789012345678901234test_of_the_mergency_broadcastingnet_work.html", 30) }
75
75
 
76
- it "should not truncate the host name and truncated the last part of the path to truncation length and all dirs" do
77
- shortened.should == "www128376218.skjdhfskdjfhs.lqdkwjqlkwjdqlqwkjd.com/.../123456789012345678901234567..."
76
+ it "should truncate to 30 chars with ellipses" do
77
+ shortened.should == "www128376218.skjdhfskdjfhs...."
78
+ end
79
+ end
80
+
81
+ context 'when turning subfolder(s) into ellipses does not truncate enough' do
82
+ let(:hypenated_url) { 'http://www.whitehouse.gov/contact/submit-questions-and-comments' }
83
+ it 'should truncate to 30 chars with ellipses' do
84
+ Truncator::UrlParser.shorten_url(hypenated_url, 30).should == 'www.whitehouse.gov/contact/...'
85
+ end
86
+ end
87
+
88
+ context "when the URL contains a short host name, short folder names, and a longer trailing filename" do
89
+ let(:shortened) { Truncator::UrlParser.shorten_url("http://www.ddd.com/some/path/123456789.html", 30) }
90
+
91
+ it "should truncate to 30 chars with ellipses" do
92
+ shortened.should == "www.ddd.com/.../123456789.html"
78
93
  end
79
94
  end
80
95
 
81
- #TODO: this and the next spec can be merged
82
96
  context "when the URL contains a really long host name and is an http url and has an empty path" do
83
97
  let(:shortened) { Truncator::UrlParser.shorten_url("http://www128376218.skjdhfskdj.lqdkwjqlkwjdqlqwkjd.com/", 30) }
84
98
 
@@ -98,8 +112,8 @@ describe Truncator::UrlParser do
98
112
  context "when the URL contains a really long host name and has a really long query parameter" do
99
113
  let(:shortened) { Truncator::UrlParser.shorten_url("http://www128376218.skjdhfskdjfhs.lqdkwjqlkwjdqlqwkjd.com/?cmd=1234567890123456789012345678901234&api_key=1234567890123456789012345678901234", 30) }
100
114
 
101
- it "should not truncate the host name but truncate the query parameter" do
102
- shortened.should == "www128376218.skjdhfskdjfhs.lqdkwjqlkwjdqlqwkjd.com/?cmd=1234567890123456789012..."
115
+ it "should truncate to 30 chars with ellipses" do
116
+ shortened.should == "www128376218.skjdhfskdjfhs...."
103
117
  end
104
118
  end
105
119
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: truncator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - freemanoid
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-05 00:00:00.000000000 Z
11
+ date: 2013-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport