tzispa_utils 0.2.4 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 77cdf8877650d45bb79b04666e53592548ac3f73
4
- data.tar.gz: eaddb0eb6ec243f2c12e457800c52da144908fb7
3
+ metadata.gz: b55ae95092dbd645f059f641d88e38d989d054e3
4
+ data.tar.gz: 9616fc67b77bb2cd386f715d6b443c73422ac5ec
5
5
  SHA512:
6
- metadata.gz: 8707b3881ded8b189a52f08099044243245ab45a4a5bd8692a5ca4d1abe6ba98b433675ec42a5be7eefd13c5223015e7379ea4de510b77a113229ae0eef99972
7
- data.tar.gz: 1096ba2208612c0ae5d8605bf7d4f19cd949ab609872f71f7b077289bf80763d557623f38b1b55add77e44d414aaa81c95bcca43a0ea29e07c5bc72f95c39d75
6
+ metadata.gz: c3400b388e83f3996bc67e135ca1e80fa7c95a7962835a56c6cb1e1ac782433e6f2f5fb8ebdffcc864ac07ce342d6fd958024483eaef44cb9a3229197de5a614
7
+ data.tar.gz: 7b56e7502e060edf19741caf5e54244475714845341c2c2b9dad2036e1c957f8560adc08b080e941c24352a42d91eef04110a191a4716fdcc9daf75a0f99999a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  Tzispa Utils
2
2
 
3
+ ## v0.3.0
4
+ - remove String module keeping only String refinement
5
+ - added integer refinement
6
+ - added escape and unescape html methods to string refinement
7
+ - added sanitize_html to string refinement
8
+ - more testing implemented
9
+
3
10
  ## v0.2.4
4
11
  - Remove obsolete / unused code
5
12
  - Optimization and bug fixes
data/lib/tzispa/utils.rb CHANGED
@@ -5,6 +5,7 @@ module Tzispa
5
5
  require 'tzispa/utils/decorator'
6
6
  require 'tzispa/utils/indenter'
7
7
  require 'tzispa/utils/string'
8
+ require 'tzispa/utils/integer'
8
9
 
9
10
  end
10
11
  end
@@ -0,0 +1,30 @@
1
+ require 'i18n'
2
+ require 'sanitize'
3
+ require 'escape_utils'
4
+ require 'i18n'
5
+
6
+ module Tzispa
7
+ module Utils
8
+
9
+ refine Integer do
10
+
11
+ def to_filesize(precission=1)
12
+ fsz = {
13
+ I18n.t('number.human.storage_units.byte.other', default: 'Bytes') => 1024,
14
+ I18n.t('number.human.storage_units.kb', default: 'KB') => 1024 * 1024,
15
+ I18n.t('number.human.storage_units.mb', default: 'MB') => 1024 * 1024 * 1024,
16
+ I18n.t('number.human.storage_units.gb', default: 'GB') => 1024 * 1024 * 1024 * 1024,
17
+ I18n.t('number.human.storage_units.tb', default: 'TB') => 1024 * 1024 * 1024 * 1024 * 1024
18
+ }.select { |k, v| self < v }.first
19
+ ns = (self.to_f / (fsz[1] / 1024)).round(precission).to_s.split('.')
20
+ n = ns[1].to_i == 0 ? ns[0] : ns.join( I18n.t('number.format.separator', default: '.') )
21
+ u = fsz[0]
22
+ fmt = I18n.t('number.human.storage_units.format', default: '%n %u')
23
+ fmt.sub('%n', n).sub('%u', u)
24
+ end
25
+
26
+ end
27
+
28
+
29
+ end
30
+ end
@@ -1,15 +1,18 @@
1
1
  require 'i18n'
2
+ require 'sanitize'
3
+ require 'escape_utils'
2
4
 
3
5
  module Tzispa
4
6
  module Utils
5
7
 
6
- refine String do
8
+ NAMESPACE_SEPARATOR = '::'
9
+ CLASSIFY_SEPARATOR = '_'
10
+ UNDERSCORE_SEPARATOR = '/'
11
+ DOT_SEPARATOR = '.'
12
+ UNDERSCORE_DIVISION_TARGET = '\1_\2'
13
+
7
14
 
8
- NAMESPACE_SEPARATOR = '::'
9
- CLASSIFY_SEPARATOR = '_'
10
- UNDERSCORE_SEPARATOR = '/'
11
- DOT_SEPARATOR = '.'
12
- UNDERSCORE_DIVISION_TARGET = '\1_\2'
15
+ refine String do
13
16
 
14
17
  def constantize
15
18
  names = self.split(NAMESPACE_SEPARATOR)
@@ -113,7 +116,21 @@ module Tzispa
113
116
  split(word_splitter).take_while { |s| (ml += s.length + 1) <= max }.join(word_splitter)
114
117
  end
115
118
 
119
+ def sanitize_html(level = nil)
120
+ level ? Sanitize.fragment(self, level) :
121
+ Sanitize.fragment(self)
122
+ end
123
+ alias sanitize_htm sanitize_html
116
124
 
125
+ def escape_html
126
+ EscapeUtils.escape_html(self)
127
+ end
128
+ alias_method :escape_htm, :escape_html
129
+
130
+ def unescape_html
131
+ EscapeUtils.unescape_html(self)
132
+ end
133
+ alias_method :unescape_htm, :unescape_html
117
134
 
118
135
  end
119
136
 
@@ -144,6 +161,21 @@ module Tzispa
144
161
  String.new(str).indentize count, char
145
162
  end
146
163
 
164
+ def sanitize_html(str, level = nil)
165
+ String.new(str).santize(level)
166
+ end
167
+ alias_method :sanitize_htm, :sanitize_html
168
+
169
+ def escape_html(str)
170
+ String.new(str).escape_html
171
+ end
172
+ alias_method :escape_htm, :escape_html
173
+
174
+ def unescape_html(str)
175
+ String.new(str).unescape_html
176
+ end
177
+ alias_method :unescape_htm, :unescape_html
178
+
147
179
  end
148
180
 
149
181
 
@@ -3,7 +3,7 @@
3
3
  module Tzispa
4
4
  module Utils
5
5
 
6
- VERSION = '0.2.4'
6
+ VERSION = '0.3.0'
7
7
  NAME = 'Tzispa Utils'
8
8
  GEM_NAME = 'tzispa_utils'
9
9
 
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+
3
+ class IntegerTest < Minitest::Test
4
+ using Tzispa::Utils
5
+
6
+ def setup
7
+ I18n.enforce_available_locales = false
8
+ end
9
+
10
+ def test_that_it_has_a_version_number
11
+ refute_nil ::Tzispa::Utils::VERSION
12
+ end
13
+
14
+ def test_filesize
15
+ assert_equal 8192.to_filesize, '8 KB'
16
+ assert_equal 8192000.to_filesize, '7.8 MB'
17
+ assert_equal 8388608.to_filesize, '8 MB'
18
+ assert_equal 8589934592.to_filesize, '8 GB'
19
+ assert_equal 0x80000000000.to_filesize, '8 TB'
20
+ assert_equal 0x85000000000.to_filesize, '8.3 TB'
21
+ end
22
+
23
+ def test_localized_filesize
24
+ locales = I18n.load_path += Dir["test/res/locales/*.yml"]
25
+ refute_nil locales
26
+ I18n.default_locale = :es
27
+ assert_equal 8192.to_filesize, '8 KB'
28
+ assert_equal 8192000.to_filesize, '7,8 MB'
29
+ assert_equal 8388608.to_filesize, '8 MB'
30
+ assert_equal 8589934592.to_filesize, '8 GB'
31
+ assert_equal 0x80000000000.to_filesize, '8 TB'
32
+ assert_equal 0x85000000000.to_filesize, '8,3 TB'
33
+ end
34
+
35
+
36
+ end
@@ -26,3 +26,119 @@ es:
26
26
  Ü: U
27
27
  ç: c
28
28
  Ç: c
29
+ number:
30
+ currency:
31
+ format:
32
+ delimiter: "."
33
+ format: "%n %u"
34
+ precision: 2
35
+ separator: ","
36
+ significant: false
37
+ strip_insignificant_zeros: false
38
+ unit: "€"
39
+ format:
40
+ delimiter: "."
41
+ precision: 2
42
+ separator: ","
43
+ significant: false
44
+ strip_insignificant_zeros: false
45
+ human:
46
+ decimal_units:
47
+ format: "%n %u"
48
+ units:
49
+ billion: mil millones
50
+ million:
51
+ one: millón
52
+ other: millones
53
+ quadrillion: mil billones
54
+ thousand: mil
55
+ trillion:
56
+ one: billón
57
+ other: billones
58
+ unit: ''
59
+ format:
60
+ delimiter: ''
61
+ precision: 1
62
+ significant: true
63
+ strip_insignificant_zeros: true
64
+ storage_units:
65
+ format: "%n %u"
66
+ units:
67
+ byte:
68
+ one: Byte
69
+ other: Bytes
70
+ gb: GB
71
+ kb: KB
72
+ mb: MB
73
+ tb: TB
74
+ percentage:
75
+ format:
76
+ delimiter: ''
77
+ format: "%n %"
78
+ precision:
79
+ format:
80
+ delimiter: ''
81
+ time:
82
+ am: a.m.
83
+ formats:
84
+ default: '%a, %d %b %Y %H:%M:%S %z'
85
+ long: ! '%B %d, %Y %H:%M'
86
+ short: ! '%d %b %H:%M'
87
+ pm: p.m.
88
+ datetime:
89
+ formats:
90
+ default: '%d/%m/%Y %H:%M:%S'
91
+ date:
92
+ abbr_day_names:
93
+ - Dom
94
+ - Lun
95
+ - Mar
96
+ - Mie
97
+ - Jue
98
+ - Vie
99
+ - Sab
100
+ abbr_month_names:
101
+ - Ene
102
+ - Feb
103
+ - Mar
104
+ - Abr
105
+ - May
106
+ - Jun
107
+ - Jul
108
+ - Ago
109
+ - Sep
110
+ - Oct
111
+ - Nov
112
+ - Dic
113
+ day_names:
114
+ - Domingo
115
+ - Lunes
116
+ - Martes
117
+ - Miércoles
118
+ - Jueves
119
+ - Viernes
120
+ - Sábado
121
+ formats:
122
+ birth: ! '%d.%m.'
123
+ default: ! '%d/%m/%Y'
124
+ long: ! '%B %d, %Y'
125
+ month_and_year: ! '%b %Y'
126
+ short: ! '%b %d'
127
+ month_names:
128
+ - Enero
129
+ - Febrero
130
+ - Marzo
131
+ - Abril
132
+ - Mayo
133
+ - Junio
134
+ - Julio
135
+ - Agosto
136
+ - Septiembre
137
+ - Octubre
138
+ - Noviembre
139
+ - Diciembre
140
+ order:
141
+ - day
142
+ - month
143
+ - year
144
+
data/test/string_test.rb CHANGED
@@ -11,31 +11,31 @@ class StringTest < Minitest::Test
11
11
  end
12
12
 
13
13
  def test_camelize
14
- assert "my_test".camelize == "MyTest" &&
15
- "mytest".camelize == "Mytest" &&
16
- "MY_TEST".camelize == "MyTest"
14
+ assert_equal "my_test".camelize, "MyTest"
15
+ assert_equal "mytest".camelize, "Mytest"
16
+ assert_equal "MY_TEST".camelize, "MyTest"
17
17
  end
18
18
 
19
19
  def test_constantize
20
- assert "Tzispa::Utils::Decorator".constantize == Tzispa::Utils::Decorator &&
21
- "Hash".constantize == Hash
20
+ assert_equal "Tzispa::Utils::Decorator".constantize, Tzispa::Utils::Decorator
21
+ assert_equal "Hash".constantize, Hash
22
22
  end
23
23
 
24
24
  def test_dottize
25
- assert "Tzispa::Utils::SimpleStringParser".dottize == "tzispa.utils.simple_string_parser" &&
26
- "SimpleStringParser".dottize == "simple_string_parser"
25
+ assert_equal "Tzispa::Utils::SimpleStringParser".dottize, "tzispa.utils.simple_string_parser"
26
+ assert_equal "SimpleStringParser".dottize, "simple_string_parser"
27
27
  end
28
28
 
29
29
  def test_underscore
30
- assert "SimpleStringParser".underscore == "simple_string_parser" &&
31
- "Tzispa::Utils::SimpleStringParser".underscore == "tzispa/utils/simple_string_parser"
30
+ assert_equal "SimpleStringParser".underscore, "simple_string_parser"
31
+ assert_equal "Tzispa::Utils::SimpleStringParser".underscore, "tzispa/utils/simple_string_parser"
32
32
  end
33
33
 
34
34
  def test_transliterate
35
35
  refute_nil @@locales
36
36
  refute_empty @@locales
37
- assert "áéíóúÁÉÍÓÚñÑ".transliterate(:es) == "aeiouAEIOUnN" &&
38
- "aeiouAEIOUnN".transliterate(:es) == "aeiouAEIOUnN"
37
+ assert_equal "áéíóúÁÉÍÓÚñÑ".transliterate(:es), "aeiouAEIOUnN"
38
+ assert_equal "aeiouAEIOUnN".transliterate(:es), "aeiouAEIOUnN"
39
39
  end
40
40
 
41
41
  def test_urlize
@@ -46,10 +46,23 @@ class StringTest < Minitest::Test
46
46
 
47
47
  def test_length_constraint_wordify
48
48
  str_t = "en un lugar de la mancha de cuyo nombre no quiero acordarme"
49
- assert str_t.length_constraint_wordify(25) == "en un lugar de la mancha" &&
50
- str_t.length_constraint_wordify(64) == str_t &&
51
- str_t.length_constraint_wordify(0) == ""
49
+ assert_equal str_t.length_constraint_wordify(25), "en un lugar de la mancha"
50
+ assert_equal str_t.length_constraint_wordify(64), str_t
51
+ assert_equal str_t.length_constraint_wordify(0), ""
52
52
  end
53
53
 
54
+ def test_escape_html
55
+ str_t = "esto<es un>texto\" & inseguro' para html"
56
+ assert_equal str_t.escape_html, "esto&lt;es un&gt;texto&quot; &amp; inseguro&#39; para html"
57
+ assert_equal str_t.escape_html.unescape_html, str_t
58
+ end
59
+
60
+ def test_sanitize
61
+ str_t = '<b><a href="http://foo.com/">foo</a></b><img src="bar.jpg">'
62
+ assert_equal str_t.sanitize_html, 'foo'
63
+ assert_equal str_t.sanitize_html(Sanitize::Config::RESTRICTED), '<b>foo</b>'
64
+ assert_equal str_t.sanitize_html(Sanitize::Config::BASIC), '<b><a href="http://foo.com/" rel="nofollow">foo</a></b>'
65
+ assert_equal str_t.sanitize_html(Sanitize::Config::RELAXED), '<b><a href="http://foo.com/">foo</a></b><img src="bar.jpg">'
66
+ end
54
67
 
55
68
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tzispa_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Antonio Piñero
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-16 00:00:00.000000000 Z
11
+ date: 2017-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -16,14 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.7'
19
+ version: '0.8'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.7'
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sanitize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: escape_utils
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.2'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: minitest
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -52,11 +80,13 @@ files:
52
80
  - lib/tzispa/utils.rb
53
81
  - lib/tzispa/utils/decorator.rb
54
82
  - lib/tzispa/utils/indenter.rb
83
+ - lib/tzispa/utils/integer.rb
55
84
  - lib/tzispa/utils/string.rb
56
85
  - lib/tzispa/utils/version.rb
57
86
  - lib/tzispa_utils.rb
58
87
  - test/decorator_test.rb
59
88
  - test/indenter_test.rb
89
+ - test/integer_test.rb
60
90
  - test/res/locales/es.yml
61
91
  - test/string_test.rb
62
92
  - test/test_helper.rb
@@ -80,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
110
  version: '0'
81
111
  requirements: []
82
112
  rubyforge_project:
83
- rubygems_version: 2.5.2
113
+ rubygems_version: 2.6.10
84
114
  signing_key:
85
115
  specification_version: 4
86
116
  summary: Utilities for Tzispa