string_awesome 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e7a2d49fdaa2999e14bbd47104154df9831f39f
4
- data.tar.gz: 77f20be63fb8cde1a961ccd6678b0961f6a507f8
3
+ metadata.gz: f78f62624cff19d1223034e0de8c83df051ba22e
4
+ data.tar.gz: 6b0f254d8cd5c41a76bda6db88d2c0a0e649c052
5
5
  SHA512:
6
- metadata.gz: 1dec6e517d7750c18e9a95b955393a7f03d83743a939c06d1dec88d4f2a43824f1fed7e5f194d37ec804c6d93f69bd516ea0d3f723e272780fc845dde44e8f1a
7
- data.tar.gz: c43e8eae6e433c0df800c0a62ddd214a1ba976eebc2e46316978fbbba90081df0a20842c061f78d7dfbf6c390c6db7c55ba11dad84a9f21010004dbdf96c28dd
6
+ metadata.gz: 772e18e331e7e45e6b8e394d6ad9263d1316239a58ce7dcbbfc852254260ce1c0b12f64d5e9f95be73221165437457b873dc936905ff222a10283945673e90c8
7
+ data.tar.gz: b4fb338d8557c6ae399b5e2e49cf7b22d350bb31415a841cc4f482b9ca02b184037cde817a26b3d828847ad6c3c1fa868dd171b7998b4ffd6b9558d03d9ffce6
data/README.md CHANGED
@@ -114,12 +114,12 @@ Removes accents from words in the text.
114
114
  #=> 'lorem ipsum dolor sit amet!'
115
115
  ```
116
116
 
117
- #### String#slug
117
+ #### String#slugfy
118
118
 
119
119
  Parses the text to a valid format for URLs.
120
120
 
121
121
  ``` ruby
122
- 'Lórem IPSUM Dolor?'.slug
122
+ 'Lórem IPSUM Dolor?'.slugfy
123
123
  #=> 'lorem-ipsum-dolor'
124
124
  ```
125
125
 
data/Rakefile CHANGED
@@ -1,4 +1,4 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
  require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
@@ -46,7 +46,7 @@ module StringAwesome
46
46
  # => 'lorem ipsum dolor sit amet!'
47
47
 
48
48
  def no_accents
49
- self.mb_chars.normalize(:kd).gsub(SA_ACCENT_REGEX, '').to_s
49
+ self.mb_chars.normalize(:kd).gsub(SA_REGEXES[:accent], '').to_s
50
50
  end
51
51
 
52
52
  # Parses the text to a valid format for URLs.
@@ -59,11 +59,17 @@ module StringAwesome
59
59
  # downcase: (Boolean)
60
60
  # - If true, it will force the String to be in downcase.
61
61
 
62
- def slug(downcase = true)
62
+ def slugfy(downcase = true)
63
63
  str = self.no_accents.words.join '-'
64
64
  downcase ? str.downcase : str
65
65
  end
66
66
 
67
+ # DEPRECATED: Use 'slugfy' instead.
68
+ def slug(downcase = true)
69
+ warn '[DEPRECATION] `String#slug` is deprecated. Please use `String#slugfy` instead.'
70
+ slugfy downcase
71
+ end
72
+
67
73
  # Returns an array with all the words from the string.
68
74
  #
69
75
  # Example:
@@ -208,7 +214,7 @@ module StringAwesome
208
214
  # - :target - Value for "target" attribute: <a href="url" target="_blank">url</a>
209
215
 
210
216
  def linkify(options = {})
211
- self.gsub!(SA_URL_REGEX) do |match|
217
+ self.gsub!(SA_REGEXES[:url]) do |match|
212
218
  # http://verylongurl...
213
219
  displayed = _truncate_url match, options[:truncate]
214
220
 
@@ -216,7 +222,7 @@ module StringAwesome
216
222
  options.delete(:truncate) unless !options
217
223
 
218
224
  # Forces the presence of the 'http://'
219
- match = "http://#{match}" unless match =~ SA_PROTOCOL_REGEX
225
+ match = "http://#{match}" unless match =~ SA_REGEXES[:protocol]
220
226
 
221
227
  "<a href=\"#{match}\"#{_apply_tag_attrs(options)}>#{displayed}</a>"
222
228
  end
@@ -267,7 +273,7 @@ module StringAwesome
267
273
  str = options[:only] ? self : self.linkify(options[:url] || {})
268
274
 
269
275
  # Iterates with the matched expressions
270
- str.gsub!(SA_TWEET_REGEX) do |match|
276
+ str.gsub!(SA_REGEXES[:tweet]) do |match|
271
277
  is_hashtag = match =~ /#/
272
278
 
273
279
  unless _restricted?(is_hashtag, options[:only])
@@ -1,8 +1,10 @@
1
1
  module StringAwesome
2
2
  module AwesomeRegexes
3
- SA_ACCENT_REGEX = /[^\x00-\x7F]/n
4
- SA_URL_REGEX = /\b((((ht|f)tp[s]?:\/\/)|([a-z0-9]+\.))+(?<!@)([a-z0-9\_\-]+)(\.[a-z]+)+([\?\/\:][a-z0-9_=%&@\?\.\/\-\:\#\(\)]+)?\/?)/i
5
- SA_PROTOCOL_REGEX = /(ht|f)tp[s]?/i
6
- SA_TWEET_REGEX = /(((^[@#])|([^a-z0-9\W]|\s)([@|#]))([a-z0-9\_]+))/i
3
+ SA_REGEXES = {
4
+ accent: /[^\x00-\x7F]/n,
5
+ url: /\b((((ht|f)tp[s]?:\/\/)|([a-z0-9]+\.))+(?<!@)([a-z0-9\_\-]+)(\.[a-z]+)+([\?\/\:][a-z0-9_=%&@\?\.\/\-\:\#\(\)]+)?\/?)/i,
6
+ protocol: /(ht|f)tp[s]?/i,
7
+ tweet: /(((^[@#])|([^a-z0-9\W]|\s)([@|#]))([a-z0-9\_]+))/i
8
+ }
7
9
  end
8
10
  end
@@ -1,3 +1,3 @@
1
1
  module StringAwesome
2
- VERSION = '0.2.3'
2
+ VERSION = '0.2.4'
3
3
  end
@@ -2,349 +2,365 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- #
6
- # String#nl2br
7
- #
8
- describe 'String#nl2br' do
9
- it 'should replace "\n" to "<br />"' do
10
- "break\nrow".nl2br.should eq 'break<br />row'
11
- end
12
-
13
- it 'should not replace "\n" to "<br />" when "\n" is literal' do
14
- "\\no break row".nl2br.should_not eq '<br />o break row'
15
- end
16
- end
17
-
18
- #
19
- # String#to_title
20
- #
21
- describe 'String#to_title' do
22
- it 'should format the text properly to looks like a title' do
23
- "here's aN AWEsome TiTle!".to_title.should eq "Here's An Awesome Title!"
24
- end
25
- end
26
-
27
- #
28
- # String#strip_tags
29
- #
30
- describe 'String#strip_tags' do
31
- it 'should remove any HTML tag from a given text' do
32
- '<h1><a href="http://somecoolurl.com">Aloha!</a></h1>'.strip_tags.should eq 'Aloha!'
33
- end
34
-
35
- it 'should remove the whitespaces that were allocated in place of HTML tags' do
36
- '<h1>lorem ipsum </h1> dolor'.strip_tags.should eq 'lorem ipsum dolor'
37
- end
38
- end
39
-
40
- #
41
- # String#no_accents
42
- #
43
- describe 'String#no_accents' do
44
- it 'should remove accents from words in the text' do
45
- 'lórem ipsùm dólor sìt ãmet!'.no_accents.should eq 'lorem ipsum dolor sit amet!'
46
- end
47
- end
48
-
49
- #
50
- # String#slug
51
- #
52
- describe 'String#slug' do
53
- it 'should parse the text to an URL valid format, downcase by default' do
54
- 'Lorem IPSUM Dolor?'.slug.should eq 'lorem-ipsum-dolor'
55
- end
56
-
57
- it 'should parse the text to an URL valid format without forcing it to downcase' do
58
- 'Lorem Ipsum Dolor 2013!'.slug(false).should eq 'Lorem-Ipsum-Dolor-2013'
59
- end
60
- end
61
-
62
- #
63
- # String#truncate
64
- #
65
- describe 'String#truncate' do
66
- it 'shoud truncate the text' do
67
- "It's a very loooooong text!".truncate(11).should eq "It's a very"
68
- end
69
-
70
- it 'shoud truncate the text after a word' do
71
- "It's a very loooooong text!".truncate(8, true).should eq "It's a"
72
- end
73
- end
74
-
75
- #
76
- # String#ellipsis
77
- #
78
- describe 'String#ellipsis' do
79
- it "shoud not append ellipsis when String's length is less then 2" do
80
- 'l'.ellipsis.should eq 'l'
81
- end
82
-
83
- it "shoud not have the max_length value greater than text's length" do
84
- 'foo'.ellipsis(10).should eq 'foo'
85
- end
86
-
87
- it "should append ellipsis in the text's half length (default behaviour)" do
88
- 'lorem ipsum!'.ellipsis.should eq 'lorem...'
89
- end
90
-
91
- it "should append ellipsis in the text's rounded half length when the number of characters is odd (default behaviour)" do
92
- 'lorem ipsum'.ellipsis.should eq 'lorem...'
93
- end
94
-
95
- it 'should append the HTML encoded ellipsis in the text' do
96
- 'lorem ipsum'.ellipsis(5, html_encoded: true).should eq 'lorem&hellip;'
97
- end
98
-
99
- it "should not append the HTML encoded ellipsis in the text when it's not required" do
100
- 'lorem ipsum'.ellipsis(5, html_encoded: false).should_not eq 'lorem&hellip;'
101
- end
102
-
103
- it 'should append ellipsis after a word' do
104
- 'lorem ipsum dolor'.ellipsis(14, after_a_word: true).should eq 'lorem ipsum...'
105
- end
106
-
107
- it 'should append the HTML encoded ellipsis after a word' do
108
- 'lorem ipsum dolor'.ellipsis(13, html_encoded: true, after_a_word: true).should eq 'lorem ipsum&hellip;'
109
- end
110
- end
111
-
112
- #
113
- # String#words
114
- #
115
- describe 'String#words' do
116
- it 'should return an array with all the words from the string' do
117
- 'Lorem! Ipsum dolor, sit amet 2013.'.words.should eq ['Lorem', 'Ipsum', 'dolor', 'sit', 'amet', '2013']
118
- end
119
- end
120
-
121
- #
122
- # String#reverse_words
123
- #
124
- describe 'String#reverse_words' do
125
- it 'should reverse a string by words' do
126
- 'lorem ipsum dolor'.reverse_words.should eq 'dolor ipsum lorem'
127
- end
128
- end
129
-
130
- #
131
- # String#count_words
132
- #
133
- describe 'String#count_words' do
134
- it 'should count how many words there are in the string' do
135
- 'lorem ipsum dolor'.count_words.should eq 3
136
- end
137
-
138
- it 'should count how many words there are in the string limited by the max_length value (whitespace test)' do
139
- 'lorem ipsum dolor'.count_words(6).should eq 1
140
- end
141
-
142
- it 'should count how many words there are in the string limited by the max_length value (splitted word, test 1)' do
143
- 'lorem ipsum dolor'.count_words(7).should eq 1
144
- end
145
-
146
- it 'should count how many words there are in the string limited by the max_length value (splitted word, test 2)' do
147
- 'lorem ipsum dolor'.count_words(8).should eq 1
148
- end
149
-
150
- it 'should not condiser a non-word character (!) as a word' do
151
- 'lorem ipsum ! dolor'.count_words.should eq 3
152
- end
153
-
154
- it 'should not condiser a non-word character (!) as a word' do
155
- 'lorem ipsum ! dolor'.count_words(13).should eq 2
156
- end
157
- end
158
-
159
- #
160
- # String#first_words
161
- #
162
- describe 'String#first_words' do
163
- it 'should return all the words when the amount is not passed' do
164
- 'lorem ipsum dolor'.first_words.should eq ['lorem', 'ipsum', 'dolor']
165
- end
166
-
167
- it 'should return only the 2 first words (Array)' do
168
- 'lorem. ! ipsum dolor'.first_words(2).should eq ['lorem', 'ipsum']
169
- end
170
- end
171
-
172
- #
173
- # String#last_words
174
- #
175
- describe 'String#last_words' do
176
- it 'should return all the words when the amount is not passed' do
177
- 'lorem ipsum dolor'.last_words.should eq ['lorem', 'ipsum', 'dolor']
178
- end
179
-
180
- it 'should return only the 2 last words (Array)' do
181
- 'lorem. ! ipsum dolor'.first_words(2).should eq ['lorem', 'ipsum']
182
- end
183
- end
184
-
185
- #
186
- # String#linkify
187
- #
188
- describe 'String#linkify' do
189
- it 'should find all the HTTP URLs in text and wrap in anchor tags' do
190
- str = 'Awesome site: http://foobar.com'
191
- this = 'Awesome site: <a href="http://foobar.com">http://foobar.com</a>'
192
- str.linkify.should eq this
193
- end
194
-
195
- it 'should find all the HTTPS URLs in text and wrap in anchor tags' do
196
- str = 'Awesome site: https://foobar.com'
197
- this = 'Awesome site: <a href="https://foobar.com">https://foobar.com</a>'
198
- str.linkify.should eq this
199
- end
200
-
201
- it 'should find all the FTP URLs in text and wrap in anchor tags' do
202
- str = 'Awesome site: ftp://foobar.com'
203
- this = 'Awesome site: <a href="ftp://foobar.com">ftp://foobar.com</a>'
204
- str.linkify.should eq this
205
- end
206
-
207
- it 'should match http://sub.sub.domain' do
208
- url = 'http://www3.blog.foo.bar'
209
- str = "Awesome site: #{url}"
210
- this = "Awesome site: <a href=\"#{url}\">#{url}</a>"
211
- str.linkify.should eq this
212
- end
213
-
214
- it 'should match query strings' do
215
- url = 'http://www.foobar.com/blog?category_id=54322&itle=lorem-ipsum-dolor'
216
- str = "Awesome site: #{url}"
217
- this = "Awesome site: <a href=\"#{url}\">#{url}</a>"
218
- str.linkify.should eq this
219
- end
220
-
221
- it 'should match friendly URLs' do
222
- url = 'http://www.foobar.com/blog/tech/lorem-ipsum-dolor'
223
- str = "Awesome site: #{url}"
224
- this = "Awesome site: <a href=\"#{url}\">#{url}</a>"
225
- str.linkify.should eq this
226
- end
227
-
228
- it 'can set the "class" HTML attribute to be applied on the anchor tag' do
229
- str = 'Awesome site: http://foobar.com'
230
- this = 'Awesome site: <a href="http://foobar.com" class="link">http://foobar.com</a>'
231
- str.linkify(class: 'link').should eq this
232
- end
233
-
234
- it 'can set the "target" HTML attribute to be applied on the anchor tag' do
235
- str = 'Awesome site: http://foobar.com'
236
- this = 'Awesome site: <a href="http://foobar.com" target="_blank">http://foobar.com</a>'
237
- str.linkify(target: '_blank').should eq this
238
- end
239
-
240
- it 'can set the class and target HTML attributes to be applied on the anchor tag' do
241
- str = 'Awesome site: http://foobar.com'
242
- this = 'Awesome site: <a href="http://foobar.com" class="link" target="_blank">http://foobar.com</a>'
243
- str.linkify(class: 'link', target: '_blank').should eq this
244
- end
245
-
246
- it 'can truncate the URL displayed whithin the anchor tag (Interger param)' do
247
- str = 'Awesome site: http://foobar.com'
248
- this = 'Awesome site: <a href="http://foobar.com">http://foo...</a>'
249
- str.linkify(truncate: 10).should eq this
250
- end
251
-
252
- it 'can truncate the URL displayed whithin the anchor tag (Hash param)' do
253
- str = 'Awesome site: http://foobar.com'
254
- this = 'Awesome site: <a href="http://foobar.com">http://foo&hellip;</a>'
255
- str.linkify(truncate: { length: 10, html_encoded: true }).should eq this
256
- end
257
-
258
- it 'can set HTML attributes and truncate the URL' do
259
- str = 'Awesome site: http://foobar.com'
260
- this = 'Awesome site: <a href="http://foobar.com" class="link">http://foo...</a>'
261
- str.linkify(class: 'link', truncate: 10).should eq this
262
- end
263
-
264
- it "matches URLs without the presence of 'http://' but presenting 'www'" do
265
- 'www.foobar.com'.linkify.should eq '<a href="http://www.foobar.com">www.foobar.com</a>'
266
- end
267
-
268
- it "should not match URLs without the presence of 'http://' and 'www'" do
269
- 'foobar.com'.linkify.should eq 'foobar.com'
270
- end
271
- end
272
-
273
- #
274
- # String#tweetify
275
- #
276
- describe 'String#tweetify' do
277
- it 'should find Twitter handles (@username) and wrap them in anchor tags' do
278
- str = 'What about to follow @tiagopog?'
279
- this = 'What about to follow <a href="https://twitter.com/tiagopog">@tiagopog</a>?'
280
- str.tweetify.should eq this
281
- end
282
-
283
- it 'should find Twitter handles (@username) and wrap them in anchor tags (attributes included)' do
284
- str = 'What about to follow @tiagopog?'
285
- this = 'What about to follow <a href="https://twitter.com/tiagopog" target="_blank" class="tt-handle">@tiagopog</a>?'
286
- str.tweetify(tt_handle: { target: '_blank', class: 'tt-handle' }).should eq this
287
- end
288
-
289
- it 'should not match foo@bar as being a Twitter handle' do
290
- 'foo@bar'.tweetify.should eq 'foo@bar'
291
- end
292
-
293
- it 'should find hashtags (#hashtag) and wrap them in anchor tags' do
294
- str = "Let's code! #rubyrocks"
295
- this = "Let's code! <a href=\"https://twitter.com/search?q=%23rubyrocks\">#rubyrocks</a>"
296
- str.tweetify.should eq this
297
- end
298
-
299
- it 'should find hashtags (#hashtag) and wrap them in anchor tags (attributes included)' do
300
- str = "Let's code! #rubyrocks"
301
- this = "Let's code! <a href=\"https://twitter.com/search?q=%23rubyrocks\" target=\"_blank\" class=\"hashtag\">#rubyrocks</a>"
302
- str.tweetify(hashtag: { target: '_blank', class: 'hashtag' }).should eq this
303
- end
304
-
305
- it 'should not match foo#bar as being a hashtag' do
306
- 'foo#bar'.tweetify.should eq 'foo#bar'
307
- end
308
-
309
- it 'should find URLs and wrap them in anchor tags' do
310
- str = 'Tweet some cool link: http://foobar.com'
311
- this = 'Tweet some cool link: <a href="http://foobar.com">http://foobar.com</a>'
312
- str.tweetify.should eq this
313
- end
314
-
315
- it 'should find URLs and wrap them in anchor tags (attributes included)' do
316
- str = 'Tweet some cool link: http://foobar.com'
317
- this = 'Tweet some cool link: <a href="http://foobar.com" class="link">http://foobar.com</a>'
318
- str.tweetify(url: { class: 'link' }).should eq this
319
- end
320
-
321
- it 'should find links, Twitter handles, hashtags and wrap them in anchor tags' do
322
- str = 'Cool link from @tiagopog! http://foobar.com #rubyrocks'
323
- this = 'Cool link from <a href="https://twitter.com/tiagopog">@tiagopog</a>! <a href="http://foobar.com">http://foobar.com</a> <a href="https://twitter.com/search?q=%23rubyrocks">#rubyrocks</a>'
324
- str.tweetify.should eq this
325
- end
326
-
327
- it 'should find only Twitter handles' do
328
- str = 'Cool link from @tiagopog! http://foobar.com #rubyrocks'
329
- this = 'Cool link from <a href="https://twitter.com/tiagopog">@tiagopog</a>! http://foobar.com #rubyrocks'
330
- str.tweetify(only: [:tt_handle]).should eq this
331
- end
332
-
333
- it 'should find only hashtags' do
334
- str = 'Cool link from @tiagopog! http://foobar.com #rubyrocks'
335
- this = 'Cool link from @tiagopog! http://foobar.com <a href="https://twitter.com/search?q=%23rubyrocks">#rubyrocks</a>'
336
- str.tweetify(only: [:hashtag]).should eq this
337
- end
338
-
339
- it 'should not find URLs, just hashtags and Twitter handles' do
340
- str = 'Cool link from @tiagopog! http://foobar.com #rubyrocks'
341
- this = 'Cool link from <a href="https://twitter.com/tiagopog">@tiagopog</a>! http://foobar.com <a href="https://twitter.com/search?q=%23rubyrocks">#rubyrocks</a>'
342
- str.tweetify(only: [:hashtag, :tt_handle]).should eq this
343
- end
344
-
345
- it 'should not find URLs, just hashtags and Twitter handles' do
346
- str = 'Cool link from @tiagopog! http://foobar.com #rubyrocks'
347
- this = 'Cool link from <a href="https://twitter.com/tiagopog" target="_blank" class="tt-handle">@tiagopog</a>! http://foobar.com <a href="https://twitter.com/search?q=%23rubyrocks" target="_blank" class="hashtag">#rubyrocks</a>'
348
- str.tweetify(only: [:hashtag, :tt_handle], tt_handle: { target: '_blank', class: 'tt-handle' }, hashtag: { target: '_blank', class: 'hashtag' }).should eq this
5
+ describe String do
6
+ #
7
+ # String#nl2br
8
+ #
9
+
10
+ describe '#nl2br' do
11
+ it 'should replace "\n" to "<br />"' do
12
+ "break\nrow".nl2br.should eq 'break<br />row'
13
+ end
14
+
15
+ it 'should not replace "\n" to "<br />" when "\n" is literal' do
16
+ "\\no break row".nl2br.should_not eq '<br />o break row'
17
+ end
18
+ end
19
+
20
+ #
21
+ # String#to_title
22
+ #
23
+
24
+ describe '#to_title' do
25
+ it 'should format the text properly to looks like a title' do
26
+ "here's aN AWEsome TiTle!".to_title.should eq "Here's An Awesome Title!"
27
+ end
28
+ end
29
+
30
+ #
31
+ # String#strip_tags
32
+ #
33
+
34
+ describe '#strip_tags' do
35
+ it 'should remove any HTML tag from a given text' do
36
+ '<h1><a href="http://somecoolurl.com">Aloha!</a></h1>'.strip_tags.should eq 'Aloha!'
37
+ end
38
+
39
+ it 'should remove the whitespaces that were allocated in place of HTML tags' do
40
+ '<h1>lorem ipsum </h1> dolor'.strip_tags.should eq 'lorem ipsum dolor'
41
+ end
42
+ end
43
+
44
+ #
45
+ # String#no_accents
46
+ #
47
+
48
+ describe '#no_accents' do
49
+ it 'should remove accents from words in the text' do
50
+ 'lórem ipsùm dólor sìt ãmet!'.no_accents.should eq 'lorem ipsum dolor sit amet!'
51
+ end
52
+ end
53
+
54
+ #
55
+ # String#slugfy
56
+ #
57
+
58
+ describe '#slug' do
59
+ it 'should parse the text to an URL valid format, downcase by default' do
60
+ 'Lorem IPSUM Dolor?'.slugfy.should eq 'lorem-ipsum-dolor'
61
+ end
62
+
63
+ it 'should parse the text to an URL valid format without forcing it to downcase' do
64
+ 'Lorem Ipsum Dolor 2013!'.slugfy(false).should eq 'Lorem-Ipsum-Dolor-2013'
65
+ end
66
+ end
67
+
68
+ #
69
+ # String#truncate
70
+ #
71
+
72
+ describe '#truncate' do
73
+ it 'shoud truncate the text' do
74
+ "It's a very loooooong text!".truncate(11).should eq "It's a very"
75
+ end
76
+
77
+ it 'shoud truncate the text after a word' do
78
+ "It's a very loooooong text!".truncate(8, true).should eq "It's a"
79
+ end
80
+ end
81
+
82
+ #
83
+ # String#ellipsis
84
+ #
85
+
86
+ describe '#ellipsis' do
87
+ it "shoud not append ellipsis when String's length is less then 2" do
88
+ 'l'.ellipsis.should eq 'l'
89
+ end
90
+
91
+ it "shoud not have the max_length value greater than text's length" do
92
+ 'foo'.ellipsis(10).should eq 'foo'
93
+ end
94
+
95
+ it "should append ellipsis in the text's half length (default behaviour)" do
96
+ 'lorem ipsum!'.ellipsis.should eq 'lorem...'
97
+ end
98
+
99
+ it "should append ellipsis in the text's rounded half length when the number of characters is odd (default behaviour)" do
100
+ 'lorem ipsum'.ellipsis.should eq 'lorem...'
101
+ end
102
+
103
+ it 'should append the HTML encoded ellipsis in the text' do
104
+ 'lorem ipsum'.ellipsis(5, html_encoded: true).should eq 'lorem&hellip;'
105
+ end
106
+
107
+ it "should not append the HTML encoded ellipsis in the text when it's not required" do
108
+ 'lorem ipsum'.ellipsis(5, html_encoded: false).should_not eq 'lorem&hellip;'
109
+ end
110
+
111
+ it 'should append ellipsis after a word' do
112
+ 'lorem ipsum dolor'.ellipsis(14, after_a_word: true).should eq 'lorem ipsum...'
113
+ end
114
+
115
+ it 'should append the HTML encoded ellipsis after a word' do
116
+ 'lorem ipsum dolor'.ellipsis(13, html_encoded: true, after_a_word: true).should eq 'lorem ipsum&hellip;'
117
+ end
118
+ end
119
+
120
+ #
121
+ # String#words
122
+ #
123
+
124
+ describe '#words' do
125
+ it 'should return an array with all the words from the string' do
126
+ 'Lorem! Ipsum dolor, sit amet 2013.'.words.should eq ['Lorem', 'Ipsum', 'dolor', 'sit', 'amet', '2013']
127
+ end
128
+ end
129
+
130
+ #
131
+ # String#reverse_words
132
+ #
133
+
134
+ describe '#reverse_words' do
135
+ it 'should reverse a string by words' do
136
+ 'lorem ipsum dolor'.reverse_words.should eq 'dolor ipsum lorem'
137
+ end
138
+ end
139
+
140
+ #
141
+ # String#count_words
142
+ #
143
+
144
+ describe '#count_words' do
145
+ it 'should count how many words there are in the string' do
146
+ 'lorem ipsum dolor'.count_words.should eq 3
147
+ end
148
+
149
+ it 'should count how many words there are in the string limited by the max_length value (whitespace test)' do
150
+ 'lorem ipsum dolor'.count_words(6).should eq 1
151
+ end
152
+
153
+ it 'should count how many words there are in the string limited by the max_length value (splitted word, test 1)' do
154
+ 'lorem ipsum dolor'.count_words(7).should eq 1
155
+ end
156
+
157
+ it 'should count how many words there are in the string limited by the max_length value (splitted word, test 2)' do
158
+ 'lorem ipsum dolor'.count_words(8).should eq 1
159
+ end
160
+
161
+ it 'should not condiser a non-word character (!) as a word' do
162
+ 'lorem ipsum ! dolor'.count_words.should eq 3
163
+ end
164
+
165
+ it 'should not condiser a non-word character (!) as a word' do
166
+ 'lorem ipsum ! dolor'.count_words(13).should eq 2
167
+ end
168
+ end
169
+
170
+ #
171
+ # String#first_words
172
+ #
173
+
174
+ describe '#first_words' do
175
+ it 'should return all the words when the amount is not passed' do
176
+ 'lorem ipsum dolor'.first_words.should eq ['lorem', 'ipsum', 'dolor']
177
+ end
178
+
179
+ it 'should return only the 2 first words (Array)' do
180
+ 'lorem. ! ipsum dolor'.first_words(2).should eq ['lorem', 'ipsum']
181
+ end
182
+ end
183
+
184
+ #
185
+ # String#last_words
186
+ #
187
+
188
+ describe '#last_words' do
189
+ it 'should return all the words when the amount is not passed' do
190
+ 'lorem ipsum dolor'.last_words.should eq ['lorem', 'ipsum', 'dolor']
191
+ end
192
+
193
+ it 'should return only the 2 last words (Array)' do
194
+ 'lorem. ! ipsum dolor'.first_words(2).should eq ['lorem', 'ipsum']
195
+ end
196
+ end
197
+
198
+ #
199
+ # String#linkify
200
+ #
201
+
202
+ describe '#linkify' do
203
+ it 'should find all the HTTP URLs in text and wrap in anchor tags' do
204
+ str = 'Awesome site: http://foobar.com'
205
+ this = 'Awesome site: <a href="http://foobar.com">http://foobar.com</a>'
206
+ str.linkify.should eq this
207
+ end
208
+
209
+ it 'should find all the HTTPS URLs in text and wrap in anchor tags' do
210
+ str = 'Awesome site: https://foobar.com'
211
+ this = 'Awesome site: <a href="https://foobar.com">https://foobar.com</a>'
212
+ str.linkify.should eq this
213
+ end
214
+
215
+ it 'should find all the FTP URLs in text and wrap in anchor tags' do
216
+ str = 'Awesome site: ftp://foobar.com'
217
+ this = 'Awesome site: <a href="ftp://foobar.com">ftp://foobar.com</a>'
218
+ str.linkify.should eq this
219
+ end
220
+
221
+ it 'should match http://sub.sub.domain' do
222
+ url = 'http://www3.blog.foo.bar'
223
+ str = "Awesome site: #{url}"
224
+ this = "Awesome site: <a href=\"#{url}\">#{url}</a>"
225
+ str.linkify.should eq this
226
+ end
227
+
228
+ it 'should match query strings' do
229
+ url = 'http://www.foobar.com/blog?category_id=54322&itle=lorem-ipsum-dolor'
230
+ str = "Awesome site: #{url}"
231
+ this = "Awesome site: <a href=\"#{url}\">#{url}</a>"
232
+ str.linkify.should eq this
233
+ end
234
+
235
+ it 'should match friendly URLs' do
236
+ url = 'http://www.foobar.com/blog/tech/lorem-ipsum-dolor'
237
+ str = "Awesome site: #{url}"
238
+ this = "Awesome site: <a href=\"#{url}\">#{url}</a>"
239
+ str.linkify.should eq this
240
+ end
241
+
242
+ it 'can set the "class" HTML attribute to be applied on the anchor tag' do
243
+ str = 'Awesome site: http://foobar.com'
244
+ this = 'Awesome site: <a href="http://foobar.com" class="link">http://foobar.com</a>'
245
+ str.linkify(class: 'link').should eq this
246
+ end
247
+
248
+ it 'can set the "target" HTML attribute to be applied on the anchor tag' do
249
+ str = 'Awesome site: http://foobar.com'
250
+ this = 'Awesome site: <a href="http://foobar.com" target="_blank">http://foobar.com</a>'
251
+ str.linkify(target: '_blank').should eq this
252
+ end
253
+
254
+ it 'can set the class and target HTML attributes to be applied on the anchor tag' do
255
+ str = 'Awesome site: http://foobar.com'
256
+ this = 'Awesome site: <a href="http://foobar.com" class="link" target="_blank">http://foobar.com</a>'
257
+ str.linkify(class: 'link', target: '_blank').should eq this
258
+ end
259
+
260
+ it 'can truncate the URL displayed whithin the anchor tag (Interger param)' do
261
+ str = 'Awesome site: http://foobar.com'
262
+ this = 'Awesome site: <a href="http://foobar.com">http://foo...</a>'
263
+ str.linkify(truncate: 10).should eq this
264
+ end
265
+
266
+ it 'can truncate the URL displayed whithin the anchor tag (Hash param)' do
267
+ str = 'Awesome site: http://foobar.com'
268
+ this = 'Awesome site: <a href="http://foobar.com">http://foo&hellip;</a>'
269
+ str.linkify(truncate: { length: 10, html_encoded: true }).should eq this
270
+ end
271
+
272
+ it 'can set HTML attributes and truncate the URL' do
273
+ str = 'Awesome site: http://foobar.com'
274
+ this = 'Awesome site: <a href="http://foobar.com" class="link">http://foo...</a>'
275
+ str.linkify(class: 'link', truncate: 10).should eq this
276
+ end
277
+
278
+ it "matches URLs without the presence of 'http://' but presenting 'www'" do
279
+ 'www.foobar.com'.linkify.should eq '<a href="http://www.foobar.com">www.foobar.com</a>'
280
+ end
281
+
282
+ it "should not match URLs without the presence of 'http://' and 'www'" do
283
+ 'foobar.com'.linkify.should eq 'foobar.com'
284
+ end
285
+ end
286
+
287
+ #
288
+ # String#tweetify
289
+ #
290
+
291
+ describe '#tweetify' do
292
+ it 'should find Twitter handles (@username) and wrap them in anchor tags' do
293
+ str = 'What about to follow @tiagopog?'
294
+ this = 'What about to follow <a href="https://twitter.com/tiagopog">@tiagopog</a>?'
295
+ str.tweetify.should eq this
296
+ end
297
+
298
+ it 'should find Twitter handles (@username) and wrap them in anchor tags (attributes included)' do
299
+ str = 'What about to follow @tiagopog?'
300
+ this = 'What about to follow <a href="https://twitter.com/tiagopog" target="_blank" class="tt-handle">@tiagopog</a>?'
301
+ str.tweetify(tt_handle: { target: '_blank', class: 'tt-handle' }).should eq this
302
+ end
303
+
304
+ it 'should not match foo@bar as being a Twitter handle' do
305
+ 'foo@bar'.tweetify.should eq 'foo@bar'
306
+ end
307
+
308
+ it 'should find hashtags (#hashtag) and wrap them in anchor tags' do
309
+ str = "Let's code! #rubyrocks"
310
+ this = "Let's code! <a href=\"https://twitter.com/search?q=%23rubyrocks\">#rubyrocks</a>"
311
+ str.tweetify.should eq this
312
+ end
313
+
314
+ it 'should find hashtags (#hashtag) and wrap them in anchor tags (attributes included)' do
315
+ str = "Let's code! #rubyrocks"
316
+ this = "Let's code! <a href=\"https://twitter.com/search?q=%23rubyrocks\" target=\"_blank\" class=\"hashtag\">#rubyrocks</a>"
317
+ str.tweetify(hashtag: { target: '_blank', class: 'hashtag' }).should eq this
318
+ end
319
+
320
+ it 'should not match foo#bar as being a hashtag' do
321
+ 'foo#bar'.tweetify.should eq 'foo#bar'
322
+ end
323
+
324
+ it 'should find URLs and wrap them in anchor tags' do
325
+ str = 'Tweet some cool link: http://foobar.com'
326
+ this = 'Tweet some cool link: <a href="http://foobar.com">http://foobar.com</a>'
327
+ str.tweetify.should eq this
328
+ end
329
+
330
+ it 'should find URLs and wrap them in anchor tags (attributes included)' do
331
+ str = 'Tweet some cool link: http://foobar.com'
332
+ this = 'Tweet some cool link: <a href="http://foobar.com" class="link">http://foobar.com</a>'
333
+ str.tweetify(url: { class: 'link' }).should eq this
334
+ end
335
+
336
+ it 'should find links, Twitter handles, hashtags and wrap them in anchor tags' do
337
+ str = 'Cool link from @tiagopog! http://foobar.com #rubyrocks'
338
+ this = 'Cool link from <a href="https://twitter.com/tiagopog">@tiagopog</a>! <a href="http://foobar.com">http://foobar.com</a> <a href="https://twitter.com/search?q=%23rubyrocks">#rubyrocks</a>'
339
+ str.tweetify.should eq this
340
+ end
341
+
342
+ it 'should find only Twitter handles' do
343
+ str = 'Cool link from @tiagopog! http://foobar.com #rubyrocks'
344
+ this = 'Cool link from <a href="https://twitter.com/tiagopog">@tiagopog</a>! http://foobar.com #rubyrocks'
345
+ str.tweetify(only: [:tt_handle]).should eq this
346
+ end
347
+
348
+ it 'should find only hashtags' do
349
+ str = 'Cool link from @tiagopog! http://foobar.com #rubyrocks'
350
+ this = 'Cool link from @tiagopog! http://foobar.com <a href="https://twitter.com/search?q=%23rubyrocks">#rubyrocks</a>'
351
+ str.tweetify(only: [:hashtag]).should eq this
352
+ end
353
+
354
+ it 'should not find URLs, just hashtags and Twitter handles' do
355
+ str = 'Cool link from @tiagopog! http://foobar.com #rubyrocks'
356
+ this = 'Cool link from <a href="https://twitter.com/tiagopog">@tiagopog</a>! http://foobar.com <a href="https://twitter.com/search?q=%23rubyrocks">#rubyrocks</a>'
357
+ str.tweetify(only: [:hashtag, :tt_handle]).should eq this
358
+ end
359
+
360
+ it 'should not find URLs, just hashtags and Twitter handles' do
361
+ str = 'Cool link from @tiagopog! http://foobar.com #rubyrocks'
362
+ this = 'Cool link from <a href="https://twitter.com/tiagopog" target="_blank" class="tt-handle">@tiagopog</a>! http://foobar.com <a href="https://twitter.com/search?q=%23rubyrocks" target="_blank" class="hashtag">#rubyrocks</a>'
363
+ str.tweetify(only: [:hashtag, :tt_handle], tt_handle: { target: '_blank', class: 'tt-handle' }, hashtag: { target: '_blank', class: 'hashtag' }).should eq this
364
+ end
349
365
  end
350
366
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string_awesome
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tiago Guedes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-20 00:00:00.000000000 Z
11
+ date: 2014-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport