resque-restriction 0.5.1 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 86a9aca569bec8f4f56affc4d804245a9fe335e7
4
- data.tar.gz: '019f6b3d2bb70ea8fea85fbbcc194a00288382c2'
3
+ metadata.gz: 135cae91708b3d84ff8c6feafdb715e7043893ec
4
+ data.tar.gz: c4d04692d1b62a3b0bf8dc797a1c679965e05c69
5
5
  SHA512:
6
- metadata.gz: b2f1e27d8a499374a074b525c8ea64d57503ddd2a3d9a4f018ecc54639af1bb468d31f84c16c246fb00b8ee3718e2418cfc497b119f753513906ad2778569e6a
7
- data.tar.gz: 11dda9dc7c6629f268d55c1b7a7856d86db329e2e58c5db380cc84d9bd8a05d3c02cc19cbbcafd7b54b5d9d5776a7463286082698f61b988d66a31d14c10e1a0
6
+ metadata.gz: e0640880f52054dcfee91e434373a2db2b1cd5f61997fbaaefedce2d59f3647fb10ca0d921afd541c0fb2310ad8b29befe5d7f35c080e55be8d50183db089130
7
+ data.tar.gz: ecb9750f0198139276220ccb85178d09c44f824eba13bf83c3b11e8f3313c837f83f24064a65ca8a43de3b34f6fffc546ade0f5bc890052a88283e1075924876
@@ -1,6 +1,9 @@
1
1
  ### Unreleased
2
2
 
3
- * prepend restriction to cache_key
3
+ # 0.6.0
4
+
5
+ * support customized period like `per_minute_and_user_id`
6
+ * prepend restriction to `cache_key`
4
7
 
5
8
  # 0.5.0
6
9
 
data/README.md CHANGED
@@ -59,6 +59,20 @@ Advance
59
59
 
60
60
  You can also add customized restriction as you like. For example, we have a job to restrict the facebook post numbers 40 times per user per day, we can define as:
61
61
 
62
+
63
+ ```ruby
64
+ class GenerateFacebookShares
65
+ extend Resque::Plugins::Restriction
66
+
67
+ restrict :per_day_and_user_id => 40
68
+
69
+ # rest of your class here
70
+ def self.perform(options)
71
+ # options["user_id"] exists
72
+ end
73
+ end
74
+ ```
75
+
62
76
  ```ruby
63
77
  class GenerateFacebookShares
64
78
  extend Resque::Plugins::Restriction
@@ -64,13 +64,15 @@ module Resque
64
64
  end
65
65
 
66
66
  def redis_key(period, *args)
67
- period_str = case period
67
+ period_key, custom_key = period.to_s.split('_and_')
68
+ period_str = case period_key.to_sym
68
69
  when :concurrent then "*"
69
- when :per_minute, :per_hour, :per_day, :per_week then (Time.now.to_i / SECONDS[period]).to_s
70
+ when :per_minute, :per_hour, :per_day, :per_week then (Time.now.to_i / SECONDS[period_key.to_sym]).to_s
70
71
  when :per_month then Date.today.strftime("%Y-%m")
71
72
  when :per_year then Date.today.year.to_s
72
- else period.to_s =~ /^per_(\d+)$/ and (Time.now.to_i / $1.to_i).to_s end
73
- [RESTRICTION_QUEUE_PREFIX, self.restriction_identifier(*args), period_str].compact.join(":")
73
+ else period_key =~ /^per_(\d+)$/ and (Time.now.to_i / $1.to_i).to_s end
74
+ custom_value = (custom_key && args.first && args.first.is_a?(Hash)) ? args.first[custom_key] : nil
75
+ [RESTRICTION_QUEUE_PREFIX, self.restriction_identifier(*args), custom_value, period_str].compact.join(":")
74
76
  end
75
77
 
76
78
  def restriction_identifier(*args)
@@ -83,10 +85,11 @@ module Resque
83
85
  end
84
86
 
85
87
  def seconds(period)
86
- if SECONDS.keys.include? period
87
- SECONDS[period]
88
+ period_key, _ = period.to_s.split('_and_')
89
+ if SECONDS.keys.include?(period_key.to_sym)
90
+ SECONDS[period_key.to_sym]
88
91
  else
89
- period.to_s =~ /^per_(\d+)$/ and $1.to_i
92
+ period_key =~ /^per_(\d+)$/ and $1.to_i
90
93
  end
91
94
  end
92
95
 
@@ -1,5 +1,5 @@
1
1
  module Resque
2
2
  module Restriction
3
- VERSION = '0.5.1'
3
+ VERSION = '0.6.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque-restriction
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang