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 +4 -4
- data/README.md +2 -2
- data/Rakefile +1 -1
- data/lib/string_awesome/awesome_methods.rb +11 -5
- data/lib/string_awesome/awesome_regexes.rb +6 -4
- data/lib/string_awesome/version.rb +1 -1
- data/spec/awesome_methods_spec.rb +360 -344
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f78f62624cff19d1223034e0de8c83df051ba22e
|
4
|
+
data.tar.gz: 6b0f254d8cd5c41a76bda6db88d2c0a0e649c052
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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#
|
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?'.
|
122
|
+
'Lórem IPSUM Dolor?'.slugfy
|
123
123
|
#=> 'lorem-ipsum-dolor'
|
124
124
|
```
|
125
125
|
|
data/Rakefile
CHANGED
@@ -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(
|
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
|
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!(
|
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 =~
|
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!(
|
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
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
@@ -2,349 +2,365 @@
|
|
2
2
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
|
-
|
6
|
-
#
|
7
|
-
#
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
#
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
end
|
119
|
-
|
120
|
-
|
121
|
-
#
|
122
|
-
#
|
123
|
-
|
124
|
-
describe '
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
end
|
129
|
-
|
130
|
-
#
|
131
|
-
# String#
|
132
|
-
#
|
133
|
-
|
134
|
-
|
135
|
-
'
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
#
|
173
|
-
|
174
|
-
#
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
end
|
183
|
-
|
184
|
-
|
185
|
-
#
|
186
|
-
#
|
187
|
-
|
188
|
-
describe '
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
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…'
|
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…'
|
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…'
|
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…</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.
|
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:
|
11
|
+
date: 2014-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|