blackbox 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitattributes +2 -0
- data/Guardfile +16 -0
- data/Makefile +24 -0
- data/blackbox.gemspec +4 -0
- data/coverage/.last_run.json +1 -1
- data/coverage/.resultset.json +171 -2
- data/coverage/index.html +1064 -34
- data/doc/BB.html +5 -5
- data/doc/BB/Crypto.html +21 -21
- data/doc/BB/Hash.html +4 -4
- data/doc/BB/String.html +2 -2
- data/doc/_index.html +17 -2
- data/doc/class_list.html +1 -1
- data/doc/file.README.html +2 -2
- data/doc/index.html +2 -2
- data/doc/method_list.html +24 -0
- data/doc/top-level-namespace.html +2 -2
- data/lib/blackbox/crypto.rb +24 -24
- data/lib/blackbox/number.rb +167 -0
- data/lib/blackbox/version.rb +1 -1
- data/spec/blackbox_spec.rb +1 -1
- data/spec/crypto_spec.rb +4 -4
- data/spec/hash_spec.rb +4 -4
- data/spec/number_spec.rb +93 -0
- data/spec/string_spec.rb +1 -1
- metadata +65 -3
data/doc/BB.html
CHANGED
@@ -80,7 +80,7 @@
|
|
80
80
|
|
81
81
|
<dt class="r1 last">Defined in:</dt>
|
82
82
|
<dd class="r1 last">lib/blackbox/hash.rb<span class="defines">,<br />
|
83
|
-
lib/blackbox/string.rb,<br /> lib/blackbox/crypto.rb,<br /> lib/blackbox/version.rb</span>
|
83
|
+
lib/blackbox/string.rb,<br /> lib/blackbox/number.rb,<br /> lib/blackbox/crypto.rb,<br /> lib/blackbox/version.rb</span>
|
84
84
|
</dd>
|
85
85
|
|
86
86
|
</dl>
|
@@ -90,7 +90,7 @@
|
|
90
90
|
<p class="children">
|
91
91
|
|
92
92
|
|
93
|
-
<strong class="modules">Modules:</strong> <span class='object_link'><a href="BB/Crypto.html" title="BB::Crypto (module)">Crypto</a></span>, <span class='object_link'><a href="BB/Hash.html" title="BB::Hash (module)">Hash</a></span>, <span class='object_link'><a href="BB/String.html" title="BB::String (module)">String</a></span>
|
93
|
+
<strong class="modules">Modules:</strong> <span class='object_link'><a href="BB/Crypto.html" title="BB::Crypto (module)">Crypto</a></span>, <span class='object_link'><a href="BB/Hash.html" title="BB::Hash (module)">Hash</a></span>, <span class='object_link'><a href="BB/Number.html" title="BB::Number (module)">Number</a></span>, <span class='object_link'><a href="BB/String.html" title="BB::String (module)">String</a></span>
|
94
94
|
|
95
95
|
|
96
96
|
|
@@ -104,7 +104,7 @@
|
|
104
104
|
<dt id="VERSION-constant" class="">VERSION =
|
105
105
|
|
106
106
|
</dt>
|
107
|
-
<dd><pre class="code"><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>1.
|
107
|
+
<dd><pre class="code"><span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>1.1.0</span><span class='tstring_end'>"</span></span></pre></dd>
|
108
108
|
|
109
109
|
</dl>
|
110
110
|
|
@@ -120,9 +120,9 @@
|
|
120
120
|
</div>
|
121
121
|
|
122
122
|
<div id="footer">
|
123
|
-
Generated on
|
123
|
+
Generated on Sun Nov 22 18:25:23 2015 by
|
124
124
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
125
|
-
0.8.5.2 (ruby-2.
|
125
|
+
0.8.5.2 (ruby-2.2.2).
|
126
126
|
</div>
|
127
127
|
|
128
128
|
</body>
|
data/doc/BB/Crypto.html
CHANGED
@@ -397,19 +397,19 @@
|
|
397
397
|
<td>
|
398
398
|
<pre class="code"><span class="info file"># File 'lib/blackbox/crypto.rb', line 36</span>
|
399
399
|
|
400
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_decrypt'>decrypt</span><span class='lparen'>(</span><span class='id identifier rubyid_ciphertext'>ciphertext</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='op'>=</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>aes-256-cbc</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='op'>=</span><span class='kw'>nil</span><span class='rparen'>)</span>
|
401
|
-
<span class='id identifier
|
402
|
-
<span class='id identifier
|
403
|
-
<span class='id identifier
|
400
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_decrypt'>decrypt</span><span class='lparen'>(</span><span class='id identifier rubyid_ciphertext'>ciphertext</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='op'>=</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>aes-256-cbc</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='op'>=</span><span class='kw'>nil</span><span class='rparen'>)</span>
|
401
|
+
<span class='id identifier rubyid_cipher'>cipher</span> <span class='op'>=</span> <span class='const'>OpenSSL</span><span class='op'>::</span><span class='const'>Cipher</span><span class='op'>::</span><span class='const'>Cipher</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='rparen'>)</span>
|
402
|
+
<span class='id identifier rubyid_cipher'>cipher</span><span class='period'>.</span><span class='id identifier rubyid_decrypt'>decrypt</span>
|
403
|
+
<span class='id identifier rubyid_cipher'>cipher</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span> <span class='op'>=</span> <span class='id identifier rubyid_key'>key</span>
|
404
404
|
<span class='kw'>if</span> <span class='id identifier rubyid_iv'>iv</span><span class='period'>.</span><span class='id identifier rubyid_nil?'>nil?</span>
|
405
405
|
<span class='id identifier rubyid_iv_len'>iv_len</span> <span class='op'>=</span> <span class='id identifier rubyid_ciphertext'>ciphertext</span><span class='period'>.</span><span class='id identifier rubyid_slice!'>slice!</span><span class='lparen'>(</span><span class='int'>0</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_unpack'>unpack</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>C</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span><span class='lbracket'>[</span><span class='int'>0</span><span class='rbracket'>]</span>
|
406
406
|
<span class='kw'>unless</span> <span class='int'>0</span> <span class='op'>==</span> <span class='id identifier rubyid_iv_len'>iv_len</span>
|
407
|
-
<span class='id identifier
|
407
|
+
<span class='id identifier rubyid_cipher'>cipher</span><span class='period'>.</span><span class='id identifier rubyid_iv'>iv</span> <span class='op'>=</span> <span class='id identifier rubyid_ciphertext'>ciphertext</span><span class='period'>.</span><span class='id identifier rubyid_slice!'>slice!</span><span class='lparen'>(</span><span class='int'>0</span><span class='op'>..</span><span class='id identifier rubyid_iv_len'>iv_len</span><span class='op'>-</span><span class='int'>1</span><span class='rparen'>)</span>
|
408
408
|
<span class='kw'>end</span>
|
409
409
|
<span class='kw'>else</span>
|
410
|
-
<span class='id identifier
|
410
|
+
<span class='id identifier rubyid_cipher'>cipher</span><span class='period'>.</span><span class='id identifier rubyid_iv'>iv</span> <span class='op'>=</span> <span class='id identifier rubyid_iv'>iv</span>
|
411
411
|
<span class='kw'>end</span>
|
412
|
-
<span class='id identifier
|
412
|
+
<span class='id identifier rubyid_cipher'>cipher</span><span class='period'>.</span><span class='id identifier rubyid_update'>update</span><span class='lparen'>(</span><span class='id identifier rubyid_ciphertext'>ciphertext</span><span class='rparen'>)</span> <span class='op'>+</span> <span class='id identifier rubyid_cipher'>cipher</span><span class='period'>.</span><span class='id identifier rubyid_final'>final</span>
|
413
413
|
<span class='kw'>end</span></pre>
|
414
414
|
</td>
|
415
415
|
</tr>
|
@@ -539,7 +539,7 @@
|
|
539
539
|
<td>
|
540
540
|
<pre class="code"><span class="info file"># File 'lib/blackbox/crypto.rb', line 70</span>
|
541
541
|
|
542
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_decrypt_base64'>decrypt_base64</span><span class='lparen'>(</span><span class='id identifier rubyid_ciphertext'>ciphertext</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='op'>=</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>aes-256-cbc</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='op'>=</span><span class='kw'>nil</span><span class='rparen'>)</span>
|
542
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_decrypt_base64'>decrypt_base64</span><span class='lparen'>(</span><span class='id identifier rubyid_ciphertext'>ciphertext</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='op'>=</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>aes-256-cbc</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='op'>=</span><span class='kw'>nil</span><span class='rparen'>)</span>
|
543
543
|
<span class='id identifier rubyid_decrypt'>decrypt</span><span class='lparen'>(</span><span class='const'>Base64</span><span class='period'>.</span><span class='id identifier rubyid_decode64'>decode64</span><span class='lparen'>(</span><span class='id identifier rubyid_ciphertext'>ciphertext</span><span class='rparen'>)</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='rparen'>)</span>
|
544
544
|
<span class='kw'>end</span></pre>
|
545
545
|
</td>
|
@@ -670,7 +670,7 @@
|
|
670
670
|
<td>
|
671
671
|
<pre class="code"><span class="info file"># File 'lib/blackbox/crypto.rb', line 93</span>
|
672
672
|
|
673
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_decrypt_urlsafe_base64'>decrypt_urlsafe_base64</span><span class='lparen'>(</span><span class='id identifier rubyid_ciphertext'>ciphertext</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='op'>=</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>aes-256-cbc</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='op'>=</span><span class='kw'>nil</span><span class='rparen'>)</span>
|
673
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_decrypt_urlsafe_base64'>decrypt_urlsafe_base64</span><span class='lparen'>(</span><span class='id identifier rubyid_ciphertext'>ciphertext</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='op'>=</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>aes-256-cbc</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='op'>=</span><span class='kw'>nil</span><span class='rparen'>)</span>
|
674
674
|
<span class='id identifier rubyid_decrypt'>decrypt</span><span class='lparen'>(</span><span class='const'>Base64</span><span class='period'>.</span><span class='id identifier rubyid_urlsafe_decode64'>urlsafe_decode64</span><span class='lparen'>(</span><span class='id identifier rubyid_ciphertext'>ciphertext</span><span class='rparen'>)</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='rparen'>)</span>
|
675
675
|
<span class='kw'>end</span></pre>
|
676
676
|
</td>
|
@@ -824,16 +824,16 @@
|
|
824
824
|
<td>
|
825
825
|
<pre class="code"><span class="info file"># File 'lib/blackbox/crypto.rb', line 16</span>
|
826
826
|
|
827
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_encrypt'>encrypt</span><span class='lparen'>(</span><span class='id identifier rubyid_plaintext'>plaintext</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='op'>=</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>aes-256-cbc</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='op'>=</span><span class='kw'>nil</span><span class='rparen'>)</span>
|
828
|
-
<span class='id identifier
|
829
|
-
<span class='id identifier
|
830
|
-
<span class='id identifier
|
827
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_encrypt'>encrypt</span><span class='lparen'>(</span><span class='id identifier rubyid_plaintext'>plaintext</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='op'>=</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>aes-256-cbc</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='op'>=</span><span class='kw'>nil</span><span class='rparen'>)</span>
|
828
|
+
<span class='id identifier rubyid_cipher'>cipher</span> <span class='op'>=</span> <span class='const'>OpenSSL</span><span class='op'>::</span><span class='const'>Cipher</span><span class='op'>::</span><span class='const'>Cipher</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='rparen'>)</span>
|
829
|
+
<span class='id identifier rubyid_cipher'>cipher</span><span class='period'>.</span><span class='id identifier rubyid_encrypt'>encrypt</span>
|
830
|
+
<span class='id identifier rubyid_cipher'>cipher</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span> <span class='op'>=</span> <span class='id identifier rubyid_key'>key</span>
|
831
831
|
<span class='kw'>if</span> <span class='id identifier rubyid_iv'>iv</span><span class='period'>.</span><span class='id identifier rubyid_nil?'>nil?</span>
|
832
|
-
<span class='id identifier rubyid_iv'>iv</span> <span class='op'>=</span> <span class='id identifier
|
833
|
-
<span class='lbracket'>[</span><span class='id identifier rubyid_iv'>iv</span><span class='period'>.</span><span class='id identifier rubyid_length'>length</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_pack'>pack</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>C</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span> <span class='op'>+</span> <span class='id identifier rubyid_iv'>iv</span> <span class='op'>+</span> <span class='id identifier
|
832
|
+
<span class='id identifier rubyid_iv'>iv</span> <span class='op'>=</span> <span class='id identifier rubyid_cipher'>cipher</span><span class='period'>.</span><span class='id identifier rubyid_random_iv'>random_iv</span>
|
833
|
+
<span class='lbracket'>[</span><span class='id identifier rubyid_iv'>iv</span><span class='period'>.</span><span class='id identifier rubyid_length'>length</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_pack'>pack</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>C</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span> <span class='op'>+</span> <span class='id identifier rubyid_iv'>iv</span> <span class='op'>+</span> <span class='id identifier rubyid_cipher'>cipher</span><span class='period'>.</span><span class='id identifier rubyid_update'>update</span><span class='lparen'>(</span><span class='id identifier rubyid_plaintext'>plaintext</span><span class='rparen'>)</span> <span class='op'>+</span> <span class='id identifier rubyid_cipher'>cipher</span><span class='period'>.</span><span class='id identifier rubyid_final'>final</span>
|
834
834
|
<span class='kw'>else</span>
|
835
|
-
<span class='id identifier
|
836
|
-
<span class='id identifier
|
835
|
+
<span class='id identifier rubyid_cipher'>cipher</span><span class='period'>.</span><span class='id identifier rubyid_iv'>iv</span> <span class='op'>=</span> <span class='id identifier rubyid_iv'>iv</span>
|
836
|
+
<span class='id identifier rubyid_cipher'>cipher</span><span class='period'>.</span><span class='id identifier rubyid_update'>update</span><span class='lparen'>(</span><span class='id identifier rubyid_plaintext'>plaintext</span><span class='rparen'>)</span> <span class='op'>+</span> <span class='id identifier rubyid_cipher'>cipher</span><span class='period'>.</span><span class='id identifier rubyid_final'>final</span>
|
837
837
|
<span class='kw'>end</span>
|
838
838
|
<span class='kw'>end</span></pre>
|
839
839
|
</td>
|
@@ -978,7 +978,7 @@
|
|
978
978
|
<td>
|
979
979
|
<pre class="code"><span class="info file"># File 'lib/blackbox/crypto.rb', line 59</span>
|
980
980
|
|
981
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_encrypt_base64'>encrypt_base64</span><span class='lparen'>(</span><span class='id identifier rubyid_plaintext'>plaintext</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='op'>=</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>aes-256-cbc</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='op'>=</span><span class='kw'>nil</span><span class='rparen'>)</span>
|
981
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_encrypt_base64'>encrypt_base64</span><span class='lparen'>(</span><span class='id identifier rubyid_plaintext'>plaintext</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='op'>=</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>aes-256-cbc</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='op'>=</span><span class='kw'>nil</span><span class='rparen'>)</span>
|
982
982
|
<span class='const'>Base64</span><span class='period'>.</span><span class='id identifier rubyid_strict_encode64'>strict_encode64</span><span class='lparen'>(</span><span class='id identifier rubyid_encrypt'>encrypt</span><span class='lparen'>(</span><span class='id identifier rubyid_plaintext'>plaintext</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='rparen'>)</span><span class='rparen'>)</span>
|
983
983
|
<span class='kw'>end</span></pre>
|
984
984
|
</td>
|
@@ -1123,7 +1123,7 @@
|
|
1123
1123
|
<td>
|
1124
1124
|
<pre class="code"><span class="info file"># File 'lib/blackbox/crypto.rb', line 82</span>
|
1125
1125
|
|
1126
|
-
<span class='kw'>def</span> <span class='id identifier rubyid_encrypt_urlsafe_base64'>encrypt_urlsafe_base64</span><span class='lparen'>(</span><span class='id identifier rubyid_plaintext'>plaintext</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='op'>=</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>aes-256-cbc</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='op'>=</span><span class='kw'>nil</span><span class='rparen'>)</span>
|
1126
|
+
<span class='kw'>def</span> <span class='id identifier rubyid_encrypt_urlsafe_base64'>encrypt_urlsafe_base64</span><span class='lparen'>(</span><span class='id identifier rubyid_plaintext'>plaintext</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='op'>=</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>aes-256-cbc</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='op'>=</span><span class='kw'>nil</span><span class='rparen'>)</span>
|
1127
1127
|
<span class='const'>Base64</span><span class='period'>.</span><span class='id identifier rubyid_urlsafe_encode64'>urlsafe_encode64</span><span class='lparen'>(</span><span class='id identifier rubyid_encrypt'>encrypt</span><span class='lparen'>(</span><span class='id identifier rubyid_plaintext'>plaintext</span><span class='comma'>,</span> <span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_cipher_type'>cipher_type</span><span class='comma'>,</span> <span class='id identifier rubyid_iv'>iv</span><span class='rparen'>)</span><span class='rparen'>)</span>
|
1128
1128
|
<span class='kw'>end</span></pre>
|
1129
1129
|
</td>
|
@@ -1136,9 +1136,9 @@
|
|
1136
1136
|
</div>
|
1137
1137
|
|
1138
1138
|
<div id="footer">
|
1139
|
-
Generated on
|
1139
|
+
Generated on Sun Nov 22 18:25:23 2015 by
|
1140
1140
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
1141
|
-
0.8.5.2 (ruby-2.
|
1141
|
+
0.8.5.2 (ruby-2.2.2).
|
1142
1142
|
</div>
|
1143
1143
|
|
1144
1144
|
</body>
|
data/doc/BB/Hash.html
CHANGED
@@ -331,10 +331,10 @@ BB::Hash.flatten_prop_style(input)
|
|
331
331
|
|
332
332
|
<span class='kw'>def</span> <span class='id identifier rubyid_flatten_prop_style'>flatten_prop_style</span><span class='lparen'>(</span><span class='id identifier rubyid_input'>input</span><span class='op'>=</span><span class='lbrace'>{</span><span class='rbrace'>}</span><span class='comma'>,</span> <span class='id identifier rubyid_opts'>opts</span><span class='op'>=</span><span class='lbrace'>{</span><span class='rbrace'>}</span><span class='comma'>,</span> <span class='id identifier rubyid_output'>output</span><span class='op'>=</span><span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
|
333
333
|
<span class='id identifier rubyid_input'>input</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='op'>|</span>
|
334
|
-
<span class='id identifier rubyid_key'>key</span> <span class='op'>=</span> <span class='id identifier rubyid_opts'>opts</span><span class='lbracket'>[</span><span class='symbol'>:prefix</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_nil?'>nil?</span> <span class='op'>?</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_key'>key</span><span class='embexpr_end'>}</span><span class='tstring_end'>"</span></span> <span class='op'>:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_opts'>opts</span><span class='lbracket'>[</span><span class='symbol'>:prefix</span><span class='rbracket'>]</span><span class='embexpr_end'>}</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_opts'>opts</span><span class='lbracket'>[</span><span class='symbol'>:delimiter</span><span class='rbracket'>]</span><span class='op'>||</span><span class='tstring'><span class='tstring_beg'
|
334
|
+
<span class='id identifier rubyid_key'>key</span> <span class='op'>=</span> <span class='id identifier rubyid_opts'>opts</span><span class='lbracket'>[</span><span class='symbol'>:prefix</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_nil?'>nil?</span> <span class='op'>?</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_key'>key</span><span class='embexpr_end'>}</span><span class='tstring_end'>"</span></span> <span class='op'>:</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_opts'>opts</span><span class='lbracket'>[</span><span class='symbol'>:prefix</span><span class='rbracket'>]</span><span class='embexpr_end'>}</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_opts'>opts</span><span class='lbracket'>[</span><span class='symbol'>:delimiter</span><span class='rbracket'>]</span><span class='op'>||</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>.</span><span class='tstring_end'>'</span></span><span class='embexpr_end'>}</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_key'>key</span><span class='embexpr_end'>}</span><span class='tstring_end'>"</span></span>
|
335
335
|
<span class='kw'>case</span> <span class='id identifier rubyid_value'>value</span>
|
336
336
|
<span class='kw'>when</span> <span class='op'>::</span><span class='const'>Hash</span>
|
337
|
-
<span class='id identifier rubyid_flatten_prop_style'>flatten_prop_style</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='
|
337
|
+
<span class='id identifier rubyid_flatten_prop_style'>flatten_prop_style</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_opts'>opts</span><span class='period'>.</span><span class='id identifier rubyid_merge'>merge</span><span class='lparen'>(</span><span class='lbrace'>{</span><span class='symbol'>:prefix</span> <span class='op'>=></span> <span class='id identifier rubyid_key'>key</span><span class='rbrace'>}</span><span class='rparen'>)</span><span class='comma'>,</span> <span class='id identifier rubyid_output'>output</span><span class='rparen'>)</span>
|
338
338
|
<span class='kw'>when</span> <span class='const'>Array</span>
|
339
339
|
<span class='id identifier rubyid_output'>output</span><span class='lbracket'>[</span><span class='id identifier rubyid_key'>key</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_join'>join</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>,</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
|
340
340
|
<span class='kw'>else</span>
|
@@ -432,9 +432,9 @@ BB::Hash.flatten_prop_style(input)
|
|
432
432
|
</div>
|
433
433
|
|
434
434
|
<div id="footer">
|
435
|
-
Generated on
|
435
|
+
Generated on Sun Nov 22 18:25:23 2015 by
|
436
436
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
437
|
-
0.8.5.2 (ruby-2.
|
437
|
+
0.8.5.2 (ruby-2.2.2).
|
438
438
|
</div>
|
439
439
|
|
440
440
|
</body>
|
data/doc/BB/String.html
CHANGED
@@ -231,9 +231,9 @@
|
|
231
231
|
</div>
|
232
232
|
|
233
233
|
<div id="footer">
|
234
|
-
Generated on
|
234
|
+
Generated on Sun Nov 22 18:25:23 2015 by
|
235
235
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
236
|
-
0.8.5.2 (ruby-2.
|
236
|
+
0.8.5.2 (ruby-2.2.2).
|
237
237
|
</div>
|
238
238
|
|
239
239
|
</body>
|
data/doc/_index.html
CHANGED
@@ -123,6 +123,21 @@
|
|
123
123
|
</ul>
|
124
124
|
|
125
125
|
|
126
|
+
<ul id="alpha_N" class="alpha">
|
127
|
+
<li class="letter">N</li>
|
128
|
+
<ul>
|
129
|
+
|
130
|
+
<li>
|
131
|
+
<span class='object_link'><a href="BB/Number.html" title="BB::Number (module)">Number</a></span>
|
132
|
+
|
133
|
+
<small>(BB)</small>
|
134
|
+
|
135
|
+
</li>
|
136
|
+
|
137
|
+
</ul>
|
138
|
+
</ul>
|
139
|
+
|
140
|
+
|
126
141
|
<ul id="alpha_S" class="alpha">
|
127
142
|
<li class="letter">S</li>
|
128
143
|
<ul>
|
@@ -146,9 +161,9 @@
|
|
146
161
|
</div>
|
147
162
|
|
148
163
|
<div id="footer">
|
149
|
-
Generated on
|
164
|
+
Generated on Sun Nov 22 18:25:23 2015 by
|
150
165
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
151
|
-
0.8.5.2 (ruby-2.
|
166
|
+
0.8.5.2 (ruby-2.2.2).
|
152
167
|
</div>
|
153
168
|
|
154
169
|
</body>
|
data/doc/class_list.html
CHANGED
@@ -45,7 +45,7 @@
|
|
45
45
|
|
46
46
|
<ul id="full_list" class="class">
|
47
47
|
<li><span class='object_link'><a href="top-level-namespace.html" title=" (root)">Top Level Namespace</a></span></li>
|
48
|
-
<li><a class='toggle'></a> <span class='object_link'><a href="BB.html" title="BB (module)">BB</a></span><small class='search_info'>Top Level Namespace</small></li><ul><li><span class='object_link'><a href="BB/Crypto.html" title="BB::Crypto (module)">Crypto</a></span><small class='search_info'>BB</small></li><li><span class='object_link'><a href="BB/Hash.html" title="BB::Hash (module)">Hash</a></span><small class='search_info'>BB</small></li><li><span class='object_link'><a href="BB/String.html" title="BB::String (module)">String</a></span><small class='search_info'>BB</small></li></ul>
|
48
|
+
<li><a class='toggle'></a> <span class='object_link'><a href="BB.html" title="BB (module)">BB</a></span><small class='search_info'>Top Level Namespace</small></li><ul><li><span class='object_link'><a href="BB/Crypto.html" title="BB::Crypto (module)">Crypto</a></span><small class='search_info'>BB</small></li><li><span class='object_link'><a href="BB/Hash.html" title="BB::Hash (module)">Hash</a></span><small class='search_info'>BB</small></li><li><span class='object_link'><a href="BB/Number.html" title="BB::Number (module)">Number</a></span><small class='search_info'>BB</small></li><li><span class='object_link'><a href="BB/String.html" title="BB::String (module)">String</a></span><small class='search_info'>BB</small></li></ul>
|
49
49
|
|
50
50
|
</ul>
|
51
51
|
</div>
|
data/doc/file.README.html
CHANGED
@@ -95,9 +95,9 @@ BB::String.strip_ansi( ... )
|
|
95
95
|
</div></div>
|
96
96
|
|
97
97
|
<div id="footer">
|
98
|
-
Generated on
|
98
|
+
Generated on Sun Nov 22 18:25:23 2015 by
|
99
99
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
100
|
-
0.8.5.2 (ruby-2.
|
100
|
+
0.8.5.2 (ruby-2.2.2).
|
101
101
|
</div>
|
102
102
|
|
103
103
|
</body>
|
data/doc/index.html
CHANGED
@@ -95,9 +95,9 @@ BB::String.strip_ansi( ... )
|
|
95
95
|
</div></div>
|
96
96
|
|
97
97
|
<div id="footer">
|
98
|
-
Generated on
|
98
|
+
Generated on Sun Nov 22 18:25:23 2015 by
|
99
99
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
100
|
-
0.8.5.2 (ruby-2.
|
100
|
+
0.8.5.2 (ruby-2.2.2).
|
101
101
|
</div>
|
102
102
|
|
103
103
|
</body>
|
data/doc/method_list.html
CHANGED
@@ -118,6 +118,30 @@
|
|
118
118
|
</li>
|
119
119
|
|
120
120
|
|
121
|
+
<li class="r2 ">
|
122
|
+
<span class='object_link'><a href="BB/Number.html#to_human_size-class_method" title="BB::Number.to_human_size (method)">to_human_size</a></span>
|
123
|
+
|
124
|
+
<small>BB::Number</small>
|
125
|
+
|
126
|
+
</li>
|
127
|
+
|
128
|
+
|
129
|
+
<li class="r1 ">
|
130
|
+
<span class='object_link'><a href="BB/Number.html#with_delimiter-class_method" title="BB::Number.with_delimiter (method)">with_delimiter</a></span>
|
131
|
+
|
132
|
+
<small>BB::Number</small>
|
133
|
+
|
134
|
+
</li>
|
135
|
+
|
136
|
+
|
137
|
+
<li class="r2 ">
|
138
|
+
<span class='object_link'><a href="BB/Number.html#with_precision-class_method" title="BB::Number.with_precision (method)">with_precision</a></span>
|
139
|
+
|
140
|
+
<small>BB::Number</small>
|
141
|
+
|
142
|
+
</li>
|
143
|
+
|
144
|
+
|
121
145
|
</ul>
|
122
146
|
</div>
|
123
147
|
</body>
|
@@ -103,9 +103,9 @@
|
|
103
103
|
</div>
|
104
104
|
|
105
105
|
<div id="footer">
|
106
|
-
Generated on
|
106
|
+
Generated on Sun Nov 22 18:25:23 2015 by
|
107
107
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
108
|
-
0.8.5.2 (ruby-2.
|
108
|
+
0.8.5.2 (ruby-2.2.2).
|
109
109
|
</div>
|
110
110
|
|
111
111
|
</body>
|
data/lib/blackbox/crypto.rb
CHANGED
@@ -4,7 +4,7 @@ require 'base64'
|
|
4
4
|
module BB
|
5
5
|
# Crypto utilities.
|
6
6
|
module Crypto
|
7
|
-
class << self
|
7
|
+
class << self
|
8
8
|
# Encrypt a String.
|
9
9
|
#
|
10
10
|
# @param [String] plaintext Input String (plaintext)
|
@@ -13,18 +13,18 @@ module BB
|
|
13
13
|
# @param [String] iv Initialization vector
|
14
14
|
# @return [String] When iv == nil: iv_length+iv+ciphertext
|
15
15
|
# @return [String] When iv != nil: ciphertext
|
16
|
-
def encrypt(plaintext, key, cipher_type='aes-256-cbc', iv=nil)
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
def encrypt(plaintext, key, cipher_type='aes-256-cbc', iv=nil)
|
17
|
+
cipher = OpenSSL::Cipher::Cipher.new(cipher_type)
|
18
|
+
cipher.encrypt
|
19
|
+
cipher.key = key
|
20
20
|
if iv.nil?
|
21
|
-
iv =
|
22
|
-
[iv.length].pack('C') + iv +
|
21
|
+
iv = cipher.random_iv
|
22
|
+
[iv.length].pack('C') + iv + cipher.update(plaintext) + cipher.final
|
23
23
|
else
|
24
|
-
|
25
|
-
|
24
|
+
cipher.iv = iv
|
25
|
+
cipher.update(plaintext) + cipher.final
|
26
26
|
end
|
27
|
-
end
|
27
|
+
end
|
28
28
|
|
29
29
|
# Decrypt a String.
|
30
30
|
#
|
@@ -33,21 +33,21 @@ module BB
|
|
33
33
|
# @param [String] cipher_type OpenSSL cipher
|
34
34
|
# @param [String] iv Initialization vector
|
35
35
|
# @return [String] Plaintext
|
36
|
-
def decrypt(ciphertext, key, cipher_type='aes-256-cbc', iv=nil)
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
def decrypt(ciphertext, key, cipher_type='aes-256-cbc', iv=nil)
|
37
|
+
cipher = OpenSSL::Cipher::Cipher.new(cipher_type)
|
38
|
+
cipher.decrypt
|
39
|
+
cipher.key = key
|
40
40
|
if iv.nil?
|
41
41
|
iv_len = ciphertext.slice!(0).unpack('C')[0]
|
42
42
|
unless 0 == iv_len
|
43
|
-
|
43
|
+
cipher.iv = ciphertext.slice!(0..iv_len-1)
|
44
44
|
end
|
45
45
|
else
|
46
|
-
|
46
|
+
cipher.iv = iv
|
47
47
|
end
|
48
|
-
|
49
|
-
end
|
50
|
-
|
48
|
+
cipher.update(ciphertext) + cipher.final
|
49
|
+
end
|
50
|
+
|
51
51
|
# Encrypt a String and encode the resulting ciphertext to Base64.
|
52
52
|
#
|
53
53
|
# @param [String] plaintext Input String (plaintext)
|
@@ -56,7 +56,7 @@ module BB
|
|
56
56
|
# @param [String] iv Initialization vector
|
57
57
|
# @return [String] When iv == nil: base64(iv_length+iv+ciphertext)
|
58
58
|
# @return [String] When iv != nil: base64(ciphertext)
|
59
|
-
def encrypt_base64(plaintext, key, cipher_type='aes-256-cbc', iv=nil)
|
59
|
+
def encrypt_base64(plaintext, key, cipher_type='aes-256-cbc', iv=nil)
|
60
60
|
Base64.strict_encode64(encrypt(plaintext, key, cipher_type, iv))
|
61
61
|
end
|
62
62
|
|
@@ -67,7 +67,7 @@ module BB
|
|
67
67
|
# @param [String] cipher_type OpenSSL cipher
|
68
68
|
# @param [String] iv Initialization vector
|
69
69
|
# @return [String] Plaintext
|
70
|
-
def decrypt_base64(ciphertext, key, cipher_type='aes-256-cbc', iv=nil)
|
70
|
+
def decrypt_base64(ciphertext, key, cipher_type='aes-256-cbc', iv=nil)
|
71
71
|
decrypt(Base64.decode64(ciphertext), key, cipher_type, iv)
|
72
72
|
end
|
73
73
|
|
@@ -79,7 +79,7 @@ module BB
|
|
79
79
|
# @param [String] iv Initialization vector
|
80
80
|
# @return [String] When iv == nil: urlsafe_base64(iv_length+iv+ciphertext)
|
81
81
|
# @return [String] When iv != nil: urlsafe_base64(ciphertext)
|
82
|
-
def encrypt_urlsafe_base64(plaintext, key, cipher_type='aes-256-cbc', iv=nil)
|
82
|
+
def encrypt_urlsafe_base64(plaintext, key, cipher_type='aes-256-cbc', iv=nil)
|
83
83
|
Base64.urlsafe_encode64(encrypt(plaintext, key, cipher_type, iv))
|
84
84
|
end
|
85
85
|
|
@@ -90,10 +90,10 @@ module BB
|
|
90
90
|
# @param [String] cipher_type OpenSSL cipher
|
91
91
|
# @param [String] iv Initialization vector
|
92
92
|
# @return [String] Plaintext
|
93
|
-
def decrypt_urlsafe_base64(ciphertext, key, cipher_type='aes-256-cbc', iv=nil)
|
93
|
+
def decrypt_urlsafe_base64(ciphertext, key, cipher_type='aes-256-cbc', iv=nil)
|
94
94
|
decrypt(Base64.urlsafe_decode64(ciphertext), key, cipher_type, iv)
|
95
95
|
end
|
96
|
-
end
|
96
|
+
end
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
@@ -0,0 +1,167 @@
|
|
1
|
+
require 'blackbox/hash'
|
2
|
+
|
3
|
+
module BB
|
4
|
+
# String utilities.
|
5
|
+
module Number
|
6
|
+
class << self
|
7
|
+
STORAGE_UNITS = ['byte', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'].freeze
|
8
|
+
|
9
|
+
##
|
10
|
+
# Formats the bytes in +size+ into a more understandable representation
|
11
|
+
# (e.g., giving it 1500 yields 1.5 KB). This method is useful for
|
12
|
+
# reporting file sizes to users. This method returns nil if
|
13
|
+
# +size+ cannot be converted into a number. You can customize the
|
14
|
+
# format in the +options+ hash.
|
15
|
+
#
|
16
|
+
# @overload to_human_size(number, options={})
|
17
|
+
# @param [Fixnum] number
|
18
|
+
# Number value to format.
|
19
|
+
# @param [Hash] options
|
20
|
+
# Options for formatter.
|
21
|
+
# @option options [Fixnum] :precision (1)
|
22
|
+
# Sets the level of precision.
|
23
|
+
# @option options [String] :separator (".")
|
24
|
+
# Sets the separator between the units.
|
25
|
+
# @option options [String] :delimiter ("")
|
26
|
+
# Sets the thousands delimiter.
|
27
|
+
# @option options [String] :kilo (1024)
|
28
|
+
# Sets the number of bytes in a kilobyte.
|
29
|
+
# @option options [String] :format ("%n %u")
|
30
|
+
# Sets the display format.
|
31
|
+
#
|
32
|
+
# @return [String] The formatted representation of bytes
|
33
|
+
#
|
34
|
+
# @example
|
35
|
+
# to_human_size(123) # => 123 Bytes
|
36
|
+
# to_human_size(1234) # => 1.2 KB
|
37
|
+
# to_human_size(12345) # => 12.1 KB
|
38
|
+
# to_human_size(1234567) # => 1.2 MB
|
39
|
+
# to_human_size(1234567890) # => 1.1 GB
|
40
|
+
# to_human_size(1234567890123) # => 1.1 TB
|
41
|
+
# to_human_size(1234567, :precision => 2) # => 1.18 MB
|
42
|
+
# to_human_size(483989, :precision => 0) # => 473 KB
|
43
|
+
# to_human_size(1234567, :precision => 2, :separator => ',') # => 1,18 MB
|
44
|
+
#
|
45
|
+
def to_human_size(number, args={})
|
46
|
+
return nil if number.nil?
|
47
|
+
|
48
|
+
options = BB::Hash.symbolize_keys(args)
|
49
|
+
|
50
|
+
precision ||= (options[:precision] || 1)
|
51
|
+
separator ||= (options[:separator] || '.')
|
52
|
+
delimiter ||= (options[:delimiter] || '')
|
53
|
+
kilo ||= (options[:kilo] || 1024)
|
54
|
+
storage_units_format ||= (options[:format] || '%n%u')
|
55
|
+
|
56
|
+
if number.to_i < kilo
|
57
|
+
#unit = I18n.translate(:'number.human.storage_units.units.byte', :locale => options[:locale], :count => number.to_i, :raise => true)
|
58
|
+
storage_units_format.gsub(/%n/, number.to_i.to_s).gsub(/%u/, '')
|
59
|
+
else
|
60
|
+
max_exp = STORAGE_UNITS.size - 1
|
61
|
+
number = Float(number)
|
62
|
+
exponent = (Math.log(number) / Math.log(kilo)).to_i # Convert to base 1024
|
63
|
+
exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
|
64
|
+
number /= kilo ** exponent
|
65
|
+
|
66
|
+
unit = STORAGE_UNITS[exponent]
|
67
|
+
|
68
|
+
begin
|
69
|
+
escaped_separator = Regexp.escape(separator)
|
70
|
+
formatted_number = with_precision(number,
|
71
|
+
:precision => precision,
|
72
|
+
:separator => separator,
|
73
|
+
:delimiter => delimiter
|
74
|
+
).sub(/(#{escaped_separator})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '')
|
75
|
+
storage_units_format.gsub(/%n/, formatted_number).gsub(/%u/, unit)
|
76
|
+
rescue => e
|
77
|
+
e
|
78
|
+
#number
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Formats a +number+ with the specified level of <tt>:precision</tt> (e.g., 112.32 has a precision of 2).
|
85
|
+
# You can customize the format in the +options+ hash.
|
86
|
+
#
|
87
|
+
# @overload with_precision(number, options={})
|
88
|
+
# @param [Fixnum, Float] number
|
89
|
+
# Number value to format.
|
90
|
+
# @param [Hash] options
|
91
|
+
# Options for formatter.
|
92
|
+
# @option options [Fixnum] :precision (3)
|
93
|
+
# Sets the level of precision.
|
94
|
+
# @option options [String] :separator (".")
|
95
|
+
# Sets the separator between the units.
|
96
|
+
# @option options [String] :delimiter ("")
|
97
|
+
# Sets the thousands delimiter.
|
98
|
+
#
|
99
|
+
# @return [String] The formatted representation of the number.
|
100
|
+
#
|
101
|
+
# @example
|
102
|
+
# with_precision(111.2345) # => 111.235
|
103
|
+
# with_precision(111.2345, :precision => 2) # => 111.23
|
104
|
+
# with_precision(13, :precision => 5) # => 13.00000
|
105
|
+
# with_precision(389.32314, :precision => 0) # => 389
|
106
|
+
# with_precision(1111.2345, :precision => 2, :separator => ',', :delimiter => '.')
|
107
|
+
# # => 1.111,23
|
108
|
+
#
|
109
|
+
def with_precision(number, args)
|
110
|
+
options = BB::Hash.symbolize_keys(args)
|
111
|
+
|
112
|
+
precision ||= (options[:precision] || 3)
|
113
|
+
separator ||= (options[:separator] || '.')
|
114
|
+
delimiter ||= (options[:delimiter] || '')
|
115
|
+
|
116
|
+
begin
|
117
|
+
rounded_number = (Float(number) * (10 ** precision)).round.to_f / 10 ** precision
|
118
|
+
with_delimiter("%01.#{precision}f" % rounded_number,
|
119
|
+
:separator => separator,
|
120
|
+
:delimiter => delimiter)
|
121
|
+
rescue
|
122
|
+
number
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
##
|
127
|
+
# Formats a +number+ with grouped thousands using +delimiter+ (e.g., 12,324). You can
|
128
|
+
# customize the format in the +options+ hash.
|
129
|
+
#
|
130
|
+
# @overload with_delimiter(number, options={})
|
131
|
+
# @param [Fixnum, Float] number
|
132
|
+
# Number value to format.
|
133
|
+
# @param [Hash] options
|
134
|
+
# Options for formatter.
|
135
|
+
# @option options [String] :delimiter (", ")
|
136
|
+
# Sets the thousands delimiter.
|
137
|
+
# @option options [String] :separator (".")
|
138
|
+
# Sets the separator between the units.
|
139
|
+
#
|
140
|
+
# @return [String] The formatted representation of the number.
|
141
|
+
#
|
142
|
+
# @example
|
143
|
+
# with_delimiter(12345678) # => 12,345,678
|
144
|
+
# with_delimiter(12345678.05) # => 12,345,678.05
|
145
|
+
# with_delimiter(12345678, :delimiter => ".") # => 12.345.678
|
146
|
+
# with_delimiter(12345678, :separator => ",") # => 12,345,678
|
147
|
+
# with_delimiter(98765432.98, :delimiter => " ", :separator => ",")
|
148
|
+
# # => 98 765 432,98
|
149
|
+
#
|
150
|
+
def with_delimiter(number, args)
|
151
|
+
options = BB::Hash.symbolize_keys(args)
|
152
|
+
|
153
|
+
delimiter ||= (options[:delimiter] || '.')
|
154
|
+
separator ||= (options[:separator] || ',')
|
155
|
+
|
156
|
+
begin
|
157
|
+
parts = number.to_s.split('.')
|
158
|
+
parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
|
159
|
+
parts.join(separator)
|
160
|
+
rescue
|
161
|
+
number
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|