Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#random_dateObject



79
80
81
# File 'lib/specstar/support/random.rb', line 79

def random_date
  random_time.to_date
end

#random_domainObject



1
2
3
4
5
6
# File 'lib/specstar/support/random.rb', line 1

def random_domain
  tld = ["com", "org", "net", "biz", "info", "co", "co.in"].sample
  domain = (0..rand(3)).map { random_text(:max_length => 12).downcase }.join(".")

  "#{domain}.#{tld}"
end

#random_emailObject



8
9
10
# File 'lib/specstar/support/random.rb', line 8

def random_email
  "#{random_text}@#{random_text}.com".downcase
end

#random_exceptionObject



12
13
14
15
16
17
18
# File 'lib/specstar/support/random.rb', line 12

def random_exception
  begin
    raise random_text
  rescue => e
    e
  end
end

#random_hash(options = {}) ⇒ Object



20
21
22
23
24
25
# File 'lib/specstar/support/random.rb', line 20

def random_hash(options={})
  hash = {}
  elements = options[:length] || 1 + rand(options[:max_length] || 10)
  elements.times { hash[random_text.to_sym] = random_text }
  hash
end

#random_hexadecimal(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/specstar/support/random.rb', line 28

def random_hexadecimal(options={})
  length = if options[:length]
             options[:length]
           elsif options[:max_length]
             1 + rand(options[:max_length])
           elsif options[:min_length]
             options[:min_length] + rand(64)
	   else
	     1 + rand(64)
           end

  (1..length).map { '0123456789abcdef'.chars.sample }.join
end

#random_number(options = {}) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/specstar/support/random.rb', line 43

def random_number(options={})
  output = nil
  while output.nil? || output == options[:except]
    output = rand(options[:max] || 1e9)
  end

  output
end

#random_queryObject



52
53
54
55
56
57
58
# File 'lib/specstar/support/random.rb', line 52

def random_query
  params = {}

  (0..rand(10)).each { params[random_text :max_length => 8] = random_text }
  
  params.to_param
end

#random_text(options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/specstar/support/random.rb', line 60

def random_text(options={})
  length = if options[:length]
             options[:length]
           elsif options[:max_length]
             1 + rand(options[:max_length])
           else
             1 + rand(32)
           end

  alpha_chars = ("A".."Z").to_a + ("a".."z").to_a
  chars = alpha_chars + ("0".."9").to_a

  ([alpha_chars.sample] + (2..length).map { chars.sample }).shuffle.join("")
end

#random_timeObject



75
76
77
# File 'lib/specstar/support/random.rb', line 75

def random_time
  Time.at rand Time.now.to_i
end

#random_url(options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/specstar/support/random.rb', line 83

def random_url(options={})
  domain = options.include?(:domain) ? options[:domain] : random_domain
  scheme = options.include?(:scheme) ? options[:scheme] : ['http', 'https'].sample
  fragment = options.include?(:fragment) ? options[:fragment] : random_text
  query = options.include?(:query)? options[:query] : random_query

  uri = URI("#{scheme}://#{domain}")
  uri.path = "/#{random_text.downcase}"
  uri.query = query
  uri.fragment = fragment

  uri.to_s
end