utility 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +37 -0
- data/README.md +30 -31
- data/lib/utility/atob.rb +104 -0
- data/lib/utility/version.rb +1 -1
- data/lib/utility.rb +3 -2
- data/utility.gemspec +3 -3
- metadata +6 -6
- data/CODE_OF_CONDUCT.md +0 -74
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7146d6516db0a0b290e4570b5e9d5d97623ff10e
|
4
|
+
data.tar.gz: d847d69e41dbce10df764aba51e92e22d3104d35
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6be1be7251b66c04f4edde21136d0c98dde378ffef63f4e7293d6f0df639dbc785208e6c45c7f003b58dd38c3ce83d38504d0903e7acc768600da8381bc68a1e
|
7
|
+
data.tar.gz: f64610245f2f557b4a73882959cd51db7003021e9abdd964771788ed3e536b967b60613cc00e5d56caad5b7c31c4cdb77079b825ae67b89c579f45ee5900ed1a
|
data/.gitignore
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# Default
|
1
2
|
/.bundle/
|
2
3
|
/.yardoc
|
3
4
|
/Gemfile.lock
|
@@ -10,3 +11,39 @@
|
|
10
11
|
|
11
12
|
# rspec failure tracking
|
12
13
|
.rspec_status
|
14
|
+
|
15
|
+
## MAC OS
|
16
|
+
.DS_Store
|
17
|
+
|
18
|
+
## TEXTMATE
|
19
|
+
*.tmproj
|
20
|
+
tmtags
|
21
|
+
|
22
|
+
## EMACS
|
23
|
+
*~
|
24
|
+
\#*
|
25
|
+
.\#*
|
26
|
+
|
27
|
+
## VIM
|
28
|
+
*.swp
|
29
|
+
|
30
|
+
## RUBYMINE
|
31
|
+
.idea
|
32
|
+
|
33
|
+
## PROJECT::GENERAL
|
34
|
+
coverage
|
35
|
+
rdoc
|
36
|
+
pkg
|
37
|
+
.ruby-version
|
38
|
+
gemfiles/*.gemfile.lock
|
39
|
+
Gemfile.lock
|
40
|
+
/tmp
|
41
|
+
/.bundle
|
42
|
+
/gemfiles/.bundle
|
43
|
+
|
44
|
+
## PROJECT::RVM
|
45
|
+
.rvmrc
|
46
|
+
|
47
|
+
# PROJECT::RBENV
|
48
|
+
.ruby-gemset
|
49
|
+
*.gem
|
data/README.md
CHANGED
@@ -1,41 +1,40 @@
|
|
1
|
-
|
1
|
+
## Utility ##
|
2
2
|
|
3
|
-
|
3
|
+
The useful utilities which made in ruby ways.
|
4
4
|
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
6
|
-
|
7
|
-
## Installation
|
8
|
-
|
9
|
-
Add this line to your application's Gemfile:
|
10
|
-
|
11
|
-
```ruby
|
12
|
-
gem 'utility'
|
13
|
-
```
|
14
|
-
|
15
|
-
And then execute:
|
16
|
-
|
17
|
-
$ bundle
|
18
|
-
|
19
|
-
Or install it yourself as:
|
20
5
|
|
6
|
+
### Installation ###
|
7
|
+
# Installing as Ruby gem
|
21
8
|
$ gem install utility
|
22
9
|
|
23
|
-
|
24
|
-
|
25
|
-
TODO: Write usage instructions here
|
10
|
+
# Or in gemfile
|
11
|
+
$ gem utility
|
26
12
|
|
27
|
-
|
28
|
-
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
-
|
31
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
-
|
33
|
-
## Contributing
|
34
|
-
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/utility. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
13
|
+
### Usage ###
|
14
|
+
You will be able use utility in `rails c` or `irb`.
|
36
15
|
|
16
|
+
```ruby
|
17
|
+
require "utility"
|
18
|
+
```
|
37
19
|
|
38
|
-
|
20
|
+
#### base64 ####
|
21
|
+
```ruby
|
22
|
+
# base64 encode
|
23
|
+
Utility.base64encode "ruby" #=> cnVieQ==
|
24
|
+
Utility.base64encode "utility" #=> dXRpbGl0eQ==
|
25
|
+
Utility.base64encode "中文" #=> 5Lit5paH
|
26
|
+
|
27
|
+
# base64 decode
|
28
|
+
Utility.base64decode "cnVieQ==" #=> ruby
|
29
|
+
Utility.base64decode "dXRpbGl0eQ==" #=> utility
|
30
|
+
Utility.base64decode "5Lit5paH" #=> 中文
|
31
|
+
```
|
39
32
|
|
40
|
-
|
33
|
+
### TODO ###
|
34
|
+
- Add md5
|
35
|
+
- Add sha1
|
36
|
+
- Add sha256
|
37
|
+
- Add random numbers/chars
|
41
38
|
|
39
|
+
### License ###
|
40
|
+
Released under the [MIT](http://opensource.org/licenses/MIT) license. See LICENSE file for details.
|
data/lib/utility/atob.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
module Utility::Atob
|
2
|
+
|
3
|
+
# Base64 convert in ruby way
|
4
|
+
# Usually, we use Base64 module to handle everything of base64 with `require 'base64'`
|
5
|
+
# But now, here is the pure ruby code to do the same thing :D
|
6
|
+
# refer: http://www.webtoolkitonline.com/base64-converter.html
|
7
|
+
|
8
|
+
CHARS = [*'A'..'Z', *'a'..'z', *'0'..'9', '+', '/', '=']
|
9
|
+
|
10
|
+
# Convert a string to Base64
|
11
|
+
# Example:
|
12
|
+
# base64encode("ruby") #=> cnVieQ==
|
13
|
+
# base64encode("utility") #=> dXRpbGl0eQ==
|
14
|
+
# base64encode("中文") #=> 5Lit5paH
|
15
|
+
def base64encode(input)
|
16
|
+
output = ""
|
17
|
+
input = utf8_base64encode input
|
18
|
+
(0...input.size).step(3) do |i|
|
19
|
+
chr1, chr2, chr3 = input[i..i+2].each_char.map(&:ord) + [0]*2
|
20
|
+
|
21
|
+
enc1 = chr1 >> 2
|
22
|
+
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)
|
23
|
+
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
|
24
|
+
enc4 = chr3 & 63
|
25
|
+
|
26
|
+
if chr2.zero?
|
27
|
+
enc3 = enc4 = 64
|
28
|
+
elsif chr3.zero?
|
29
|
+
enc4 = 64
|
30
|
+
end
|
31
|
+
output << CHARS.values_at(enc1, enc2, enc3, enc4).join
|
32
|
+
end
|
33
|
+
|
34
|
+
output
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
# Convert Base64 to a string
|
39
|
+
# Example:
|
40
|
+
# base64decode("cnVieQ==") #=> ruby
|
41
|
+
# base64decode("dXRpbGl0eQ==") #=> utility
|
42
|
+
# base64decode("5Lit5paH") #=> 中文
|
43
|
+
def base64decode(input)
|
44
|
+
output = ""
|
45
|
+
input = input.gsub(/[^A-Za-z0-9\+\/\=]/, "")
|
46
|
+
(0...input.size).step(4) do |i|
|
47
|
+
enc1, enc2, enc3, enc4 = input[i..i+3].each_char.map{|c| CHARS.index c} + [0]*3
|
48
|
+
|
49
|
+
chr1 = (enc1 << 2) | (enc2 >> 4)
|
50
|
+
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
|
51
|
+
chr3 = ((enc3 & 3) << 6) | enc4
|
52
|
+
|
53
|
+
output << chr1.chr
|
54
|
+
output << chr2.chr if enc3 != 64
|
55
|
+
output << chr3.chr if enc4 != 64
|
56
|
+
end
|
57
|
+
|
58
|
+
utf8_base64decode output
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def utf8_base64encode(string)
|
64
|
+
string = string.to_s.gsub(/\r\n/, "\n")
|
65
|
+
utftext = ''
|
66
|
+
string.size.times do |i|
|
67
|
+
c = string[i].ord
|
68
|
+
if c < 128
|
69
|
+
utftext << c.chr('UTF-8')
|
70
|
+
elsif c > 127 && c < 2048
|
71
|
+
utftext << ((c >> 6) | 192).chr('UTF-8')
|
72
|
+
utftext << ((c & 63) | 128).chr('UTF-8')
|
73
|
+
else
|
74
|
+
utftext << ((c >> 12) | 224).chr('UTF-8')
|
75
|
+
utftext << (((c >> 6) & 63) | 128).chr('UTF-8')
|
76
|
+
utftext << ((c & 63) | 128).chr('UTF-8')
|
77
|
+
end
|
78
|
+
end
|
79
|
+
utftext
|
80
|
+
end
|
81
|
+
|
82
|
+
def utf8_base64decode(utftext)
|
83
|
+
string = ""
|
84
|
+
i = 0
|
85
|
+
while i < utftext.size do
|
86
|
+
c = utftext[i].ord
|
87
|
+
if (c < 128)
|
88
|
+
string << c.chr
|
89
|
+
i += 1
|
90
|
+
elsif ((c > 191) && (c < 224))
|
91
|
+
c2 = utftext[i+1].ord
|
92
|
+
string << (((c & 31) << 6) | (c2 & 63)).chr
|
93
|
+
i += 2
|
94
|
+
else
|
95
|
+
c2 = utftext[i+1].ord
|
96
|
+
c3 = utftext[i+2].ord
|
97
|
+
string << (((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)).chr
|
98
|
+
i += 3
|
99
|
+
end
|
100
|
+
end
|
101
|
+
string
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
data/lib/utility/version.rb
CHANGED
data/lib/utility.rb
CHANGED
data/utility.gemspec
CHANGED
@@ -9,9 +9,9 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Cenxky"]
|
10
10
|
spec.email = ["cenxky@gmail.com"]
|
11
11
|
|
12
|
-
spec.summary = %q{
|
13
|
-
spec.description = %q{
|
14
|
-
spec.homepage = ""
|
12
|
+
spec.summary = %q{The useful utilities which made in ruby ways.}
|
13
|
+
spec.description = %q{The useful utilities which made in ruby ways.}
|
14
|
+
spec.homepage = "https://github.com/cenxky/utility"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
17
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: utility
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cenxky
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-07-
|
11
|
+
date: 2017-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
-
description:
|
55
|
+
description: The useful utilities which made in ruby ways.
|
56
56
|
email:
|
57
57
|
- cenxky@gmail.com
|
58
58
|
executables: []
|
@@ -62,7 +62,6 @@ files:
|
|
62
62
|
- ".gitignore"
|
63
63
|
- ".rspec"
|
64
64
|
- ".travis.yml"
|
65
|
-
- CODE_OF_CONDUCT.md
|
66
65
|
- Gemfile
|
67
66
|
- LICENSE.txt
|
68
67
|
- README.md
|
@@ -70,9 +69,10 @@ files:
|
|
70
69
|
- bin/console
|
71
70
|
- bin/setup
|
72
71
|
- lib/utility.rb
|
72
|
+
- lib/utility/atob.rb
|
73
73
|
- lib/utility/version.rb
|
74
74
|
- utility.gemspec
|
75
|
-
homepage:
|
75
|
+
homepage: https://github.com/cenxky/utility
|
76
76
|
licenses:
|
77
77
|
- MIT
|
78
78
|
metadata: {}
|
@@ -95,5 +95,5 @@ rubyforge_project:
|
|
95
95
|
rubygems_version: 2.6.6
|
96
96
|
signing_key:
|
97
97
|
specification_version: 4
|
98
|
-
summary:
|
98
|
+
summary: The useful utilities which made in ruby ways.
|
99
99
|
test_files: []
|
data/CODE_OF_CONDUCT.md
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
# Contributor Covenant Code of Conduct
|
2
|
-
|
3
|
-
## Our Pledge
|
4
|
-
|
5
|
-
In the interest of fostering an open and welcoming environment, we as
|
6
|
-
contributors and maintainers pledge to making participation in our project and
|
7
|
-
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
-
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
-
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
-
orientation.
|
11
|
-
|
12
|
-
## Our Standards
|
13
|
-
|
14
|
-
Examples of behavior that contributes to creating a positive environment
|
15
|
-
include:
|
16
|
-
|
17
|
-
* Using welcoming and inclusive language
|
18
|
-
* Being respectful of differing viewpoints and experiences
|
19
|
-
* Gracefully accepting constructive criticism
|
20
|
-
* Focusing on what is best for the community
|
21
|
-
* Showing empathy towards other community members
|
22
|
-
|
23
|
-
Examples of unacceptable behavior by participants include:
|
24
|
-
|
25
|
-
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
-
advances
|
27
|
-
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
-
* Public or private harassment
|
29
|
-
* Publishing others' private information, such as a physical or electronic
|
30
|
-
address, without explicit permission
|
31
|
-
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
-
professional setting
|
33
|
-
|
34
|
-
## Our Responsibilities
|
35
|
-
|
36
|
-
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
-
behavior and are expected to take appropriate and fair corrective action in
|
38
|
-
response to any instances of unacceptable behavior.
|
39
|
-
|
40
|
-
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
-
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
-
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
-
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
-
threatening, offensive, or harmful.
|
45
|
-
|
46
|
-
## Scope
|
47
|
-
|
48
|
-
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
-
when an individual is representing the project or its community. Examples of
|
50
|
-
representing a project or community include using an official project e-mail
|
51
|
-
address, posting via an official social media account, or acting as an appointed
|
52
|
-
representative at an online or offline event. Representation of a project may be
|
53
|
-
further defined and clarified by project maintainers.
|
54
|
-
|
55
|
-
## Enforcement
|
56
|
-
|
57
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
-
reported by contacting the project team at dfzy5566@qq.com. All
|
59
|
-
complaints will be reviewed and investigated and will result in a response that
|
60
|
-
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
-
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
-
Further details of specific enforcement policies may be posted separately.
|
63
|
-
|
64
|
-
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
-
faith may face temporary or permanent repercussions as determined by other
|
66
|
-
members of the project's leadership.
|
67
|
-
|
68
|
-
## Attribution
|
69
|
-
|
70
|
-
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
-
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
-
|
73
|
-
[homepage]: http://contributor-covenant.org
|
74
|
-
[version]: http://contributor-covenant.org/version/1/4/
|