netrand 0.0.2 → 1.0.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.
- data/CHANGELOG +7 -0
- data/Examples/random.rb +1 -1
- data/Gemfile.lock +1 -1
- data/Rakefile +13 -0
- data/bin/netrand +24 -8
- data/lib/netrand.rb +30 -21
- data/netrand.gemspec +1 -2
- data/pkg/netrand-1.0.0.gem +0 -0
- data/tests/Testint.rb +10 -4
- data/tests/Testorder.rb +1 -1
- data/tests/Teststring.rb +43 -0
- data/tests/test.rb +2 -2
- metadata +5 -4
- data/tests/TestuniqInt.rb +0 -23
data/CHANGELOG
CHANGED
data/Examples/random.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
|
1
|
+
require_relative "../lib/netrand.rb"
|
2
2
|
print Netrand.uniqint(100, 10000)
|
data/Gemfile.lock
CHANGED
data/Rakefile
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
# coding: utf-8
|
1
2
|
begin
|
2
3
|
require "bundler"
|
3
4
|
Bundler.setup
|
4
5
|
rescue LoadError
|
5
6
|
$stderr.puts "You need to have Bundler installed to be able build this gem."
|
6
7
|
end
|
8
|
+
require "fileutils"
|
7
9
|
|
8
10
|
gemspec = eval(File.read(Dir["*.gemspec"].first))
|
9
11
|
|
@@ -23,4 +25,15 @@ end
|
|
23
25
|
desc "Install gem locally"
|
24
26
|
task :install => :build do
|
25
27
|
system "gem install pkg/#{gemspec.name}-#{gemspec.version}"
|
28
|
+
end
|
29
|
+
|
30
|
+
desc "Clean automatically generated files"
|
31
|
+
task :clean => :build do
|
32
|
+
fileutils.mv "./pkg", ".."
|
33
|
+
fileutils.rm_rf "."
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "Publish the gem"
|
37
|
+
task :publish => :build do
|
38
|
+
system "gem push ./pkg/#{gemspec.name}-#{gemspec.version}"
|
26
39
|
end
|
data/bin/netrand
CHANGED
@@ -1,18 +1,34 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
|
2
|
+
require_relative "../lib/netrand.rb"
|
3
|
+
def self.loadstring
|
4
|
+
upperalpha = ARGV.include?("-ua") ? :upperalpha : nil
|
5
|
+
loweralpha = ARGV.include?("-la") ? :loweralpha : nil
|
6
|
+
digits = ARGV.include?("-d") ? :digits : nil
|
7
|
+
unique = ARGV.include?("-u") ? :unique : nil
|
8
|
+
Netrand.string(ARGV[1].to_i, ARGV[2].to_i, digits, upperalpha, loweralpha, unique)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.usage
|
12
|
+
puts "netrand int|uniqint|order|string"
|
13
|
+
puts "You provide additional arguments according to documentation in ri Netrand"
|
14
|
+
puts "You can force int to provide unique numbers by using -u flag at the end of command."
|
15
|
+
puts "Only in string mode, there are some changes."
|
16
|
+
puts ":upperalpha is activated using -ua switch"
|
17
|
+
puts ":loweralpha using -la"
|
18
|
+
puts ":digits using -d"
|
19
|
+
puts ":unique using -u"
|
20
|
+
end
|
21
|
+
|
3
22
|
case ARGV[0]
|
4
23
|
when "int"
|
5
|
-
puts Netrand.int(ARGV[1].to_i, ARGV[2].to_i, ARGV[3].to_i)
|
24
|
+
puts Netrand.int(ARGV[1].to_i, ARGV[2].to_i, ARGV[3].to_i, if ARGV[4] == "-u" then :unique end)
|
6
25
|
when "uniqint"
|
7
26
|
puts Netrand.uniqint(ARGV[1].to_i, ARGV[2].to_i, ARGV[3].to_i)
|
8
27
|
when "order"
|
9
28
|
puts Netrand.uniqint(ARGV[1].to_i, ARGV[2].to_i)
|
29
|
+
when "string"
|
30
|
+
puts self.loadstring
|
10
31
|
else
|
11
|
-
usage
|
32
|
+
self.usage
|
12
33
|
end
|
13
34
|
|
14
|
-
|
15
|
-
|
16
|
-
def usage()
|
17
|
-
#put up usage banner
|
18
|
-
end
|
data/lib/netrand.rb
CHANGED
@@ -4,24 +4,9 @@ module Netrand
|
|
4
4
|
#Generates numget random numbers using random.org service.
|
5
5
|
#You must set maximum and can set minimum number.
|
6
6
|
#Returns Array of numbers.
|
7
|
+
#If you use :unique flag, then there won't be two same numbers in the array.
|
7
8
|
|
8
|
-
def Netrand.int(numget, max, min = 1)
|
9
|
-
if numget == 0 then raise ArgumentError, "You must ask for more numbers than 0" end
|
10
|
-
if max == 0 then raise ArgumentError, "Maximum number must be bigger than 0" end
|
11
|
-
if min > max then raise ArgumentError, "max must be bigger than min!" end
|
12
|
-
values = Array.new
|
13
|
-
self.check_quota
|
14
|
-
open("http://www.random.org/integers/?num=#{numget}&min=#{min}&max=#{max}&format=plain&col=1&base=10") do |c|
|
15
|
-
c.each_line {|l| values << l.chop.to_i}
|
16
|
-
end
|
17
|
-
values
|
18
|
-
end
|
19
|
-
|
20
|
-
#Generates numget random numbers using random.org service.
|
21
|
-
#You must set maximum and can set minimum number.
|
22
|
-
#Returns Array of numbers in which there won't be two same numbers.
|
23
|
-
|
24
|
-
def Netrand.uniqint(numget, max, min = 1)
|
9
|
+
def Netrand.int(numget, max, min = 1, uniq = :notuniq)
|
25
10
|
if numget == 0 then raise ArgumentError, "You must ask for more numbers than 0" end
|
26
11
|
if max == 0 then raise ArgumentError, "Maximum number must be bigger than 0" end
|
27
12
|
if min > max then raise ArgumentError, "max must be bigger than min!" end
|
@@ -33,10 +18,12 @@ module Netrand
|
|
33
18
|
end
|
34
19
|
self.check_quota
|
35
20
|
random.call(numget, max, min)
|
36
|
-
|
37
|
-
while values.length != numget do
|
38
|
-
random.call(numget - values.length, max, min)
|
21
|
+
if uniq == :unique
|
39
22
|
values.uniq!
|
23
|
+
while values.length != numget do
|
24
|
+
random.call(numget - values.length, max, min)
|
25
|
+
values.uniq!
|
26
|
+
end
|
40
27
|
end
|
41
28
|
values
|
42
29
|
end
|
@@ -54,12 +41,34 @@ module Netrand
|
|
54
41
|
order
|
55
42
|
end
|
56
43
|
|
44
|
+
#Generates num random strings of len length.
|
45
|
+
#They can contain digits, uppercase and lowercase letters.
|
46
|
+
#They can be set to be unique.
|
47
|
+
|
48
|
+
def Netrand.string(num, len, *opts)
|
49
|
+
digits = opts.include?(:digits) ? "on" : "off"
|
50
|
+
upperalpha = opts.include?(:upperalpha) ? "on" : "off"
|
51
|
+
loweralpha = opts.include?(:loweralpha) ? "on" : "off"
|
52
|
+
unique = opts.include?(:unique) ? "on" : "off"
|
53
|
+
if len == 0 then raise ArgumentError, "Strings must be longer than 0 characters." end
|
54
|
+
if digits == "off" && upperalpha == "off" && loweralpha == "off" then raise ArgumentError, "You disabled every type of character." end
|
55
|
+
values = Array.new
|
56
|
+
self.check_quota
|
57
|
+
open("http://www.random.org/strings/?num=#{num}&len=#{len}&digits=#{digits}&upperalpha=#{upperalpha}&loweralpha=#{loweralpha}&unique=#{unique}&format=plain") do |c|
|
58
|
+
c.each_line {|l| values << l.chop}
|
59
|
+
end
|
60
|
+
values
|
61
|
+
end
|
62
|
+
|
57
63
|
#Generates exception if quota of random.org is exhausted.
|
58
64
|
|
59
65
|
def Netrand.check_quota
|
66
|
+
quota = nil
|
60
67
|
open("http://www.random.org/quota/?format=plain") do |c|
|
61
|
-
|
68
|
+
quota = c.read.to_i
|
69
|
+
if quota < 0 then raise QuotaExhaustedException, "Random.org quota exhausted, please try again after 0:00 UTC" end
|
62
70
|
end
|
71
|
+
quota
|
63
72
|
end
|
64
73
|
|
65
74
|
class QuotaExhaustedException < RuntimeError
|
data/netrand.gemspec
CHANGED
@@ -5,11 +5,10 @@ Gem::Specification.new do |s|
|
|
5
5
|
s.summary = "Generate true random numbers using random.org"
|
6
6
|
s.description= "Wrapper around some of the Random.org's functions. To be exact, you can use sequence randomizer and integer generator. There is also one more function, which makes list, in which every integer will be unique."
|
7
7
|
s.requirements = [ 'Working internet connection' ]
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "1.0.0"
|
9
9
|
s.author = "Róbert Selvek"
|
10
10
|
s.email = "me@sellweek.eu"
|
11
11
|
s.homepage = "http://github.com/sellweek/netrand"
|
12
|
-
s.platform = Gem::Platform::RUBY
|
13
12
|
s.required_ruby_version = '>=1.9'
|
14
13
|
s.files = Dir['**/**']
|
15
14
|
s.executables = [ 'netrand' ]
|
Binary file
|
data/tests/Testint.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
|
1
|
+
require_relative "../lib/netrand.rb"
|
2
2
|
require "test/unit"
|
3
|
-
class
|
3
|
+
class TestInt < MiniTest::Unit::TestCase
|
4
4
|
|
5
5
|
def test_not_same
|
6
|
-
nums1 = Netrand.int(
|
7
|
-
nums2 = Netrand.int(
|
6
|
+
nums1 = Netrand.int(5, 100)
|
7
|
+
nums2 = Netrand.int(5, 100)
|
8
8
|
refute_equal(nums1, nums2)
|
9
9
|
end
|
10
10
|
|
@@ -13,5 +13,11 @@ class Testint < MiniTest::Unit::TestCase
|
|
13
13
|
assert_raises(ArgumentError) {Netrand.int(100, 0)}
|
14
14
|
assert_raises(ArgumentError) {Netrand.int(100, 999999, 9999991)}
|
15
15
|
end
|
16
|
+
|
17
|
+
def test_uniq
|
18
|
+
nums = Netrand.int(150, 10000, 1, :unique)
|
19
|
+
assert_equal(nums.uniq.length, nums.length)
|
20
|
+
assert_equal(150, nums.length)
|
21
|
+
end
|
16
22
|
|
17
23
|
end
|
data/tests/Testorder.rb
CHANGED
data/tests/Teststring.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require_relative "../lib/netrand.rb"
|
2
|
+
require "test/unit"
|
3
|
+
class TestString < MiniTest::Unit::TestCase
|
4
|
+
def test_not_same
|
5
|
+
str1 = Netrand.string(10, 8, :upperalpha, :loweralpha)
|
6
|
+
str2 = Netrand.string(10, 8, :upperalpha, :loweralpha)
|
7
|
+
refute_equal(str1, str2)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_uniq
|
11
|
+
str = Netrand.string(10, 8, :loweralpha)
|
12
|
+
assert_equal(str.uniq.length, str.length)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_error_handling
|
16
|
+
assert_raises(ArgumentError) {Netrand.string(10, 0, :digits, :upperalpha)}
|
17
|
+
assert_raises(ArgumentError) {Netrand.string(10, 8)}
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_digits
|
21
|
+
s1 = Netrand.string(10, 8, :digits)
|
22
|
+
s1.each do |s|
|
23
|
+
s.each_char do |c|
|
24
|
+
assert(c.to_i.integer?, "#{c} in #{s} not a number!")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_upcase
|
30
|
+
s1 = Netrand.string(10, 8, :upperalpha)
|
31
|
+
s2 = s1.map {|s| s.upcase}
|
32
|
+
assert_equal(s2, s1)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_downcase
|
36
|
+
s1 = Netrand.string(10, 8, :loweralpha)
|
37
|
+
s2 = s1.map {|s| s.downcase}
|
38
|
+
assert_equal(s2, s1)
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
end
|
data/tests/test.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: netrand
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 1.0.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "R\xC3\xB3bert Selvek"
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-23 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: Wrapper around some of the Random.org's functions. To be exact, you can use sequence randomizer and integer generator. There is also one more function, which makes list, in which every integer will be unique.
|
@@ -30,12 +30,13 @@ files:
|
|
30
30
|
- lib/netrand.rb
|
31
31
|
- LICENSE
|
32
32
|
- netrand.gemspec
|
33
|
+
- pkg/netrand-1.0.0.gem
|
33
34
|
- Rakefile
|
34
35
|
- README.md
|
35
36
|
- tests/test.rb
|
36
37
|
- tests/Testint.rb
|
37
38
|
- tests/Testorder.rb
|
38
|
-
- tests/
|
39
|
+
- tests/Teststring.rb
|
39
40
|
homepage: http://github.com/sellweek/netrand
|
40
41
|
licenses: []
|
41
42
|
|
@@ -55,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
56
|
requirements:
|
56
57
|
- - ">="
|
57
58
|
- !ruby/object:Gem::Version
|
58
|
-
hash:
|
59
|
+
hash: -1149190834117444518
|
59
60
|
segments:
|
60
61
|
- 0
|
61
62
|
version: "0"
|
data/tests/TestuniqInt.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
require "netrand"
|
2
|
-
require "test/unit"
|
3
|
-
class TestInt < MiniTest::Unit::TestCase
|
4
|
-
|
5
|
-
def test_uniq
|
6
|
-
nums = Netrand.uniqint(150, 10000)
|
7
|
-
assert_equal(nums.uniq.length, nums.length)
|
8
|
-
assert_equal(150, nums.length)
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_not_same
|
12
|
-
nums1 = Netrand.uniqint(3, 100)
|
13
|
-
nums2 = Netrand.uniqint(3, 100)
|
14
|
-
refute_equal(nums1, nums2)
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_error_handling
|
18
|
-
assert_raises(ArgumentError) {Netrand.uniqint(0, 10)}
|
19
|
-
assert_raises(ArgumentError) {Netrand.uniqint(100, 0)}
|
20
|
-
assert_raises(ArgumentError) {Netrand.uniqint(100, 999999, 9999991)}
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|