rtoken 0.0.1
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.
- data/lib/rtoken.rb +83 -0
- metadata +55 -0
data/lib/rtoken.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# Generates Random Tokens for general purposes.
|
2
|
+
#
|
3
|
+
# @example Generate 8 length token
|
4
|
+
# RToken.rtoken #=> Random sequence 8 chars
|
5
|
+
#
|
6
|
+
# @example Generate 10 length token
|
7
|
+
# RToken.rtoken(:size => 10) #=> Random sequence 10 chars
|
8
|
+
#
|
9
|
+
# @example Generate 16 length token with special chars
|
10
|
+
# RToken.rtoken(:size => 16, :special_chars => '!@#$%%&')
|
11
|
+
#
|
12
|
+
# @example Generate 32 length token with uppercase chars
|
13
|
+
# RToken.rtoken(:size => 32, :uppercase => true)
|
14
|
+
#
|
15
|
+
# @see RToken.rtoken
|
16
|
+
class RToken
|
17
|
+
|
18
|
+
UPPER_ALPHA_CHARS = ('A'..'Z').to_a.freeze
|
19
|
+
LOWER_ALPHA_CHARS = ('a'..'z').to_a.freeze
|
20
|
+
|
21
|
+
NUMERIC_CHARS = ('0'..'9').to_a.freeze
|
22
|
+
|
23
|
+
DEAFULT_OPTIONS = {
|
24
|
+
:size => 8,
|
25
|
+
:uppercase => false,
|
26
|
+
:lowercase => false,
|
27
|
+
:numeric => true,
|
28
|
+
:special_cars => ''
|
29
|
+
}.freeze
|
30
|
+
|
31
|
+
# Creates an instance of RToken and handle the options for later calls
|
32
|
+
# It is useful when you have many settings and don't want to repeat them
|
33
|
+
#
|
34
|
+
# @example Generates tokens with special chars
|
35
|
+
# rtkn = RToken.new(:special_chars => '+-*/')
|
36
|
+
# # All next calls keeps the same options
|
37
|
+
# rtkn.rtoken #=> Same as Rtoken.rtoken(:special_chars => '+-*/')
|
38
|
+
#
|
39
|
+
# @param [Hash] opts same as rtoken
|
40
|
+
def initialize(opts=nil)
|
41
|
+
@options = opts || {}
|
42
|
+
end
|
43
|
+
|
44
|
+
# Generates a token with previews options
|
45
|
+
#
|
46
|
+
# @see RToken.rtoken
|
47
|
+
#
|
48
|
+
# @param [Hash] opts options that will be merged to the predefined on initialize
|
49
|
+
# @return [String] token
|
50
|
+
def rtoken(opts=nil)
|
51
|
+
RToken.rtoken @options.merge(opts || {})
|
52
|
+
end
|
53
|
+
|
54
|
+
# Generates a token
|
55
|
+
#
|
56
|
+
# @param [Hash] opts The options to configure the token generator
|
57
|
+
# @param opts [Fixnum] :size The size of token string. Default 8
|
58
|
+
# @param opts :uppercase if true all chars will be replaced by uppercase. Default false
|
59
|
+
# @param opts :lowercase if true all chars will be replaced by lowercase. Has priority against :uppercase definition. Default false
|
60
|
+
# @param opts :numeric if true include numeric chars ('0'..'9') on tokens. Default true
|
61
|
+
# @param opts [String] :special_chars special chars to be include on token generation, by default include alphanumeric chars
|
62
|
+
# @return [String] token
|
63
|
+
def self.rtoken(opts=nil)
|
64
|
+
options = DEAFULT_OPTIONS.merge(opts || {})
|
65
|
+
size = options[:size] || 8
|
66
|
+
# Merge available chars
|
67
|
+
chars_array = options[:numeric] ? Array.new(NUMERIC_CHARS) : []
|
68
|
+
if options[:uppercase] || options[:lowercase]
|
69
|
+
chars_array += (options[:lowercase] ? LOWER_ALPHA_CHARS : UPPER_ALPHA_CHARS)
|
70
|
+
else
|
71
|
+
chars_array += LOWER_ALPHA_CHARS
|
72
|
+
chars_array += UPPER_ALPHA_CHARS
|
73
|
+
end
|
74
|
+
chars_array += options[:special_chars].split('') if options[:special_chars]
|
75
|
+
chars_array_size = chars_array.size
|
76
|
+
# creates token
|
77
|
+
token_chars = Array.new(size)
|
78
|
+
size.times do |i|
|
79
|
+
token_chars[i] = chars_array[rand(chars_array_size)]
|
80
|
+
end
|
81
|
+
token_chars.join
|
82
|
+
end
|
83
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rtoken
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Thiago Lewin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-03-02 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: "RToken generates random tokens, of any length, for multiple purposes, ex: web urls, data identification, etc."
|
17
|
+
email: thiago_lewin@yahoo.com.br
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/rtoken.rb
|
26
|
+
homepage: https://github.com/tlewin/rtoken
|
27
|
+
licenses: []
|
28
|
+
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
requirements: []
|
47
|
+
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 1.8.11
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: Simple Random Token Generator
|
53
|
+
test_files: []
|
54
|
+
|
55
|
+
has_rdoc:
|