random_data_despegar 2.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.
@@ -0,0 +1,10 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rdoc/task'
4
+
5
+ RDoc::Task.new do |rdoc|
6
+ end
7
+
8
+ # TODO - want other tests/tasks run by default? Add them to the list
9
+ # task :default => [:spec, :features]
10
+ task :default => :test
@@ -0,0 +1,2 @@
1
+ host: subelsky@rubyforge.org
2
+ remote_dir: /var/www/gforge-projects/random-data/
@@ -0,0 +1,69 @@
1
+ dir = "#{File.dirname(__FILE__)}/random_data_despegar"
2
+ #----dependencies-----#
3
+ require "securerandom"
4
+
5
+ require "#{dir}/array_randomizer"
6
+ require "#{dir}/booleans"
7
+ require "#{dir}/contact_info"
8
+ require "#{dir}/dates"
9
+ require "#{dir}/locations"
10
+ require "#{dir}/names"
11
+ require "#{dir}/numbers"
12
+ require "#{dir}/invoice_data"
13
+ require "#{dir}/id_generator"
14
+ require "#{dir}/text"
15
+ require "#{dir}/markov"
16
+ require "#{dir}/grammar"
17
+ require "#{dir}/version"
18
+
19
+ class Random
20
+ extend RandomDataDespegar::Booleans
21
+ extend RandomDataDespegar::ContactInfo
22
+ extend RandomDataDespegar::Dates
23
+ extend RandomDataDespegar::Grammar
24
+ extend RandomDataDespegar::Locations
25
+ extend RandomDataDespegar::Names
26
+ extend RandomDataDespegar::Numbers
27
+ extend RandomDataDespegar::InvoiceData
28
+ extend RandomDataDespegar::IdGenerator
29
+ extend RandomDataDespegar::Text
30
+
31
+ # Looks for a file in the load path with the name methodname.dat, reads the lines from that file, then gives you a random line from that file.
32
+ # Raises an error if it can't find the file. For example, given a file named "horse.dat" in your load path:
33
+ # >> Random.horse
34
+ # => "Stallion"
35
+ # >> Random.horse
36
+ # => "Pony"
37
+ # >> Random.horse
38
+ # => "Mare"
39
+ # >> Random.horse
40
+ # => "Clydesdale"
41
+ # >> Random.horse
42
+ # => "Stallion"
43
+ # >> Random.horse
44
+ # => "Mare"
45
+
46
+ def self.method_missing(methodname)
47
+ thing = "#{methodname}.dat"
48
+ filename = find_path(thing)
49
+
50
+ if filename.nil?
51
+ super
52
+ else
53
+ array = []
54
+ File.open(filename, 'r') { |f| array = f.read.split(/[\r\n]+/) }
55
+ return array.rand
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def self.find_path(filename)
62
+ $:.each do |path|
63
+ new_path = File.join(path,filename)
64
+ return new_path if File.exist?(new_path)
65
+ end
66
+ return nil
67
+ end
68
+
69
+ end
@@ -0,0 +1,67 @@
1
+ # Extends the Array class with a function used to randomly choose elements. Note that if you are using Rails, ActiveSupport recently was updated to include a similar
2
+ # function. This code checks for the presence of that method and does not try to replace it. They work the same though so no worries.
3
+
4
+ module RandomDataDespegar
5
+ module ArrayRandomizer
6
+
7
+ # Randomly chooses an element from an array
8
+ # >> [1,2,3].rand = 3
9
+ # [].rand = nil
10
+
11
+ def rand
12
+ return self[Kernel.rand(self.size)]
13
+ end
14
+
15
+ # Takes an array of non-negative weights
16
+ # and returns the index selected by a
17
+ # roulette wheel weighted according to those
18
+ # weights.
19
+ # If a block is given then k is used to determine
20
+ # how many times the block is called. In this
21
+ # case nil is returned.
22
+ def roulette(k=1)
23
+ wheel = []
24
+ weight = 0
25
+ # Create the cumulative array.
26
+ self.each do |x|
27
+ raise "Illegal negative weight #{x}" if x < 0
28
+ wheel.push(weight += x)
29
+ end
30
+ # print "wheel is #{wheel.inspect}\n";
31
+ # print "weight is #{weight.inspect}\n";
32
+ raise "Array had all zero weights" if weight.zero?
33
+ wheel.push(weight + 1) #Add extra element
34
+ if block_given?
35
+ k.times do
36
+ r = Kernel.rand() # so we don't pick up that from array.
37
+ # print "r is #{r.inspect}\n";
38
+ roll = weight.to_f * r
39
+ # print "roll is #{roll.inspect}\n";
40
+ 0.upto(self.size - 1) do |i|
41
+ if wheel[i+1] > roll
42
+ yield i
43
+ break
44
+ end # if
45
+ end # upto
46
+ end # if block_given?
47
+ return nil
48
+ else
49
+ r = Kernel.rand() # so we don't pick up that from array.
50
+ # print "r is #{r.inspect}\n";
51
+ roll = weight.to_f * r
52
+ # print "roll is #{roll.inspect}\n";
53
+ 0.upto(self.size - 1) do |i|
54
+ return i if wheel[i+1] > roll
55
+ end
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+ unless Array.respond_to?(:rand)
64
+ Array.send :include, RandomDataDespegar::ArrayRandomizer
65
+ end
66
+
67
+
@@ -0,0 +1,10 @@
1
+ module RandomDataDespegar
2
+
3
+ module Booleans
4
+ #Returns true or false randomly
5
+ def boolean
6
+ rand(2) == 1 ? true : false
7
+ end
8
+ end
9
+
10
+ end
@@ -0,0 +1,24 @@
1
+ module RandomDataDespegar
2
+ # Methods for randomly generating contact data like phone numbers and e-mail addresses
3
+
4
+ module ContactInfo
5
+
6
+ # Returns a randomly-generated string of digits that roughly resembles a US telephone number. Not guaranteed to be a valid area code.
7
+ def phone
8
+ "#{rand(900) + 100}-#{rand(900)+100}-#{rand(10000)+1000}"
9
+ end
10
+
11
+ # Returns a randomly-generated string of digits that roughly resembles an international telephone number as dialed from the US.
12
+ # Not guaranteed to be a valid number but just good enough to get some sample data going.
13
+
14
+ def international_phone
15
+ "011-#{rand(100) + 1}-#{rand(100)+10}-#{rand(10000)+1000}"
16
+ end
17
+
18
+ # Returns an e-mail address of the form "{first_initial}{last_name}@{domain}"
19
+ def email
20
+ domains = %w(yahoo.com gmail.com privacy.net webmail.com msn.com hotmail.com example.com privacy.net)
21
+ "#{(initial + lastname).downcase}\@#{domains.rand}"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,101 @@
1
+ require 'date'
2
+
3
+ module RandomDataDespegar
4
+
5
+ # Defines methods for random date generation
6
+
7
+ module Dates
8
+
9
+ # Returns a random date.
10
+ # If dayrange is an Integer,
11
+ # then the date returned will be at least
12
+ # what you specify from today.
13
+ # If dayrange is a range,
14
+ # you'll get a date between
15
+ # dayrange.min from today and dayrange.max from today.
16
+ # dayrange will be an integer or a range
17
+ #
18
+ def date(dayrange=45)
19
+ if dayrange.is_a?(Range)
20
+ offset = rand(dayrange.max-dayrange.min) + dayrange.min
21
+ else
22
+ offset = rand(dayrange*2) + dayrange
23
+ end
24
+ Date.today + offset
25
+ end
26
+
27
+ # Returns a date within the specified Range. The Range can be Date or String objects.
28
+ #
29
+ # Example:
30
+ # min = Date.parse('1966-11-15')
31
+ # max = Date.parse('1990-01-01')
32
+ # Random.date(min..max) # => a Date between 11/15/1996 and 1/1/1990
33
+ # Random.date('1966-11-15'..'1990-01-01') # => a Date between 11/15/1996 and 1/1/1990
34
+ #
35
+ def date_between(range)
36
+ min_date = range.min.is_a?(Date) ? range.min : Date.parse(range.min)
37
+ max_date = range.max.is_a?(Date) ? range.max : Date.parse(range.max)
38
+ diff = (max_date - min_date).to_i
39
+ min_date + rand(diff)
40
+ end
41
+
42
+ # Returns valid departure and return dates (Date and formated)
43
+ # Dates will be about 2/3 month from now,
44
+ # and stay will be between 5 and 20 days.
45
+ # departure_date_range will be a range
46
+ #
47
+ def departure_and_return_dates (departure_date_range=60..90)
48
+ stay = rand(5..20)
49
+ departure_date = Date.today + rand(departure_date_range)
50
+ return_date = departure_date + stay
51
+ {
52
+ :departure_date => departure_date,
53
+ :return_date => return_date,
54
+ :formated_departure_date => departure_date.strftime("%F"),
55
+ :formated_return_date => return_date.strftime("%F")
56
+ }
57
+ end
58
+
59
+ # Returns a hash with birhdate for a specific passenger type: adult, child or infant.
60
+ # Each passenger has: an age, month of birth and day of birth.
61
+ #
62
+ def passenger_birthdate (passenger_type)
63
+ present_year = Date.today.year
64
+ month = rand(1..12)
65
+ case
66
+ when passenger_type.eql?("adult") then
67
+ adt_age = rand(19..60)
68
+ month_adt_days = Date.new(present_year-adt_age,month,-1).day
69
+ adult = {
70
+ :age => adt_age,
71
+ :month => month,
72
+ :day => rand(1..month_adt_days)
73
+ }
74
+ when passenger_type.eql?("child") then
75
+ chd_age = rand(3..5)
76
+ month_chd_days = Date.new(present_year-chd_age,month,-1).day
77
+ child = {
78
+ :age => chd_age,
79
+ :month => month,
80
+ :day => rand(1..month_chd_days)
81
+ }
82
+ when passenger_type.eql?("infant") then
83
+ inf_age = rand(0..2)
84
+ month_inf_days = Date.new(present_year-inf_age,month,-1).day
85
+ infant = {
86
+ :age => inf_age,
87
+ :month => month,
88
+ :day => rand(1..month_inf_days)
89
+ }
90
+ end
91
+ end
92
+
93
+ # Returns 2016-02-26T09:16:13Z
94
+ def passenger_birthdate_parsed(passenger_type)
95
+ pax_data = passenger_birthdate(passenger_type)
96
+ now = Time.now.utc.to_date
97
+ pax_data[:b_year] = now.year - pax_data[:age]
98
+ DateTime.new(pax_data[:b_year], pax_data[:month], pax_data[:day]).strftime('%FT%TZ')
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,61 @@
1
+
2
+ module RandomDataDespegar
3
+
4
+ # Defines methods for the generation of random data based on a supplied grammar.
5
+
6
+ module Grammar
7
+
8
+ # Returns simple sentences based on a supplied grammar, which must be a hash, the
9
+ # keys of which are symbols. The values are either an array of successive values or a grammar
10
+ # (i.e, hash with symbols as keys, and hashes or arrays as values. The arrays contain symbols
11
+ # referencing the keys in the present grammar, or strings to be output. The keys are always symbols.
12
+ #
13
+ # Example:
14
+ # Random.grammatical_construct({:story => [:man, " bites ", :dog], :man => { :bob => "Bob"}, :dog => {:a =>"Rex", :b =>"Rover"}}, :story)
15
+ # => "Bob bites Rover"
16
+
17
+ def grammatical_construct(grammar, what=nil)
18
+ output = ""
19
+ if what.nil?
20
+ case grammar
21
+ when Hash
22
+ a_key = grammar.keys.sort_by{rand}[0]
23
+ output += grammatical_construct(grammar, a_key)
24
+ when Array
25
+ grammar.each do |item|
26
+ output += grammatical_construct(item)
27
+ end
28
+ when String
29
+ output += grammar
30
+ end
31
+ else
32
+ rhs = grammar[what]
33
+ case rhs
34
+ when Array
35
+ rhs.each do |item|
36
+ case item
37
+ when Symbol
38
+ output += grammatical_construct(grammar,item)
39
+ when String
40
+ output += item
41
+ when Hash
42
+ output += grammatical_construct(item)
43
+ else
44
+ raise "#{item.inspect} must be a symbol or string or Hash"
45
+ end
46
+ end
47
+ when Hash
48
+ output+= grammatical_construct(rhs)
49
+ when Symbol
50
+ output += grammatical_construct(rhs)
51
+ when String
52
+ output += rhs
53
+ else
54
+ raise "#{rhs.inspect} must be a symbol, string, Array or Hash"
55
+ end
56
+ end
57
+ return output
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,137 @@
1
+ module RandomDataDespegar
2
+ module IdGenerator
3
+ # cr ec pa pe ve mx co cl ar br
4
+ def id_for(site)
5
+ site ||= "generic"
6
+ self.send("generate_id_for_#{site.downcase}")
7
+ end
8
+
9
+ # Valido 20314562222
10
+ def argentinean_cuil(gender = "m", dni = "31456222")
11
+ generate_cuil(gender, dni)
12
+ end
13
+
14
+ private
15
+ def generate_id_for_ar
16
+ generate_dni
17
+ end
18
+
19
+ def generate_id_for_br
20
+ cpf
21
+ end
22
+
23
+ def generate_id_for_cl
24
+ generate_rut
25
+ end
26
+
27
+ def generate_id_for_generic
28
+ Random.number_with_size(8)
29
+ end
30
+
31
+
32
+ # CPF valido 625.664.266-00
33
+ #
34
+ def cpf
35
+ n = ""
36
+ until check_cpf(n)
37
+ return n = 11.times.map{rand(0..9)}.join.to_s
38
+ end
39
+ end
40
+ # CHILEAN RUT
41
+ # valido DEPRECATED
42
+ def generate_rut
43
+ generate_chilean_rut
44
+ end
45
+
46
+ # Valido 5.954.720-8 59547208
47
+ def rut
48
+ generate_chilean_rut
49
+ end
50
+
51
+ def check_cpf(cpf=nil)
52
+ return false if cpf.nil?
53
+ winvalidos = %w{12345678909 11111111111 22222222222 33333333333 44444444444 55555555555 66666666666 77777777777 88888888888 99999999999 00000000000}
54
+ wvalor = cpf.scan /[0-9]/
55
+ if wvalor.length == 11
56
+ unless winvalidos.member?(wvalor.join)
57
+ wvalor = wvalor.collect{|x| x.to_i}
58
+ wsoma = 10*wvalor[0]+9*wvalor[1]+8*wvalor[2]+7*wvalor[3]+6*wvalor[4]+5*wvalor[5]+4*wvalor[6]+3*wvalor[7]+2*wvalor[8]
59
+ wsoma = wsoma - (11 * (wsoma/11))
60
+ wresult1 = (wsoma == 0 or wsoma == 1) ? 0 : 11 - wsoma
61
+ if wresult1 == wvalor[9]
62
+ wsoma = wvalor[0]*11+wvalor[1]*10+wvalor[2]*9+wvalor[3]*8+wvalor[4]*7+wvalor[5]*6+wvalor[6]*5+wvalor[7]*4+wvalor[8]*3+wvalor[9]*2
63
+ wsoma = wsoma - (11 * (wsoma/11))
64
+ wresult2 = (wsoma == 0 or wsoma == 1) ? 0 : 11 - wsoma
65
+ return true if wresult2 == wvalor[10] # CPF validado
66
+ end
67
+ end
68
+ end
69
+ return false # CPF invalidado
70
+ end
71
+
72
+ def generate_chilean_rut
73
+ run = (rand * 20000000).round + 5000000
74
+
75
+ suma = 0
76
+ mul = 2
77
+ run.to_s.reverse.split('').each do |i|
78
+ suma = suma + i.to_i * mul
79
+ if mul == 7
80
+ mul = 2
81
+ else
82
+ mul += 1
83
+ end
84
+ end
85
+ res = suma % 11
86
+
87
+ if res == 1
88
+ return "#{run}k"
89
+ elsif res == 0
90
+ return "#{run}0"
91
+ else
92
+ return "#{run}#{11-res}"
93
+ end
94
+ end
95
+
96
+ # TO DO - Ver de hacer un generador real
97
+ # DNI Valido 31222333
98
+ #
99
+ def generate_dni
100
+ "#{rand(10..38)}#{rand(100..800)}#{rand(100..800)}"
101
+ end
102
+
103
+ # CUIL válido 20311412095
104
+ def generate_cuil(sexo, nro_dni)
105
+ dniStc = nro_dni.to_s.rjust 8, "0"
106
+ xyStc = 30
107
+ xyStc = 27 if sexo == "f"
108
+ xyStc = 20 if sexo == "m"
109
+ cDocum = xyStc.to_s + dniStc
110
+ matriz = [5,4,3,2,7,6,5,4,3,2]
111
+ xx = 0
112
+ 10.times do |t|
113
+ xx += cDocum[t].chr.to_i * matriz[t]
114
+ end
115
+ n = 11 - xx % 11
116
+ if n == 10
117
+ if (xyStc == 20 or xyStc == 27 or xyStc == 24)
118
+ xyStc, digitoStc = 23, 9
119
+ else
120
+ xyStc, digitoStc = 33, 9
121
+ end
122
+ digitoStc = 4 if xyStc == 27
123
+ else
124
+ digitoStc = (n == 11) ? 0 : n
125
+ end
126
+ return xyStc.to_s + dniStc + digitoStc.to_s
127
+ end
128
+
129
+ alias_method :generate_id_for_cr, :generate_id_for_generic
130
+ alias_method :generate_id_for_ec, :generate_id_for_generic
131
+ alias_method :generate_id_for_pa, :generate_id_for_generic
132
+ alias_method :generate_id_for_pe, :generate_id_for_generic
133
+ alias_method :generate_id_for_ve, :generate_id_for_generic
134
+ alias_method :generate_id_for_mx, :generate_id_for_generic
135
+ alias_method :generate_id_for_co, :generate_id_for_generic
136
+ end
137
+ end