fine_tune 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml CHANGED
@@ -2,4 +2,7 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 1.9.3
5
+ - 2.0.0
6
+ - 2.2.0
7
+ - jruby
5
8
  before_install: gem install bundler -v 1.12.5
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  [![Build Status](https://travis-ci.org/laknath/fine_tune.svg?branch=master)](https://travis-ci.org/laknath/fine_tune)
2
+ [![Gem Version](https://badge.fury.io/rb/fine_tune.svg)](https://badge.fury.io/rb/fine_tune)
2
3
 
3
4
  # FineTune - a flexible rate limiting/monitoring library
4
5
 
@@ -64,7 +64,8 @@ module FineTune
64
64
  # # some calculation ...
65
65
  # end
66
66
 
67
- def throttle(name, id, options)
67
+ def throttle(name, id, options = {})
68
+ options[:validate] = true
68
69
  strategy, key, options = current_strategy(name, id, options)
69
70
  count = strategy.increment(key, options)
70
71
  comp = strategy.compare?(count, options)
@@ -79,7 +80,7 @@ module FineTune
79
80
  #
80
81
  # Attributes and options are same as for +throttle+.
81
82
 
82
- def throttle!(name, id, options)
83
+ def throttle!(name, id, options = {})
83
84
  throttle(name, id, options) do |count, comp, key, strategy, opts|
84
85
  yield count, comp, key, strategy, opts if block_given?
85
86
 
@@ -90,7 +91,7 @@ module FineTune
90
91
  # Returns true if current count exceeds the given limits.
91
92
  #
92
93
  # Attributes and options are same as for +throttle+.
93
- def rate_exceeded?(name, id, options)
94
+ def rate_exceeded?(name, id, options = {})
94
95
  strategy, key, options = current_strategy(name, id, options)
95
96
  strategy.compare?(strategy.count(key, options), options) >= 0
96
97
  end
@@ -98,7 +99,7 @@ module FineTune
98
99
  # Returns the current event count.
99
100
  #
100
101
  # Attributes and options are same as for +throttle+.
101
- def count(name, id, options)
102
+ def count(name, id, options = {})
102
103
  strategy, key, options = current_strategy(name, id, options)
103
104
  strategy.count(key, options)
104
105
  end
@@ -106,7 +107,7 @@ module FineTune
106
107
  # Resets the counter for the given resource identifer to 0.
107
108
  #
108
109
  # Attributes and options are same as for +throttle+.
109
- def reset(name, id, options)
110
+ def reset(name, id, options = {})
110
111
  strategy, key, options = current_strategy(name, id, options)
111
112
  strategy.reset(key, options)
112
113
  end
@@ -143,7 +144,7 @@ module FineTune
143
144
  options = (limits[name] || {}).merge(options)
144
145
  strategy = self.class.find_strategy(options[:strategy])
145
146
 
146
- if strategy.validate?(options)
147
+ if !options.delete(:validate) || strategy.validate?(options)
147
148
  key = strategy.build_key(name, id, options)
148
149
  end
149
150
 
@@ -26,7 +26,8 @@ module FineTune
26
26
  end
27
27
 
28
28
  def increment(key, options)
29
- raise "not defined"
29
+ count(key, options) +
30
+ (options && options[:step] || 1)
30
31
  end
31
32
 
32
33
  def count(key, options)
@@ -39,8 +39,8 @@ module FineTune
39
39
  # * +:average+ - can be used to override default_average ie: 10
40
40
  # * +:window+ - can be used to override default_window ie: 3600 (one hour)
41
41
  def increment(key, options)
42
- count = count(key, options)
43
- count = [maximum(options), count + 1].min
42
+ count = super(key, options)
43
+ count = [maximum(options), count].min
44
44
  adapter.write(key, {count: count, timestamp: Time.now.to_i})
45
45
  count
46
46
  end
@@ -1,3 +1,3 @@
1
1
  module FineTune
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fine_tune
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-07-04 00:00:00.000000000 Z
12
+ date: 2016-07-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler