str_sanitizer 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +9 -0
- data/CHANGELOG.md +6 -1
- data/README.md +21 -5
- data/lib/str_sanitizer/quotes.rb +85 -11
- data/lib/str_sanitizer/version.rb +1 -1
- data/lib/str_sanitizer.rb +23 -1
- 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: 40adee975fa727815aab7c0c5342b29aa314141d
|
|
4
|
+
data.tar.gz: 61bf3734eb9e0498d0340c336c906279b29accc6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3ef826fb0f755c512fd636b6ba678041191517af6c6ba62f716ef17fe248022a8368ec38bf0339d06c055151930eebec476670adfd7161ccc590cbddc06f5832
|
|
7
|
+
data.tar.gz: 07efaf0dac8247d325828b0711dd4f13e8d9310a42b4818a6470e4536ee8b6e1efb286cbdf7a715d68dd69e00cd8e61bdb8b2ddfdfc6142c90abeb9ed85dabc0
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
+
## StrSanitizer 0.2.0 (August 09, 2017) ##
|
|
2
|
+
|
|
3
|
+
* Changed the structure of the module in `lib/str_sanitizer/quotes.rb` file. Removed the `InstanceMethods` module and `included` class method of `Quotes` module. Remove the `ClassMethods` class too from `Quotes` module. Re-structured the whole module.
|
|
4
|
+
* Use `extend` instead of `include` in `StrSanitizer` class for using other modules methods. (To use the instance methods of any modules as class methods)
|
|
5
|
+
* Add documentation about the methods in `Quotes` module.
|
|
6
|
+
|
|
1
7
|
## StrSanitizer 0.1.1 (August 07, 2017) ##
|
|
2
8
|
|
|
3
9
|
* In v0.1.0, the single_quote method wasn't working properly. This release fixes it.
|
|
4
|
-
*
|
data/README.md
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
[](https://badge.fury.io/rb/str_sanitizer)
|
|
1
2
|
# StrSanitizer
|
|
2
3
|
|
|
3
4
|
Welcome to this gem. This gem is about String Sanitization. This gem sanitizes the string which is given.
|
|
@@ -21,15 +22,30 @@ Or install it yourself as:
|
|
|
21
22
|
|
|
22
23
|
## Usage
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
- To escape quotes of a string
|
|
25
26
|
```ruby
|
|
26
27
|
require "str_sanitizer"
|
|
27
28
|
|
|
28
|
-
hello =
|
|
29
|
-
StrSanitizer.double_quote
|
|
29
|
+
hello = 'He said, "Hello!"'
|
|
30
|
+
StrSanitizer.double_quote(hello) # => He said, \"Hello!\"
|
|
30
31
|
|
|
31
|
-
hello =
|
|
32
|
-
StrSanitizer.single_quote
|
|
32
|
+
hello = "She said, 'Hello!'"
|
|
33
|
+
StrSanitizer.single_quote(hello) # => She said, \'Hello!\'
|
|
34
|
+
|
|
35
|
+
both_quotes = "They said, \"Don't do it!\""
|
|
36
|
+
StrSanitizer.both_quotes(both_quotes) # => They said, \"Don\'t do it!\"
|
|
37
|
+
```
|
|
38
|
+
You can also check if the string has any quote or not
|
|
39
|
+
```ruby
|
|
40
|
+
no_quote = "Hello, there."
|
|
41
|
+
single_quote = "It's going down."
|
|
42
|
+
double_quote = "He said, \"Hello\""
|
|
43
|
+
|
|
44
|
+
StrSanitizer.has_any_quote?(no_quote) # => nil
|
|
45
|
+
StrSanitizer.has_both_quotes?(no_quote) # => nil
|
|
46
|
+
|
|
47
|
+
StrSanitizer.has_single_quotes?(single_quote) # => true
|
|
48
|
+
StrSanitizer.has_double_quotes?(double_quote) # => true
|
|
33
49
|
```
|
|
34
50
|
|
|
35
51
|
## Development
|
data/lib/str_sanitizer/quotes.rb
CHANGED
|
@@ -1,21 +1,95 @@
|
|
|
1
|
+
|
|
1
2
|
class StrSanitizer
|
|
3
|
+
|
|
4
|
+
# This module sanitizes the given string by adding backslash (\) before quote
|
|
5
|
+
# It also can check if a string has any quote or not.
|
|
6
|
+
#
|
|
7
|
+
# Author: Jakaria (mailto: jakariablaine120@gmail.com)
|
|
8
|
+
# Copyright: Copyright (c) 2017 Jakaria
|
|
9
|
+
|
|
2
10
|
module Quotes
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
11
|
+
|
|
12
|
+
# Find any double quote (") in a string and add backslash (\) before the double quote.
|
|
13
|
+
#
|
|
14
|
+
# Params:
|
|
15
|
+
# +str+:: A +string+ which has double quote that needs to be escaped.
|
|
16
|
+
#
|
|
17
|
+
# Returns:
|
|
18
|
+
# +String+:: The string which was passed to the parameter with escaped double quotes.
|
|
19
|
+
def double_quote(str)
|
|
20
|
+
str.gsub(/"/, /\\"/.source)
|
|
6
21
|
end
|
|
7
22
|
|
|
8
|
-
|
|
23
|
+
# Find any single quote (') in a string and add backslash (\) before the single quote.
|
|
24
|
+
#
|
|
25
|
+
# Params:
|
|
26
|
+
# +str+:: A +string+ which has single quote that needs to be escaped.
|
|
27
|
+
#
|
|
28
|
+
# Returns:
|
|
29
|
+
# +String+:: The string which was passed to the parameter with escaped single quotes.
|
|
30
|
+
def single_quote(str)
|
|
31
|
+
str.gsub(/'/, /\\'/.source)
|
|
9
32
|
end
|
|
10
33
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
34
|
+
# Find any single quote (') and double quote (") in a string and add backslash (\)
|
|
35
|
+
# before the single quote and double quote (").
|
|
36
|
+
#
|
|
37
|
+
# Params:
|
|
38
|
+
# +str+:: A +string+ which has single quote and double quote that need to be escaped.
|
|
39
|
+
#
|
|
40
|
+
# Returns:
|
|
41
|
+
# +String+:: The string which was passed to the parameter with escaped single quote and
|
|
42
|
+
# double quote.
|
|
43
|
+
def both_quotes(str)
|
|
44
|
+
single_quote(double_quote(str))
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Search through the given string to see if it has any single quote (').
|
|
48
|
+
#
|
|
49
|
+
# Params:
|
|
50
|
+
# +str+:: A +string+ which needs to be checked for single quote.
|
|
51
|
+
# +pos+:: From where it should start searching. +Default+ is 0.
|
|
52
|
+
#
|
|
53
|
+
# Returns:
|
|
54
|
+
# +Boolean|Nil+:: Returns true if it has any single quote, nil for no single quote
|
|
55
|
+
def has_single_quote?(str, pos = 0)
|
|
56
|
+
true if str.match("'", pos)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Search through the given string to see if it has any double quote (").
|
|
60
|
+
#
|
|
61
|
+
# Params:
|
|
62
|
+
# +str+:: A +string+ which needs to be checked for double quote.
|
|
63
|
+
# +pos+:: From where it should start searching. +Default+ is 0.
|
|
64
|
+
#
|
|
65
|
+
# Returns:
|
|
66
|
+
# +Boolean|Nil+:: Returns true if it has any double quote, nil for no double quote
|
|
67
|
+
def has_double_quote?(str, pos = 0)
|
|
68
|
+
true if str.match('"', pos)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Search through the given string to see if it has both, single (') and double quote (").
|
|
72
|
+
#
|
|
73
|
+
# Params:
|
|
74
|
+
# +str+:: A +string+ which needs to be checked for both quotes.
|
|
75
|
+
# +pos+:: From where it should start searching. +Default+ is 0.
|
|
76
|
+
#
|
|
77
|
+
# Returns:
|
|
78
|
+
# +Boolean|Nil+:: Returns true if it has both quotes, nil for no quote.
|
|
79
|
+
def has_both_quotes?(str, pos = 0)
|
|
80
|
+
true if has_single_quote?(str, pos) && has_double_quote?(str, pos)
|
|
81
|
+
end
|
|
15
82
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
83
|
+
# Search through the given string to see if it has any quote.
|
|
84
|
+
#
|
|
85
|
+
# Params:
|
|
86
|
+
# +str+:: A +string+ which needs to be checked for any quote.
|
|
87
|
+
# +pos+:: From where it should start searching. +Default+ is 0.
|
|
88
|
+
#
|
|
89
|
+
# Returns:
|
|
90
|
+
# +Boolean|Nil+:: Returns true if it has any quote, nil for no quote
|
|
91
|
+
def has_any_quote?(str, pos = 0)
|
|
92
|
+
true if has_single_quote?(str, pos) || has_double_quote?(str, pos)
|
|
19
93
|
end
|
|
20
94
|
end
|
|
21
95
|
end
|
data/lib/str_sanitizer.rb
CHANGED
|
@@ -1,6 +1,28 @@
|
|
|
1
|
+
# The MIT License (MIT)
|
|
2
|
+
#
|
|
3
|
+
# Copyright (c) 2017 Jakaria Blaine
|
|
4
|
+
#
|
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
# furnished to do so, subject to the following conditions:
|
|
11
|
+
#
|
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
|
13
|
+
# all copies or substantial portions of the Software.
|
|
14
|
+
#
|
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
# THE SOFTWARE.
|
|
22
|
+
|
|
1
23
|
require "str_sanitizer/version"
|
|
2
24
|
require "str_sanitizer/quotes"
|
|
3
25
|
|
|
4
26
|
class StrSanitizer
|
|
5
|
-
|
|
27
|
+
extend Quotes
|
|
6
28
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: str_sanitizer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jakaria Blaine
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2017-08-
|
|
11
|
+
date: 2017-08-09 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|