string_awesome 0.0.1 → 0.2.1

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: 5d675469875aea4b7c4d6530e3047ab432fc6ce2
4
- data.tar.gz: ecd620e210b9b26361c81d6db69bc20318edd2b2
3
+ metadata.gz: f24224fff27c945ef52bf8d04e9e4aeb72ac71ce
4
+ data.tar.gz: 1902cf07e1556874940dc3fda781676e88a25028
5
5
  SHA512:
6
- metadata.gz: eb4a11e17dad5c39df691c91b6b43c50a385236495df4d2d768b56dc538d4f7b79b4565b9262e69360469e7961ef60b8b5f991051c577b2e90bbe6b07a17fb0c
7
- data.tar.gz: 5901dc93bee876e56ef2c15a45f84aee79da423d72df5340480f7798dcda1f812315819e3d6d0e353197ad2bce4d80abdf4d22dfaa96d8070ec59ee9887fbbf0
6
+ metadata.gz: e9290136f50d03073ca5dd70d0029598d73d6793f4cc1bb9b2f935fc56c533d1268e57f4fcb3764460319dc1e3a2ef892e07f67e1216de80f06af9ceec4e623a
7
+ data.tar.gz: 68d9835ff3e6ae6111abab19c82e12834f1439236d236579b574b58bd871c67320bce53a7cfa72130788c172f7668502399651000f2f8dda5ce4e703b4508657
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
data/README.md CHANGED
@@ -1,12 +1,15 @@
1
1
  # StringAwesome
2
+ <a href="https://codeclimate.com/repos/52a6620313d6373dad00a971/feed"><img src="https://codeclimate.com/repos/52a6620313d6373dad00a971/badges/c56ec1b4896feaf32263/gpa.png" /></a>
2
3
 
3
- TODO: Write a gem description
4
+ This gem adds some awesome and easy-to-use extensions to Ruby String class.
4
5
 
5
6
  ## Installation
6
7
 
8
+ Compatible with Ruby 1.9.3+
9
+
7
10
  Add this line to your application's Gemfile:
8
11
 
9
- gem 'string_awesome'
12
+ gem 'string_awesome', '~> 0.2.1'
10
13
 
11
14
  And then execute:
12
15
 
@@ -18,7 +21,154 @@ Or install it yourself as:
18
21
 
19
22
  ## Usage
20
23
 
21
- TODO: Write usage instructions here
24
+ ### HTML
25
+
26
+ #### String#linkify
27
+
28
+ Finds URLs in the text and wrap in anchor tag.
29
+
30
+ ``` ruby
31
+ 'Awesome site: http://foobar.com'.linkify
32
+ #=> 'Awesome site: <a href="http://foobar.com">http://foobar.com</a>'
33
+
34
+ 'www.foobar.com'.linkify
35
+ #=> '<a href="http://www.foobar.com">www.foobar.com</a>'
36
+
37
+ 'foobar.com'.linkify
38
+ #=> '<a href="http://foobar.com">foobar.com</a>'
39
+
40
+ 'Awesome site: http://foobar.com'.linkify(class: 'link', truncate: 10)
41
+ #=> 'Awesome site: <a href="http://foobar.com" class="link">http://foo...</a>'
42
+
43
+ 'Awesome site: http://foobar.com'.linkify(class: 'link', target: '_blank')
44
+ #=> 'Awesome site: <a href="http://foobar.com" class="link" target="_blank">http://foobar.com</a>'
45
+
46
+ 'Awesome site: http://foobar.com'.linkify(truncate: { length: 10, html_encoded: true })
47
+ #=> 'Awesome site: <a href="http://foobar.com">http://foo&hellip;</a>'
48
+ ```
49
+
50
+ #### String#tweetify
51
+
52
+ Finds URLs, Twitter handles, hashtags in the text and wrap in anchor tag.
53
+
54
+ ``` ruby
55
+ 'What about to follow @tiagopog?'.tweetify
56
+ #=> 'What about to follow <a href="https://twitter.com/tiagopog" target="_blank" class="tt-handle">@tiagopog</a>?'
57
+
58
+ "Let's code! #rubyrocks".tweetify
59
+ #=> "Let's code! <a href=\"https://twitter.com/search?q=%23rubyrocks\" target=\"_blank\" class=\"hashtag\">#rubyrocks</a>"
60
+
61
+ 'Cool link from @tiagopog! http://foobar.com #rubyrocks'.tweetify(only: [:hashtag])
62
+ #=> 'Cool link from @tiagopog! http://foobar.com <a href="https://twitter.com/search?q=%23rubyrocks" target="_blank" class="hashtag">#rubyrocks</a>'
63
+
64
+ 'Cool link from @tiagopog! http://foobar.com #rubyrocks'.tweetify(only: [:hashtag, :tt_handle])
65
+ #=> '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>'
66
+ ```
67
+
68
+ #### String#nl2br
69
+
70
+ Replaces \n to \<br /\> tags.
71
+
72
+ ``` ruby
73
+ "Hello
74
+ world!".nl2br
75
+ #=> "Hello <br/ > world!"
76
+ ```
77
+
78
+ #### String#strip_tags
79
+
80
+ Removes all HTML tags from text.
81
+
82
+ ``` ruby
83
+ '<h1><a href="http://somecoolurl.com">Aloha!</a></h1>'.strip_tags
84
+ #=> 'Aloha!'
85
+ ```
86
+
87
+ ### Other Cool Features
88
+
89
+ #### String#ellipsis
90
+
91
+ Appends ellipsis to the text.
92
+
93
+ ``` ruby
94
+ 'lorem ipsum!'.ellipsis
95
+ #=> 'lorem...'
96
+
97
+ "It's a very loooooong text!".ellipsis 11
98
+ #=> "It's a very..."
99
+
100
+ "It's a very loooooong text!".ellipsis 8, after_a_word: true
101
+ #=> "It's a..."
102
+
103
+ 'lorem ipsum'.ellipsis(5, html_encoded: true)
104
+ #=> 'lorem&hellip;'
105
+ ```
106
+
107
+ #### String#no_accents
108
+
109
+ Removes accents from words in the text.
110
+
111
+ ``` ruby
112
+ 'lórem ipsùm dólor sìt ãmet!'.no_accents
113
+ #=> 'lorem ipsum dolor sit amet!'
114
+ ```
115
+
116
+ #### String#slug
117
+
118
+ Parses the text to a valid format for URLs.
119
+
120
+ ``` ruby
121
+ 'Lórem IPSUM Dolor?'.slug
122
+ #=> 'lorem-ipsum-dolor'
123
+ ```
124
+
125
+ #### String#to_title
126
+
127
+ Converts the string into a title style and prevents other letters in the middle of the word from being uppercase.
128
+
129
+ ``` ruby
130
+ 'loREm IPsuM DOLOR'.to_title
131
+ #=> 'Lorem Ipsum Dolor'
132
+ ```
133
+
134
+ #### String#reverse_words
135
+
136
+ Reverses a string by words, instead of reversing it by characters like the String#reverse.
137
+
138
+ ``` ruby
139
+ 'lorem ipsum dolor'.reverse_words
140
+ #=> 'dolor ipsum lorem'
141
+ ```
142
+
143
+ #### String#count_words
144
+
145
+ Counts how many words there are in the text.
146
+
147
+ ``` ruby
148
+ 'lorem ipsum dolor'.count_words
149
+ #=> 3
150
+
151
+ 'lorem ipsum dolor'.count_words 7 # it will count words until this length: 7
152
+ #=> 1
153
+ ```
154
+
155
+ #### String#first_words
156
+
157
+ Returns an Array with the N first words from the text.
158
+
159
+ ``` ruby
160
+ 'lorem ipsum dolor'.first_words 2
161
+ #=> 'lorem ipsum'
162
+ ```
163
+
164
+ #### String#last_words
165
+
166
+ Returns am Array with the N last words from the text.
167
+
168
+ ``` ruby
169
+ 'lorem ipsum dolor'.last_words 2
170
+ #=> 'ipsum dolor'
171
+ ```
22
172
 
23
173
  ## Contributing
24
174
 
@@ -1,11 +1,12 @@
1
1
  # coding: utf-8
2
-
2
+ require 'string_awesome/awesome_regexes'
3
3
  require 'active_support/inflector'
4
4
  require 'sanitize'
5
5
 
6
6
  module StringAwesome
7
7
  # These methods are all included into the String class.
8
8
  module AwesomeMethods
9
+ include StringAwesome::AwesomeRegexes
9
10
  # Replaces \n to <br /> tags.
10
11
  #
11
12
  # Example:
@@ -17,8 +18,8 @@ module StringAwesome
17
18
  self.gsub /\n/, '<br />'
18
19
  end
19
20
 
20
- # Converts the string to the title style and prevents other
21
- # letters in the middle of the word from being uppercase.
21
+ # Converts the string into a title style and prevents
22
+ # other letters in the middle of the word from being uppercase.
22
23
  #
23
24
  # Example:
24
25
  # >> 'loREm IPsuM DOLOR'.to_title
@@ -28,32 +29,27 @@ module StringAwesome
28
29
  self.downcase.titleize
29
30
  end
30
31
 
31
- # Removes HTML tags from text.
32
+ # Removes all HTML tags from text.
32
33
  #
33
34
  # Example:
34
35
  # >> '<h1><a href="http://somecoolurl.com">Aloha!</a></h1>'.strip_tags
35
36
  # => 'Aloha!'
36
- #
37
- # Arguments:
38
- # allow_whitespace: (Boolean)
39
- # - Let it returns the replaced block HTML tags as whitespaces.
40
37
 
41
- def strip_tags(allow_whitespace = false)
42
- str = Sanitize.clean self
43
- allow_whitespace ? str : str.strip
38
+ def strip_tags
39
+ Sanitize.clean(self).gsub(/\s+/, ' ').strip
44
40
  end
45
41
 
46
- # Remove accents from words in the text.
42
+ # Removes accents from words in the text.
47
43
  #
48
44
  # Example:
49
45
  # >> 'lórem ipsùm dólor sìt ãmet!'.no_accents
50
46
  # => 'lorem ipsum dolor sit amet!'
51
47
 
52
48
  def no_accents
53
- self.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, '').to_s
49
+ self.mb_chars.normalize(:kd).gsub(SA_ACCENT_REGEX, '').to_s
54
50
  end
55
51
 
56
- # Parses text to a valid format for URL's.
52
+ # Parses the text to a valid format for URLs.
57
53
  #
58
54
  # Example:
59
55
  # >> 'Lórem IPSUM Dolor?'.slug
@@ -61,65 +57,31 @@ module StringAwesome
61
57
  #
62
58
  # Arguments:
63
59
  # downcase: (Boolean)
64
- # - If true, it will force the String to be in downcase.
60
+ # - If true, it will force the String to be in downcase.
65
61
 
66
62
  def slug(downcase = true)
67
- str = self.no_accents.gsub(/\W|_/, '-').gsub(/[-]{2,}/, '-').gsub(/^-|-$/, '').to_s
63
+ str = self.no_accents.words.join '-'
68
64
  downcase ? str.downcase : str
69
65
  end
70
66
 
71
- # Append ellipsis to the text.
67
+ # Returns an array with all the words from the string.
72
68
  #
73
69
  # Example:
74
- # >> "It's a very loooooong text!".ellipsis 11
75
- # => "It's a very..."
76
- # >> "It's a very loooooong text!".ellipsis 8, after_a_word: true
77
- # => "It's a..."
78
- #
79
- # Arguments:
80
- # max_length: (Integer)
81
- # - Indicates the max length expected, before ellipsis, for the result.
82
- # options: (Hash)
83
- # - Other options such as:
84
- # - :html_encoded - If true, the ellipsis will be displayed in HTML encoded format: &hellip;.
85
- # - :after_a_word - If true, the ellipsis will be displayed necessarily after a word.
70
+ # >> 'Lorem! Ipsum dolor, sit amet 2013.'.words
71
+ # => ['Lorem', 'Ipsum', 'dolor', 'sit', 'amet', '2013']
86
72
 
87
- def ellipsis(max_length = 0, options = {})
88
- length = self.length
89
-
90
- if length > 1 and max_length <= length
91
- # Adjusts the max_length
92
- max_length = (length / 2).round if max_length == 0
93
-
94
- # Truncates the text according to the max_length
95
- str = self[0...max_length]
96
-
97
- # Defines how the ellipsis will be displayed
98
- ellip = options[:html_encoded] == true ? '&hellip;' : '...'
99
-
100
- # If ellipsis must be applied after a word
101
- if options[:after_a_word] == true
102
- words = str.split(/\s/)
103
- words = words[0..words.length - 2] if words.length > 1
104
- str = words.join(' ')
105
- else
106
- str = str.gsub(/\s+$/, '')
107
- end
108
-
109
- str + ellip
110
- else
111
- self
112
- end
73
+ def words
74
+ self.split(/[\s\W]+/)
113
75
  end
114
76
 
115
- # Reverses a string by words, instead of reversing it by every character (String#reverse).
77
+ # Reverses a string by words, instead of reversing it by characters.
116
78
  #
117
79
  # Example:
118
80
  # >> 'lorem ipsum dolor'.reverse_words
119
81
  # => 'dolor ipsum lorem'
120
82
 
121
83
  def reverse_words
122
- self.split(/\s/).reverse.join(' ')
84
+ self.words.reverse.join(' ')
123
85
  end
124
86
 
125
87
  # Counts how many words there are in the string limited by the max_length value.
@@ -131,20 +93,18 @@ module StringAwesome
131
93
  # => 1
132
94
  # Arguments:
133
95
  # max_length: (Integer)
134
- # - References where it will stop counting words in the string.
96
+ # - References where it will stop counting words in the string.
135
97
 
136
98
  def count_words(max_length = nil)
137
- # No duplicated whitespaces
138
- str = self.gsub(/[\s\W]+/, ' ')
139
99
  # Counts words
140
- count = (max_length ? str[0...max_length] : str).split(/\s/).count
141
- # Checks whether the last word is really a word (must be followed by a whitespace)
142
- count -= 1 unless !max_length or (str[max_length - 1] =~ /\s/) or (!(str[max_length - 1] =~ /\W/) and (str[max_length] =~ /\s/))
100
+ count = (max_length ? self[0...max_length] : self).words.count
101
+ # Checks if the last word is complete
102
+ count -= 1 if max_length and self[max_length - 1, 2] =~ /\w{2}/
143
103
 
144
104
  count
145
105
  end
146
106
 
147
- # Returns an Array with the N first words of a string.
107
+ # Returns an Array with the N first words from the text.
148
108
  #
149
109
  # Example:
150
110
  # >> 'lorem ipsum'.first_words
@@ -153,14 +113,13 @@ module StringAwesome
153
113
  # => 'lorem ipsum'
154
114
  # Arguments:
155
115
  # amount: (Integer)
156
- # - Indicates how many words it expects to ge
116
+ # - Indicates how many words it expects to ge
157
117
 
158
118
  def first_words(amount = nil)
159
- words = self.split(/[\s\W]+/)
160
- amount ? words[0...amount] : words
119
+ amount ? self.words[0...amount] : self.words
161
120
  end
162
121
 
163
- # Returns the N last words of a string.
122
+ # Returns am Array with the N last words from the text.
164
123
  #
165
124
  # Example:
166
125
  # >> 'lorem ipsum'.last_words
@@ -169,14 +128,68 @@ module StringAwesome
169
128
  # => 'ipsum dolor'
170
129
  # Arguments:
171
130
  # amount: (Integer)
172
- # - Indicates how many words it expects to ge
131
+ # - Indicates how many words it expects to ge
173
132
 
174
133
  def last_words(amount = nil)
175
- words = self.split(/[\s\W]+/).reverse
134
+ words = self.words.reverse
176
135
  (amount ? words[0...amount] : words).reverse
177
136
  end
178
137
 
179
- # Replaces all URL's in the text with HTML link tags
138
+ # Appends ellipsis to the text.
139
+ #
140
+ # Example:
141
+ # >> "It's a very loooooong text!".ellipsis 11
142
+ # => "It's a very..."
143
+ # >> "It's a very loooooong text!".ellipsis 8, after_a_word: true
144
+ # => "It's a..."
145
+ #
146
+ # Arguments:
147
+ # max_length: (Integer)
148
+ # - Indicates the max length expected, before ellipsis, for the result.
149
+ # options: (Hash)
150
+ # - Other options such as:
151
+ # - :html_encoded - If true, the ellipsis will be displayed in HTML encoded format: &hellip;.
152
+ # - :after_a_word - If true, the ellipsis will be displayed necessarily after a word.
153
+
154
+ def ellipsis(max_length = 0, options = {})
155
+ length = self.length
156
+ return self if length <= 1 or length < max_length
157
+
158
+ # Adjusts the max_length
159
+ max_length = (length / 2).round if max_length == 0
160
+
161
+ # Truncates the string
162
+ str = self.truncate(max_length, options[:after_a_word]).strip
163
+
164
+ # Appends ellipsis
165
+ str << (options[:html_encoded] == true ? '&hellip;' : '...')
166
+ end
167
+
168
+ # Truncates the text.
169
+ #
170
+ # Example:
171
+ # >> "It's a very loooooong text!".truncate 11
172
+ # => "It's a very"
173
+ # >> "It's a very loooooong text!".ellipsis 8, true
174
+ # => "It's a"
175
+ #
176
+ # Arguments:
177
+ # max_length: (Integer)
178
+ # - Indicates the max length expected, before ellipsis, for the result.
179
+ # after_a_word: (Boolean)
180
+ # - If true, the ellipsis will be displayed necessarily after a word.
181
+ def truncate(length, after_a_word = false)
182
+ str = self[0...length]
183
+
184
+ if after_a_word == true
185
+ words = str.split(/\s+/)
186
+ str = words[0..words.length - 2].join(' ')
187
+ end
188
+
189
+ str
190
+ end
191
+
192
+ # Finds URLs in text and wrap in anchor tag.
180
193
  #
181
194
  # Example:
182
195
  # >> 'Awesome site: http://foobar.com'.linkify
@@ -186,63 +199,61 @@ module StringAwesome
186
199
  # Arguments:
187
200
  # options: (Hash)
188
201
  # - Options for the link tag, such as:
189
- # - :truncate - If set, it will truncate the URL displayed in the link tag
202
+ # - :truncate - If set, it will truncate the URL displayed in the anchor tag
190
203
  # and put an ellipsis according to the given length. It can
191
204
  # be also a Hash of options:
192
- # - :length - URL's new length.
205
+ # - :length - URLs new length.
193
206
  # - :html_encoded - Ellipsis will be displayed as HTML encoded char.
194
207
  # - :class - Value for "class" attribute: <a href="url" class="link">url</a>
195
208
  # - :target - Value for "target" attribute: <a href="url" target="_blank">url</a>
196
209
 
197
210
  def linkify(options = {})
198
- self.gsub!(/\b(((ht|f)tp[s]?:\/\/)?([a-z0-9]+\.)?(?<!@)([a-z0-9\_\-]+)(\.[a-z]+)+([\?\/\:][a-z0-9_=%&@\?\.\/\-\:\#\(\)]+)?\/?)/i) do
199
- displayed = match = $1
200
-
201
- # Truncates the URL
202
- if options[:truncate]
203
- t = options[:truncate]
204
- displayed = t.instance_of?(Hash) ? match.ellipsis(t[:length], html_encoded: t[:html_encoded]) : match.ellipsis(t)
205
- end
211
+ self.gsub!(SA_URL_REGEX) do |match|
212
+ displayed = _truncate_url match, options[:truncate]
213
+
214
+ options.delete :truncate
206
215
 
207
216
  # Applies 'class' and 'target' options
208
- if !options
209
- options = ''
210
- else
211
- options = options.reduce ' ' do |s, v|
212
- s << (v[0] == :truncate ? '' : "#{v[0]}=\"#{v[1]}\" ")
213
- end.gsub(/\s+$/, '')
214
- end
217
+ options = !options ? '' : options.reduce(' ') { |s, v| s << "#{v[0]}=\"#{v[1]}\" " }
215
218
 
216
219
  # Forces the presence of the 'http://'
217
- match = "http://#{match}" unless match =~ /(ht|f)tp[s]?/i
220
+ match = "http://#{match}" unless match =~ SA_PROTOCOL_REGEX
218
221
 
219
- "<a href=\"#{match}\"#{options}>#{displayed}</a>"
222
+ "<a href=\"#{match}\"#{options.gsub(/\s+$/, '')}>#{displayed}</a>"
220
223
  end
221
224
  self
222
225
  end
223
226
 
224
- # Matches URL's, Twitter handles and hashtags putting them into HTML link tags.
227
+ # Trancutes the URL that will be displayed in the <a> tag
228
+ #
229
+ # Arguments:
230
+ # m: (String)
231
+ # - Matched URL.
232
+ # t: (Hash|Integer)
233
+ # - Where the URL will be truncated.
234
+ def _truncate_url(m, t)
235
+ t ? (t.instance_of?(Hash) ? m.ellipsis(t[:length], html_encoded: t[:html_encoded]) : m.ellipsis(t)) : m
236
+ end
237
+
238
+ # Finds URLs, Twitter handles, hashtags in the text and wrap in anchor tag.
225
239
  #
226
240
  # Example:
227
- # >> 'What about to follow @tiagopog?'
241
+ # >> 'What about to follow @tiagopog?'.tweetfy
228
242
  # => 'What about to follow <a href="https://twitter.com/tiagopog" target="_blank" class="tt-handle">@tiagopog</a>?'
229
- # >> "Let's code! #rubyrocks"
243
+ # >> "Let's code! #rubyrocks".tweetfy
230
244
  # => "Let's code! <a href=\"https://twitter.com/search?q=%23rubyrocks\" target=\"_blank\" class=\"hashtag\">#rubyrocks</a>"
231
245
  # Arguments:
232
246
  # options: (Hash)
233
- # - Options such as:
234
- # - :only - Array of Symbols restricting what will be matched on the text.
247
+ # - Options such as:
248
+ # - :only - Array of Symbols restricting what will be matched on the text.
235
249
 
236
250
  def tweetify(options = {})
237
251
  # Applies linkify unless there's some restriction
238
252
  only = options[:only]
239
253
  str = only ? self : self.linkify(class: 'link')
240
254
 
241
- # Stores the regex in a variable just to make it more readable
242
- regex = /(((^#)([a-z0-9\_]+))|(([^a-z0-9\W]|\s)((#)([a-z0-9\_]+))))|(((^@)([a-z0-9\_]+))|(([^a-z0-9\W]|\s)((@)([a-z0-9\_]+))))/i
243
-
244
255
  # Iterates with the matched expressions
245
- str.gsub!(regex) do |match|
256
+ str.gsub!(SA_TWEET_REGEX) do |match|
246
257
  is_hashtag = match =~ /#/
247
258
 
248
259
  if only and ([:hashtag, :tt_handle] != only.sort) and ((is_hashtag and !only.include?(:hashtag)) or (!is_hashtag and !only.include?(:tt_handle)))
@@ -261,4 +272,4 @@ module StringAwesome
261
272
  str
262
273
  end
263
274
  end
264
- end
275
+ end
@@ -0,0 +1,8 @@
1
+ module StringAwesome
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
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module StringAwesome
2
- VERSION = '0.0.1'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -32,12 +32,8 @@ describe 'String#strip_tags' do
32
32
  '<h1><a href="http://somecoolurl.com">Aloha!</a></h1>'.strip_tags.should eq 'Aloha!'
33
33
  end
34
34
 
35
- it 'should remove by default the whitespaces that were allocated in place of HTML tags' do
36
- '<h1>no whitespaces</h1>'.strip_tags.should eq 'no whitespaces'
37
- end
38
-
39
- it 'should not remove the whitespaces that were allocated in place of HTML tags, when required' do
40
- '<h1>whitespaces</h1>'.strip_tags(true).should eq ' whitespaces '
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'
41
37
  end
42
38
  end
43
39
 
@@ -63,6 +59,19 @@ describe 'String#slug' do
63
59
  end
64
60
  end
65
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
+
66
75
  #
67
76
  # String#ellipsis
68
77
  #
@@ -83,7 +92,7 @@ describe 'String#ellipsis' do
83
92
  'lorem ipsum'.ellipsis.should eq 'lorem...'
84
93
  end
85
94
 
86
- it "should append the HTML encoded ellipsis in the text" do
95
+ it 'should append the HTML encoded ellipsis in the text' do
87
96
  'lorem ipsum'.ellipsis(5, html_encoded: true).should eq 'lorem&hellip;'
88
97
  end
89
98
 
@@ -100,6 +109,15 @@ describe 'String#ellipsis' do
100
109
  end
101
110
  end
102
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
+
103
121
  #
104
122
  # String#reverse_words
105
123
  #
@@ -168,37 +186,37 @@ end
168
186
  # String#linkify
169
187
  #
170
188
  describe 'String#linkify' do
171
- it "should replace all the URL's in the text with HTML link tags" do
189
+ it 'should find all the URLs in text and wrap in anchor tags' do
172
190
  str = 'Awesome site: http://foobar.com'
173
191
  this = 'Awesome site: <a href="http://foobar.com">http://foobar.com</a>'
174
192
  str.linkify.should eq this
175
193
  end
176
194
 
177
- it 'can set the "class" HTML attribute to be applied on the link tag' do
195
+ it 'can set the "class" HTML attribute to be applied on the anchor tag' do
178
196
  str = 'Awesome site: http://foobar.com'
179
197
  this = 'Awesome site: <a href="http://foobar.com" class="link">http://foobar.com</a>'
180
198
  str.linkify(class: 'link').should eq this
181
199
  end
182
200
 
183
- it 'can set the "target" HTML attribute to be applied on the link tag' do
201
+ it 'can set the "target" HTML attribute to be applied on the anchor tag' do
184
202
  str = 'Awesome site: http://foobar.com'
185
203
  this = 'Awesome site: <a href="http://foobar.com" target="_blank">http://foobar.com</a>'
186
204
  str.linkify(target: '_blank').should eq this
187
205
  end
188
206
 
189
- it 'can set the class and target HTML attributes to be applied on the link tag' do
207
+ it 'can set the class and target HTML attributes to be applied on the anchor tag' do
190
208
  str = 'Awesome site: http://foobar.com'
191
209
  this = 'Awesome site: <a href="http://foobar.com" class="link" target="_blank">http://foobar.com</a>'
192
210
  str.linkify(class: 'link', target: '_blank').should eq this
193
211
  end
194
212
 
195
- it 'can truncate the URL displayed whithin the link tag (Interger param)' do
213
+ it 'can truncate the URL displayed whithin the anchor tag (Interger param)' do
196
214
  str = 'Awesome site: http://foobar.com'
197
215
  this = 'Awesome site: <a href="http://foobar.com">http://foo...</a>'
198
216
  str.linkify(truncate: 10).should eq this
199
217
  end
200
218
 
201
- it 'can truncate the URL displayed whithin the link tag (Hash param)' do
219
+ it 'can truncate the URL displayed whithin the anchor tag (Hash param)' do
202
220
  str = 'Awesome site: http://foobar.com'
203
221
  this = 'Awesome site: <a href="http://foobar.com">http://foo&hellip;</a>'
204
222
  str.linkify(truncate: { length: 10, html_encoded: true }).should eq this
@@ -210,11 +228,11 @@ describe 'String#linkify' do
210
228
  str.linkify(class: 'link', truncate: 10).should eq this
211
229
  end
212
230
 
213
- it "matches URL's without the presence of 'http://' but presenting 'www'" do
231
+ it "matches URLs without the presence of 'http://' but presenting 'www'" do
214
232
  'www.foobar.com'.linkify.should eq '<a href="http://www.foobar.com">www.foobar.com</a>'
215
233
  end
216
234
 
217
- it "matches URL's without the presence of 'http://' and 'www'" do
235
+ it "matches URLs without the presence of 'http://' and 'www'" do
218
236
  'foobar.com'.linkify.should eq '<a href="http://foobar.com">foobar.com</a>'
219
237
  end
220
238
  end
@@ -223,7 +241,7 @@ end
223
241
  # String#tweetify
224
242
  #
225
243
  describe 'String#tweetify' do
226
- it 'should match Twitter handles (@username) and replace them into HTML link tags' do
244
+ it 'should find Twitter handles (@username) and wrap them in anchor tags' do
227
245
  str = 'What about to follow @tiagopog?'
228
246
  this = 'What about to follow <a href="https://twitter.com/tiagopog" target="_blank" class="tt-handle">@tiagopog</a>?'
229
247
  str.tweetify.should eq this
@@ -234,7 +252,7 @@ describe 'String#tweetify' do
234
252
  str.tweetify.should eq str
235
253
  end
236
254
 
237
- it 'should match hashtags (#hashtag) and replace them into HTML link tags' do
255
+ it 'should find hashtags (#hashtag) and wrap them in anchor tags' do
238
256
  str = "Let's code! #rubyrocks"
239
257
  this = "Let's code! <a href=\"https://twitter.com/search?q=%23rubyrocks\" target=\"_blank\" class=\"hashtag\">#rubyrocks</a>"
240
258
  str.tweetify.should eq this
@@ -245,31 +263,31 @@ describe 'String#tweetify' do
245
263
  str.tweetify.should eq str
246
264
  end
247
265
 
248
- it "should match URL's and replace them into HTML link tags" do
266
+ it 'should find URLs and wrap them in anchor tags' do
249
267
  str = 'Tweet some cool link: http://foobar.com'
250
268
  this = 'Tweet some cool link: <a href="http://foobar.com" class="link">http://foobar.com</a>'
251
269
  str.tweetify.should eq this
252
270
  end
253
271
 
254
- it 'should match links, Twitter handles, hashtags and replace them into HTML link tags' do
272
+ it 'should find links, Twitter handles, hashtags and wrap them in anchor tags' do
255
273
  str = 'Cool link from @tiagopog! http://foobar.com #rubyrocks'
256
274
  this = 'Cool link from <a href="https://twitter.com/tiagopog" target="_blank" class="tt-handle">@tiagopog</a>! <a href="http://foobar.com" class="link">http://foobar.com</a> <a href="https://twitter.com/search?q=%23rubyrocks" target="_blank" class="hashtag">#rubyrocks</a>'
257
275
  str.tweetify.should eq this
258
276
  end
259
277
 
260
- it 'should match only Twitter handles' do
278
+ it 'should find only Twitter handles' do
261
279
  str = 'Cool link from @tiagopog! http://foobar.com #rubyrocks'
262
280
  this = 'Cool link from <a href="https://twitter.com/tiagopog" target="_blank" class="tt-handle">@tiagopog</a>! http://foobar.com #rubyrocks'
263
281
  str.tweetify(only: [:tt_handle]).should eq this
264
282
  end
265
283
 
266
- it 'should match only hashtags' do
284
+ it 'should find only hashtags' do
267
285
  str = 'Cool link from @tiagopog! http://foobar.com #rubyrocks'
268
286
  this = 'Cool link from @tiagopog! http://foobar.com <a href="https://twitter.com/search?q=%23rubyrocks" target="_blank" class="hashtag">#rubyrocks</a>'
269
287
  str.tweetify(only: [:hashtag]).should eq this
270
288
  end
271
289
 
272
- it "should not match URL's, just hashtags and Twitter handles" do
290
+ it 'should not find URLs, just hashtags and Twitter handles' do
273
291
  str = 'Cool link from @tiagopog! http://foobar.com #rubyrocks'
274
292
  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>'
275
293
  str.tweetify(only: [:hashtag, :tt_handle]).should eq this
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.0.1
4
+ version: 0.2.1
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-08 00:00:00.000000000 Z
11
+ date: 2013-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -108,12 +108,14 @@ extensions: []
108
108
  extra_rdoc_files: []
109
109
  files:
110
110
  - .gitignore
111
+ - .travis.yml
111
112
  - Gemfile
112
113
  - LICENSE.txt
113
114
  - README.md
114
115
  - Rakefile
115
116
  - lib/string_awesome.rb
116
117
  - lib/string_awesome/awesome_methods.rb
118
+ - lib/string_awesome/awesome_regexes.rb
117
119
  - lib/string_awesome/version.rb
118
120
  - spec/awesome_methods_spec.rb
119
121
  - spec/spec_helper.rb