Class: RWB::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/rwb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(testhash = nil) ⇒ Builder

Returns a new instance of Builder.



13
14
15
16
17
18
19
# File 'lib/rwb.rb', line 13

def initialize(testhash = nil)
  @urls = Hash.new
  @total_weight = 0
  if testhash
    build_urls(testhash)
  end
end

Instance Attribute Details

#total_weightObject (readonly)

Returns the value of attribute total_weight.



11
12
13
# File 'lib/rwb.rb', line 11

def total_weight
  @total_weight
end

#urlsObject (readonly)

Returns the value of attribute urls.



11
12
13
# File 'lib/rwb.rb', line 11

def urls
  @urls
end

Instance Method Details

#add_url(weight, url, method = 'get') ⇒ Object



21
22
23
24
25
# File 'lib/rwb.rb', line 21

def add_url(weight, url, method='get')
  @total_weight += weight.to_i
  @urls[@total_weight] = Url.new(weight, url, method)
  7
end

#add_url_group(weight, base, extension_array, method = 'get') ⇒ Object



27
28
29
30
31
# File 'lib/rwb.rb', line 27

def add_url_group(weight, base, extension_array, method='get')
  @total_weight += weight.to_i
  @urls[@total_weight] = UrlGroup.new(weight, base, 
                                      extension_array, method)
end

#add_url_session(weight, session, name, method = 'get') ⇒ Object



33
34
35
36
37
# File 'lib/rwb.rb', line 33

def add_url_session(weight, session, name, method='get')
  @total_weight += weight.to_i
  @urls[@total_weight] = UrlSession.new(weight, session, 
                                        name, method)
end

#build_urls(tests) ⇒ Object



51
52
53
54
55
# File 'lib/rwb.rb', line 51

def build_urls(tests)
  tests.keys.each do |key|
    add_url(tests[key], key)
  end
end

#get_url(seed = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rwb.rb', line 39

def get_url(seed = nil)
  if seed
    srand seed
  end
  pick = rand(@total_weight)
  @urls.keys.sort.each do |key|
    if pick <= key
      return @urls[key]
    end
  end
end