lib/libravatar.rb in libravatar-1.2.0 vs lib/libravatar.rb in libravatar-1.3.0

- old
+ new

@@ -65,27 +65,24 @@ return [nil, nil] unless rrs.any? min_priority = rrs.map{ |r| r.priority }.min rrs.delete_if{ |r| r.priority != min_priority } - rrs = rrs.select{ |r| r.weight == 0 } + - rrs.select{ |r| r.weight > 0 }.shuffle - weight_sum = rrs.inject(0) { |a,r| a+r.weight } - value = rand( weight_sum + 1 ) - rrs.each do |r| - return [r.target, r.port] if r.weight <= value - value -= r.weight - end + weight_sum = rrs.inject(0) { |a,r| a+r.weight }.to_f + + r = rrs.max_by { |r| r.weight == 0 ? 0 : rand ** (weight_sum / r.weight) } + + return [r.target, r.port] end end def get_base_url profile = @@profiles[ @https ? 1 : 0 ] target, port = srv_lookup if (target && port) - port_fragment = port != profile[:port] ? ':' + port : '' + port_fragment = port != profile[:port] ? ':' + port.to_s : '' return profile[:scheme] + target.to_s + port_fragment else return profile[:scheme] + profile[:host] end end