string-utils 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +1 -0
- data/Manifest +5 -0
- data/README.rdoc +69 -0
- data/Rakefile +14 -0
- data/lib/string-utils.rb +38 -0
- data/string-utils.gemspec +30 -0
- metadata +80 -0
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0 - First commit
|
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
string-utils
|
2
|
+
==========
|
3
|
+
|
4
|
+
This small extension enables string sanitization in models and controller.
|
5
|
+
|
6
|
+
Contains a workaround for Rails sanitize helper that (as any helper) works only in views, allowing you to call my_text.sanitize also in controllers, models and libs.
|
7
|
+
|
8
|
+
Provides also a function for making url friendly strings removing all unwanted characters.
|
9
|
+
|
10
|
+
Basically it adds some methods and shortcuts to the String class, see the example section for usage.
|
11
|
+
|
12
|
+
Works with Rails ~> 2.3.6 and ~> 3.0.0 beta
|
13
|
+
|
14
|
+
Install
|
15
|
+
=======
|
16
|
+
|
17
|
+
Specify it in your Rails project config/environment.rb under the "Rails::Initializer.run do |config|" block
|
18
|
+
|
19
|
+
config.gem 'string-utils', :source => "http://gemcutter.org"
|
20
|
+
|
21
|
+
Then install it from the console.
|
22
|
+
|
23
|
+
rake gems:install
|
24
|
+
|
25
|
+
|
26
|
+
Or install manually with:
|
27
|
+
|
28
|
+
sudo gem install string-utils --source http://gemcutter.org
|
29
|
+
|
30
|
+
|
31
|
+
Examples
|
32
|
+
=======
|
33
|
+
|
34
|
+
>> my_unsafe_text.sanitize
|
35
|
+
Applies the sanitize helper to text.
|
36
|
+
Works not only in views but also in models, controllers and libs!
|
37
|
+
|
38
|
+
>> my_unsafe_text.san
|
39
|
+
Shortcut for my_unsafe_text.sanitize
|
40
|
+
|
41
|
+
>> "Fire in the hole: <script>alert('bam!');</script>".san
|
42
|
+
=> "Fire in the hole: "
|
43
|
+
|
44
|
+
>> "Some tags: <asdasd>an asdasd tag</adsasd>".san
|
45
|
+
=> "Some tags: an asdasd tag"
|
46
|
+
|
47
|
+
>> my_SAFE_text.hs
|
48
|
+
Shortcut for my_SAFE_text.html_safe
|
49
|
+
USE WITH CAUTION
|
50
|
+
Forces the string to be html safe so it won't be escaped.
|
51
|
+
Useful for quick-and-dirty monkey scripting in views.
|
52
|
+
|
53
|
+
>> message = "I really need this <script>alert('popup')</script>".hs
|
54
|
+
=> "I really need this <script>alert('popup')</script>"
|
55
|
+
>> message.html_safe?
|
56
|
+
=> true
|
57
|
+
|
58
|
+
|
59
|
+
"My really great post".urlify
|
60
|
+
=> "my-really-great-post"
|
61
|
+
|
62
|
+
"O come l'è GANZA 'sta gemma!".urlify
|
63
|
+
=> "o-come-l-e-ganza-sta-gemma"
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
Copyright (c) 2010 Silvio Relli, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('string-utils', '0.1.0') do |p|
|
6
|
+
p.description = "This small extension enables string sanitization in models and controller. Provides also a function for making url friendly strings removing all unwanted characters."
|
7
|
+
p.url = "http://github.com/silviorelli/string-utils"
|
8
|
+
p.author = "Silvio Relli"
|
9
|
+
p.email = "silvio@relli.org"
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.development_dependencies = []
|
12
|
+
end
|
13
|
+
|
14
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/lib/string-utils.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Silvio Relli's string_utils gem - released under the MIT License
|
2
|
+
class String
|
3
|
+
|
4
|
+
# Workaround for sanitize method not availabe in controllers, models and libs.
|
5
|
+
# Now you can call "mystryng".sanitize everywere.
|
6
|
+
def sanitize
|
7
|
+
ActionController::Base.helpers.sanitize(self)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Ensures the string is sanitized and html safe,
|
11
|
+
# so it won't be escaped by xss protection and erubis in rails >=2.3.6 and >=3.0.
|
12
|
+
def san
|
13
|
+
self.sanitize
|
14
|
+
end
|
15
|
+
|
16
|
+
# Just a shortcut to force a string to be html safe.
|
17
|
+
# Use with caution.
|
18
|
+
def hs
|
19
|
+
self.html_safe
|
20
|
+
end
|
21
|
+
|
22
|
+
# Makes strings be url-friendly
|
23
|
+
def urlify
|
24
|
+
str = self.downcase.gsub(/ /,'-')
|
25
|
+
str.gsub!(/À|à|á|Â|â|Ã|ã|Ä|ä/i,'a')
|
26
|
+
str.gsub!(/È|è|É|é|Ê|ê|Ë|ë/i,'e')
|
27
|
+
str.gsub!(/Ì|ì|í|Î|î/i,'i')
|
28
|
+
str.gsub!(/Ò|ò|ó|Ô|ô|Õ|õ|Ö|ö/i,'o')
|
29
|
+
str.gsub!(/Ù|ù|ú|Û|û|Ü|ü/i,'u')
|
30
|
+
str.gsub!(/ç/i,'c')
|
31
|
+
str.gsub!(/œ/i,'oe')
|
32
|
+
str.gsub!(/ß/i,'ss')
|
33
|
+
str.gsub!(/[^a-z0-9\-]+/i, '')
|
34
|
+
str
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{string-utils}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Silvio Relli"]
|
9
|
+
s.date = %q{2010-06-08}
|
10
|
+
s.description = %q{This small extension enables string sanitization in models and controller. Provides also a function for making url friendly strings removing all unwanted characters.}
|
11
|
+
s.email = %q{silvio@relli.org}
|
12
|
+
s.extra_rdoc_files = ["CHANGELOG.rdoc", "README.rdoc", "lib/string-utils.rb"]
|
13
|
+
s.files = ["CHANGELOG.rdoc", "README.rdoc", "Rakefile", "lib/string-utils.rb", "Manifest", "string-utils.gemspec"]
|
14
|
+
s.homepage = %q{http://github.com/silviorelli/string-utils}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "String-utils", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{string-utils}
|
18
|
+
s.rubygems_version = %q{1.3.7}
|
19
|
+
s.summary = %q{This small extension enables string sanitization in models and controller. Provides also a function for making url friendly strings removing all unwanted characters.}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: string-utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Silvio Relli
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-08 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: This small extension enables string sanitization in models and controller. Provides also a function for making url friendly strings removing all unwanted characters.
|
23
|
+
email: silvio@relli.org
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- CHANGELOG.rdoc
|
30
|
+
- README.rdoc
|
31
|
+
- lib/string-utils.rb
|
32
|
+
files:
|
33
|
+
- CHANGELOG.rdoc
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- lib/string-utils.rb
|
37
|
+
- Manifest
|
38
|
+
- string-utils.gemspec
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/silviorelli/string-utils
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --line-numbers
|
46
|
+
- --inline-source
|
47
|
+
- --title
|
48
|
+
- String-utils
|
49
|
+
- --main
|
50
|
+
- README.rdoc
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 11
|
68
|
+
segments:
|
69
|
+
- 1
|
70
|
+
- 2
|
71
|
+
version: "1.2"
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project: string-utils
|
75
|
+
rubygems_version: 1.3.7
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: This small extension enables string sanitization in models and controller. Provides also a function for making url friendly strings removing all unwanted characters.
|
79
|
+
test_files: []
|
80
|
+
|