string_awesome 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f24224fff27c945ef52bf8d04e9e4aeb72ac71ce
4
- data.tar.gz: 1902cf07e1556874940dc3fda781676e88a25028
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDQyMTRjYWQwMzcxOGRmOTVkNWMyYzU2ODI2NmE5MTZhMTFjNDM1Mw==
5
+ data.tar.gz: !binary |-
6
+ NzYxYjA3Mjc0YmM1OTdmMWRkOTE2NTg1MzNkN2UxMzE3YWJlNDg1Mg==
5
7
  SHA512:
6
- metadata.gz: e9290136f50d03073ca5dd70d0029598d73d6793f4cc1bb9b2f935fc56c533d1268e57f4fcb3764460319dc1e3a2ef892e07f67e1216de80f06af9ceec4e623a
7
- data.tar.gz: 68d9835ff3e6ae6111abab19c82e12834f1439236d236579b574b58bd871c67320bce53a7cfa72130788c172f7668502399651000f2f8dda5ce4e703b4508657
8
+ metadata.gz: !binary |-
9
+ MGZiZjBkOTc2OTdhNjEyYjAxZmNmOGY2MTdiOTQ4YmViN2I4NzUyYjE5ZDQ3
10
+ MTE5OTZlZDEzYjZjMGYxMDJiMzg2NzBmMzk4YTY1ZDk3YzhjZTczNDg0YTli
11
+ ZGY3ZWQ2MTE1OWYyYWFjMGM4MTlhNWY2NmJmMDE5NTAzNzQ3YjY=
12
+ data.tar.gz: !binary |-
13
+ NmI5MWVhNzk2MjI5NTVjOWU3NjYxM2Y5MmQ2NjRjNmRkMTA0NDlmZGQ4NDMw
14
+ MWI1NWM3ZWI1ZDY5OWY2ZTRkOGJlY2U5YzY1YTgwMDI3ZmVhYTE4NmUwZTQx
15
+ NDZhOThlY2U1ODI1NThkYTc0ZjNlYWI3Y2U5MjhlYjhhMmQ4ZmQ=
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
- # StringAwesome
2
- <a href="https://codeclimate.com/repos/52a6620313d6373dad00a971/feed"><img src="https://codeclimate.com/repos/52a6620313d6373dad00a971/badges/c56ec1b4896feaf32263/gpa.png" /></a>
1
+ # String Awesome
2
+
3
+ [![Build Status](https://travis-ci.org/tiagopog/string_awesome.png?branch=master)](https://travis-ci.org/tiagopog/string_awesome)
4
+ [![Code Climate](https://codeclimate.com/repos/52a5452b7e00a4670f00b139/badges/f9183e7116f7e040c184/gpa.png)](https://codeclimate.com/repos/52a5452b7e00a4670f00b139/feed)
5
+ [![Dependency Status](https://gemnasium.com/tiagopog/string_awesome.png)](https://gemnasium.com/tiagopog/string_awesome)
6
+ [![Gem Version](https://badge.fury.io/rb/string_awesome.png)](http://badge.fury.io/rb/string_awesome)
3
7
 
4
8
  This gem adds some awesome and easy-to-use extensions to Ruby String class.
5
9
 
@@ -9,7 +13,7 @@ Compatible with Ruby 1.9.3+
9
13
 
10
14
  Add this line to your application's Gemfile:
11
15
 
12
- gem 'string_awesome', '~> 0.2.1'
16
+ gem 'string_awesome'
13
17
 
14
18
  And then execute:
15
19
 
@@ -34,9 +38,6 @@ Finds URLs in the text and wrap in anchor tag.
34
38
  'www.foobar.com'.linkify
35
39
  #=> '<a href="http://www.foobar.com">www.foobar.com</a>'
36
40
 
37
- 'foobar.com'.linkify
38
- #=> '<a href="http://foobar.com">foobar.com</a>'
39
-
40
41
  'Awesome site: http://foobar.com'.linkify(class: 'link', truncate: 10)
41
42
  #=> 'Awesome site: <a href="http://foobar.com" class="link">http://foo...</a>'
42
43
 
data/Rakefile CHANGED
@@ -1 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
@@ -209,20 +209,31 @@ module StringAwesome
209
209
 
210
210
  def linkify(options = {})
211
211
  self.gsub!(SA_URL_REGEX) do |match|
212
+ # http://verylongurl...
212
213
  displayed = _truncate_url match, options[:truncate]
213
214
 
214
- options.delete :truncate
215
-
216
- # Applies 'class' and 'target' options
217
- options = !options ? '' : options.reduce(' ') { |s, v| s << "#{v[0]}=\"#{v[1]}\" " }
218
-
215
+ # Now that we're done with the 'truncate' option, let's remove it...
216
+ options.delete(:truncate) unless !options
217
+
219
218
  # Forces the presence of the 'http://'
220
219
  match = "http://#{match}" unless match =~ SA_PROTOCOL_REGEX
221
220
 
222
- "<a href=\"#{match}\"#{options.gsub(/\s+$/, '')}>#{displayed}</a>"
221
+ "<a href=\"#{match}\"#{_apply_tag_attrs(options)}>#{displayed}</a>"
223
222
  end
224
223
  self
225
224
  end
225
+
226
+ # Build attributes from Hash for HTML tag
227
+ #
228
+ # Arguments:
229
+ # options: (Hash)
230
+ # - Options for the link tag, such as:
231
+ # - :class - Value for "class" attribute: <a href="url" class="link">url</a>
232
+ # - :target - Value for "target" attribute: <a href="url" target="_blank">url</a>
233
+
234
+ def _apply_tag_attrs(options)
235
+ options ? options.reduce(' ') { |s, v| s << "#{v[0]}=\"#{v[1]}\" " }.gsub(/\s+$/, '') : ''
236
+ end
226
237
 
227
238
  # Trancutes the URL that will be displayed in the <a> tag
228
239
  #
@@ -231,8 +242,9 @@ module StringAwesome
231
242
  # - Matched URL.
232
243
  # t: (Hash|Integer)
233
244
  # - Where the URL will be truncated.
245
+
234
246
  def _truncate_url(m, t)
235
- t ? (t.instance_of?(Hash) ? m.ellipsis(t[:length], html_encoded: t[:html_encoded]) : m.ellipsis(t)) : m
247
+ t ? (t.instance_of?(Hash) ? m.ellipsis(t[:length], html_encoded: t[:html_encoded]) : m.ellipsis(t)) : m
236
248
  end
237
249
 
238
250
  # Finds URLs, Twitter handles, hashtags in the text and wrap in anchor tag.
@@ -245,31 +257,38 @@ module StringAwesome
245
257
  # Arguments:
246
258
  # options: (Hash)
247
259
  # - Options such as:
248
- # - :only - Array of Symbols restricting what will be matched on the text.
260
+ # - :only - Array of Symbols restricting what will be matched in the text.
249
261
 
250
262
  def tweetify(options = {})
251
263
  # Applies linkify unless there's some restriction
252
- only = options[:only]
253
- str = only ? self : self.linkify(class: 'link')
264
+ str = options[:only] ? self : self.linkify(class: 'link')
254
265
 
255
266
  # Iterates with the matched expressions
256
267
  str.gsub!(SA_TWEET_REGEX) do |match|
257
268
  is_hashtag = match =~ /#/
258
269
 
259
- if only and ([:hashtag, :tt_handle] != only.sort) and ((is_hashtag and !only.include?(:hashtag)) or (!is_hashtag and !only.include?(:tt_handle)))
260
- match
261
- else
262
- match = match.strip
263
- tt_url = 'https://twitter.com/'
264
- tag = {
265
- href: is_hashtag ? "#{tt_url}search?q=%23#{match.gsub(/#/, '')}" : "#{tt_url}#{match.gsub(/@/, '')}",
266
- class: is_hashtag ? 'hashtag' : 'tt-handle'
267
- }
268
-
269
- " <a href=\"#{tag[:href]}\" target=\"_blank\" class=\"#{tag[:class]}\">#{match}</a>"
270
+ unless _restricted?(is_hashtag, options[:only])
271
+ match = match.strip
272
+ attrs = is_hashtag ? ['hashtag', "search?q=%23#{match.gsub(/#/, '')}"] : ['tt-handle', "#{match.gsub(/@/, '')}"]
273
+ attrs = { class: attrs[0], href: attrs[1] }
274
+ match = " <a href=\"https://twitter.com/#{attrs[:href]}\" target=\"_blank\" class=\"#{attrs[:class]}\">#{match}</a>"
270
275
  end
276
+
277
+ match
271
278
  end
272
279
  str
273
280
  end
281
+
282
+ # Checks if type (:hashtag or :tt_handle) is allowed to be wrapped in anchor tag.
283
+ #
284
+ # Arguments:
285
+ # is_hashtag: (Boolean)
286
+ # - Does the string contain '#'?
287
+ # only: (Array)
288
+ # - Types allowed: :hashtag, :tt_handle.
289
+
290
+ def _restricted?(is_hashtag, only)
291
+ only and ([:hashtag, :tt_handle] != only.sort) and ((is_hashtag and !only.include?(:hashtag)) or (!is_hashtag and !only.include?(:tt_handle)))
292
+ end
274
293
  end
275
294
  end
@@ -1,7 +1,7 @@
1
1
  module StringAwesome
2
2
  module AwesomeRegexes
3
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
4
+ SA_URL_REGEX = /\b((((ht|f)tp[s]?:\/\/)|([a-z0-9]+\.))+(?<!@)([a-z0-9\_\-]+)(\.[a-z]+)+([\?\/\:][a-z0-9_=%&@\?\.\/\-\:\#\(\)]+)?\/?)/i
5
5
  SA_PROTOCOL_REGEX = /(ht|f)tp[s]?/i
6
6
  SA_TWEET_REGEX = /(((^[@#])|([^a-z0-9\W]|\s)([@|#]))([a-z0-9\_]+))/i
7
7
  end
@@ -1,3 +1,3 @@
1
1
  module StringAwesome
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
@@ -186,12 +186,45 @@ end
186
186
  # String#linkify
187
187
  #
188
188
  describe 'String#linkify' do
189
- it 'should find all the URLs in text and wrap in anchor tags' do
189
+ it 'should find all the HTTP URLs in text and wrap in anchor tags' do
190
190
  str = 'Awesome site: http://foobar.com'
191
191
  this = 'Awesome site: <a href="http://foobar.com">http://foobar.com</a>'
192
192
  str.linkify.should eq this
193
193
  end
194
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
+
195
228
  it 'can set the "class" HTML attribute to be applied on the anchor tag' do
196
229
  str = 'Awesome site: http://foobar.com'
197
230
  this = 'Awesome site: <a href="http://foobar.com" class="link">http://foobar.com</a>'
@@ -232,8 +265,8 @@ describe 'String#linkify' do
232
265
  'www.foobar.com'.linkify.should eq '<a href="http://www.foobar.com">www.foobar.com</a>'
233
266
  end
234
267
 
235
- it "matches URLs without the presence of 'http://' and 'www'" do
236
- 'foobar.com'.linkify.should eq '<a href="http://foobar.com">foobar.com</a>'
268
+ it "should not match URLs without the presence of 'http://' and 'www'" do
269
+ 'foobar.com'.linkify.should eq 'foobar.com'
237
270
  end
238
271
  end
239
272
 
@@ -248,8 +281,7 @@ describe 'String#tweetify' do
248
281
  end
249
282
 
250
283
  it 'should not match foo@bar as being a Twitter handle' do
251
- str = 'foo@bar'
252
- str.tweetify.should eq str
284
+ 'foo@bar'.tweetify.should eq 'foo@bar'
253
285
  end
254
286
 
255
287
  it 'should find hashtags (#hashtag) and wrap them in anchor tags' do
@@ -259,8 +291,7 @@ describe 'String#tweetify' do
259
291
  end
260
292
 
261
293
  it 'should not match foo#bar as being a hashtag' do
262
- str = 'foo#bar'
263
- str.tweetify.should eq str
294
+ 'foo#bar'.tweetify.should eq 'foo#bar'
264
295
  end
265
296
 
266
297
  it 'should find URLs and wrap them in anchor tags' do
metadata CHANGED
@@ -1,20 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string_awesome
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
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-10 00:00:00.000000000 Z
11
+ date: 2013-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ! '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.0'
20
20
  - - <
@@ -24,7 +24,7 @@ dependencies:
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - '>='
27
+ - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '3.0'
30
30
  - - <
@@ -62,14 +62,14 @@ dependencies:
62
62
  name: rspec
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
- - - '>='
65
+ - - ! '>='
66
66
  - !ruby/object:Gem::Version
67
67
  version: '0'
68
68
  type: :development
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
- - - '>='
72
+ - - ! '>='
73
73
  - !ruby/object:Gem::Version
74
74
  version: '0'
75
75
  - !ruby/object:Gem::Dependency
@@ -90,14 +90,14 @@ dependencies:
90
90
  name: rake
91
91
  requirement: !ruby/object:Gem::Requirement
92
92
  requirements:
93
- - - '>='
93
+ - - ! '>='
94
94
  - !ruby/object:Gem::Version
95
95
  version: '0'
96
96
  type: :development
97
97
  prerelease: false
98
98
  version_requirements: !ruby/object:Gem::Requirement
99
99
  requirements:
100
- - - '>='
100
+ - - ! '>='
101
101
  - !ruby/object:Gem::Version
102
102
  version: '0'
103
103
  description: Awesome and easy-to-use extensions to Ruby String class.
@@ -130,17 +130,17 @@ require_paths:
130
130
  - lib
131
131
  required_ruby_version: !ruby/object:Gem::Requirement
132
132
  requirements:
133
- - - '>='
133
+ - - ! '>='
134
134
  - !ruby/object:Gem::Version
135
135
  version: '0'
136
136
  required_rubygems_version: !ruby/object:Gem::Requirement
137
137
  requirements:
138
- - - '>='
138
+ - - ! '>='
139
139
  - !ruby/object:Gem::Version
140
140
  version: '0'
141
141
  requirements: []
142
142
  rubyforge_project:
143
- rubygems_version: 2.1.5
143
+ rubygems_version: 2.1.11
144
144
  signing_key:
145
145
  specification_version: 4
146
146
  summary: Extensions for Ruby String class