Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#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_number(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/specstar/support/random.rb', line 27

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

  output
end

#random_queryObject



36
37
38
39
40
41
42
# File 'lib/specstar/support/random.rb', line 36

def random_query
  params = {}

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

#random_text(options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/specstar/support/random.rb', line 44

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

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

  (1..length).map { chars.sample }.join("")
end

#random_timeObject



58
59
60
# File 'lib/specstar/support/random.rb', line 58

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

#random_url(options = {}) ⇒ Object



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

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

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

  uri.to_s
end