random_string 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rdebugrc +3 -0
- data/.rspec +4 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.rdoc +29 -0
- data/Rakefile +9 -0
- data/lib/random_string/main.rb +28 -0
- data/lib/random_string/sample_string/data.rb +31 -0
- data/lib/random_string/sample_string.rb +33 -0
- data/lib/random_string/version.rb +3 -0
- data/lib/random_string.rb +6 -0
- data/random_string.gemspec +23 -0
- data/spec/random_string/main_spec.rb +95 -0
- data/spec/random_string/sample_string_spec.rb +52 -0
- data/spec/spec_helper.rb +10 -0
- metadata +98 -0
data/.gitignore
ADDED
data/.rdebugrc
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3-p194@random_string --create
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Tien Nguyen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
= RandomString
|
2
|
+
|
3
|
+
Ruby library which generates strings which match a regular expression
|
4
|
+
|
5
|
+
== Examples :
|
6
|
+
RandomString.generate(/[a-z]+@[a-z]+\.[a-z]+/)
|
7
|
+
=> abcd@gmail.com
|
8
|
+
|
9
|
+
== Installation
|
10
|
+
gem install 'random_string'
|
11
|
+
|
12
|
+
or add to Gemfile:
|
13
|
+
|
14
|
+
gem 'random_string'
|
15
|
+
|
16
|
+
== Usage
|
17
|
+
RandomString.generate(/[a-z]+@[a-z]+\.[a-z]+/)
|
18
|
+
=> abcd@gmail.com
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
== Contributing
|
24
|
+
|
25
|
+
The random_string/sample_string/data.rb stores the sample data which are used to match again the Regex, this will be updated continously, please feel free update it by adding more sample data and make pull request, I really appreciate your contributions
|
26
|
+
|
27
|
+
== Copyright
|
28
|
+
|
29
|
+
Copyright (c) 2013 nqtien310@gmail.com.
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'random_string/sample_string'
|
2
|
+
|
3
|
+
module RandomString
|
4
|
+
module Main
|
5
|
+
include RandomString::SampleString
|
6
|
+
|
7
|
+
def generate(pattern)
|
8
|
+
matched_strings = []
|
9
|
+
unmatched_strings = []
|
10
|
+
sample_strings.each do |sample_string|
|
11
|
+
break if matched_strings.size == ResultCount && unmatched_strings.size == ResultCount
|
12
|
+
|
13
|
+
if sample_string =~ pattern
|
14
|
+
matched_strings << sample_string if matched_strings.size < ResultCount
|
15
|
+
else
|
16
|
+
unmatched_strings << sample_string if unmatched_strings.size < ResultCount
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
{
|
21
|
+
matched_strings: matched_strings,
|
22
|
+
unmatched_strings: unmatched_strings
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
alias_method :gen, :generate
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module RandomString
|
2
|
+
module SampleString
|
3
|
+
module Data
|
4
|
+
StoredStrings = {
|
5
|
+
:Emails => ['us@gmail.com', 'us.vn@gmail.com', 'us_vn@gmail.com', 'us_vn@gmail.com.vn'],
|
6
|
+
:Urls => ['www.domain.com', 'www.domain.subdomain.com', 'http://www.domain.com', 'http://www.domain.subdomain.com',
|
7
|
+
'http://www.domain.com/path/1', 'https://www.domain.com', 'https://www.domain.subdomain.com', 'domain.subdomain.com',
|
8
|
+
'domain.com/path/1', 'subdomain.domain.com/path/1'],
|
9
|
+
:IPS => ['192.168.1.1', '192.168.1.2'],
|
10
|
+
:Sex => ['male', 'female', 'gay', 'les', 'other'],
|
11
|
+
:Zips => ['10001', '10002', '10003'],
|
12
|
+
:Phones => ['732-757-2923', '1732-757-2923', '+84 121731845', '84 121731845', '(84) 121731845'],
|
13
|
+
:Datetimes => ['1999-01-01', '99-01-01', '1999/01/01', '99/01/01', '01/01/99', '03/10/1986', '01-01-1999', '01-01-99', 'January 07, 2013',
|
14
|
+
'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday',
|
15
|
+
'january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december',
|
16
|
+
'am', 'pm'],
|
17
|
+
:Countries => ['Vietnam', 'America', 'England', 'Vietnamese', 'American', 'English'],
|
18
|
+
:Cities => ['New York', 'London', 'Ho Chi Minh'],
|
19
|
+
:Statuses => ['married', 'single', 'available', 'complicated'],
|
20
|
+
:Files => ['filename.txt', 'filename.jpg', 'filename.png', 'filename.jpeg'],
|
21
|
+
:Extensions => ['txt', 'jpg', 'png', 'jpeg', 'mp4', 'mp3', 'wmv'],
|
22
|
+
:Videos => ['filename.wmv', 'filename.mp4', 'filename.flv'],
|
23
|
+
:CCNumbers => ['4111 1111 1111 1111', '5500 0000 0000 0004', '3400 0000 0000 009', '3000 0000 0000 04'],
|
24
|
+
:CCodes => ['84', '1', '39'],
|
25
|
+
:ISOCCodes => ['US', 'USA', 'VN', 'VNM'],
|
26
|
+
:Services => ['yahoo', 'google', 'facebook', 'amazone', 'youtube', 'paypal', 'alchemy'],
|
27
|
+
:Marks => ['excellent', 'good', 'bad']
|
28
|
+
}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "random_string/sample_string/data"
|
2
|
+
|
3
|
+
module RandomString
|
4
|
+
module SampleString
|
5
|
+
ResultCount = 3
|
6
|
+
GenerateCount = 50
|
7
|
+
LowerCaseChars = ('a'..'z').to_a
|
8
|
+
UpperCaseChars = ('A'..'Z').to_a
|
9
|
+
Digits = (0..9).to_a
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def sample_strings
|
14
|
+
(stored_strings + random_digits_strings + random_chars_strings).uniq.shuffle
|
15
|
+
end
|
16
|
+
|
17
|
+
def stored_strings
|
18
|
+
Data::StoredStrings.values.flatten
|
19
|
+
end
|
20
|
+
|
21
|
+
def random_digits_strings
|
22
|
+
random_strings(Digits)
|
23
|
+
end
|
24
|
+
|
25
|
+
def random_chars_strings
|
26
|
+
random_strings(LowerCaseChars + UpperCaseChars)
|
27
|
+
end
|
28
|
+
|
29
|
+
def random_strings(base)
|
30
|
+
(1..GenerateCount).map { |length| length.times.inject('') { |initial, n| initial + base[rand(base.size-1)].to_s } }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'random_string/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "random_string"
|
8
|
+
spec.version = RandomString::VERSION
|
9
|
+
spec.authors = ["Tien Nguyen"]
|
10
|
+
spec.email = ["nqtien310@gmail.com"]
|
11
|
+
spec.description = %q{Generate random strings which match a Regular expression}
|
12
|
+
spec.summary = %q{Generate random strings which match a Regular expression}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "debugger", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rspec"
|
23
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "RandomString::Main" do
|
4
|
+
include RandomString::Main
|
5
|
+
|
6
|
+
describe '#generate' do
|
7
|
+
let(:pattern) { /^[a-z0-9_\.]+@[a-z]+\.[a-z]+$/i }
|
8
|
+
|
9
|
+
before { should_receive(:sample_strings).and_return(strings) }
|
10
|
+
|
11
|
+
context 'only matches strings' do
|
12
|
+
let(:strings) {
|
13
|
+
[
|
14
|
+
'nqtien310@gmail.com',
|
15
|
+
'nqtien310@hotdev.com'
|
16
|
+
]
|
17
|
+
}
|
18
|
+
let(:expected) do
|
19
|
+
{
|
20
|
+
matched_strings: strings,
|
21
|
+
unmatched_strings: []
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should return matches string' do
|
26
|
+
gen(pattern).should eq expected
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'only unmatched strings' do
|
31
|
+
let(:strings) {
|
32
|
+
[
|
33
|
+
'nqtien310',
|
34
|
+
'nqtien310'
|
35
|
+
]
|
36
|
+
}
|
37
|
+
let(:expected) do
|
38
|
+
{
|
39
|
+
matched_strings: [],
|
40
|
+
unmatched_strings: strings
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should return matches string' do
|
45
|
+
gen(pattern).should eq expected
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'both matched and unmatched strings' do
|
50
|
+
let(:strings) {
|
51
|
+
[
|
52
|
+
'nqtien310@gmail.com',
|
53
|
+
'nqtien310'
|
54
|
+
]
|
55
|
+
}
|
56
|
+
let(:expected) do
|
57
|
+
{
|
58
|
+
matched_strings: ['nqtien310@gmail.com'],
|
59
|
+
unmatched_strings: ['nqtien310']
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should return matches string' do
|
64
|
+
gen(pattern).should eq expected
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'multiple return results' do
|
69
|
+
before { RandomString::Main::ResultCount = 3 }
|
70
|
+
|
71
|
+
let(:strings) {
|
72
|
+
[
|
73
|
+
'nqtien310@gmail.com',
|
74
|
+
'nqtien310@skype.com',
|
75
|
+
'nqtien310@hotmail.com',
|
76
|
+
'nqtien310@abc.com',
|
77
|
+
'nqtien310',
|
78
|
+
'nqtien3101',
|
79
|
+
'nqtien3102',
|
80
|
+
'nqtien3103'
|
81
|
+
]
|
82
|
+
}
|
83
|
+
let(:expected) do
|
84
|
+
{
|
85
|
+
matched_strings: ['nqtien310@gmail.com', 'nqtien310@skype.com', 'nqtien310@hotmail.com'],
|
86
|
+
unmatched_strings: ['nqtien310', 'nqtien3101', 'nqtien3102'],
|
87
|
+
}
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should return maximum (specific in ResultCount) results for each type' do
|
91
|
+
gen(pattern).should eq expected
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "RandomString::SampleString" do
|
4
|
+
include RandomString::SampleString
|
5
|
+
|
6
|
+
describe "#stored_strings" do
|
7
|
+
it 'should return an array of string' do
|
8
|
+
send(:stored_strings).should be_an Array
|
9
|
+
send(:stored_strings).should_not be_empty
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#random_digits' do
|
14
|
+
let(:random_digits) { send(:random_digits_strings) }
|
15
|
+
|
16
|
+
it 'should return array with maximum size config in SampleString::GenerateCount' do
|
17
|
+
random_digits.size.should eq RandomString::SampleString::GenerateCount
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should return array of different length digits' do
|
21
|
+
random_digits.each { |random_str| (random_str =~ /^[0-9]+$/).should_not be_nil }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#random_chars' do
|
26
|
+
let(:random_chars) { send(:random_chars_strings) }
|
27
|
+
|
28
|
+
it 'should return array with maximum size config in SampleString::GenerateCount' do
|
29
|
+
random_chars.size.should eq RandomString::SampleString::GenerateCount
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should return array of different length digits' do
|
33
|
+
random_chars.each { |random_str| (random_str =~ /^[a-zA-Z]+$/).should_not be_nil }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#sample_strings' do
|
38
|
+
before do
|
39
|
+
should_receive(:random_chars_strings).once.and_return [1,2,3]
|
40
|
+
should_receive(:random_digits_strings).once.and_return [4,5,6,6]
|
41
|
+
should_receive(:stored_strings).once.and_return [7,8,9]
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should return an unique array which is a combination of stored_strings, random_digits, random_chars' do
|
45
|
+
send(:sample_strings).sort.should eq [1,2,3,4,5,6,7,8,9]
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should shuffle the result' do
|
49
|
+
send(:sample_strings).should_not eq [1,2,3,4,5,6,7,8,9]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'random_string'
|
5
|
+
|
6
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
7
|
+
#
|
8
|
+
# RSpec.configure do |config|
|
9
|
+
# config.include FixtureContent
|
10
|
+
# end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: random_string
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tien Nguyen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: debugger
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Generate random strings which match a Regular expression
|
47
|
+
email:
|
48
|
+
- nqtien310@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .rdebugrc
|
55
|
+
- .rspec
|
56
|
+
- .rvmrc
|
57
|
+
- Gemfile
|
58
|
+
- LICENSE.txt
|
59
|
+
- README.rdoc
|
60
|
+
- Rakefile
|
61
|
+
- lib/random_string.rb
|
62
|
+
- lib/random_string/main.rb
|
63
|
+
- lib/random_string/sample_string.rb
|
64
|
+
- lib/random_string/sample_string/data.rb
|
65
|
+
- lib/random_string/version.rb
|
66
|
+
- random_string.gemspec
|
67
|
+
- spec/random_string/main_spec.rb
|
68
|
+
- spec/random_string/sample_string_spec.rb
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
homepage: ''
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.25
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: Generate random strings which match a Regular expression
|
95
|
+
test_files:
|
96
|
+
- spec/random_string/main_spec.rb
|
97
|
+
- spec/random_string/sample_string_spec.rb
|
98
|
+
- spec/spec_helper.rb
|