Class: Rule

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Rule

Returns a new instance of Rule.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rule.rb', line 3

def initialize(options)
  default_options = {
    :match => /.*/,
    :metric => :rph,
    :type => :frequency,
    :limit => 100,
    :per_ip => true,
    :per_url => false,
    :token => false
  }
  @options = default_options.merge(options)

end

Instance Method Details

#get_expirationObject



25
26
27
# File 'lib/rule.rb', line 25

def get_expiration
  (Time.now + ( @options[:type] == :frequency ? get_frequency : get_fixed ))
end

#get_fixedObject



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

def get_fixed
  case @options[:metric]
  when :rpd
    return 86400
  when :rph
    return 3600
  when :rpm
    return 60
  end
end

#get_frequencyObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/rule.rb', line 29

def get_frequency
  case @options[:metric]
  when :rpd
    return (86400/@options[:limit] == 0 ? 1 : 86400/@options[:limit])
  when :rph
    return (3600/@options[:limit] == 0 ? 1 : 3600/@options[:limit])
  when :rpm
    return (60/@options[:limit] == 0 ? 1 : 60/@options[:limit])
  end
end

#get_key(request) ⇒ Object



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

def get_key(request)
  key = (@options[:per_url] ? request.path : @options[:match].to_s)
  key = key + request.ip.to_s if @options[:per_ip]
  key = key + request.params[@options[:token].to_s] if @options[:token]
  key
end

#limitObject



21
22
23
# File 'lib/rule.rb', line 21

def limit
  (@options[:type] == :frequency ? 1 : @options[:limit])
end

#matchObject



17
18
19
# File 'lib/rule.rb', line 17

def match
  @options[:match].class == String ? Regexp.new(@options[:match] + "$") : @options[:match]
end