lato 0.1.59 → 0.1.60

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4cc5abe2007e8028c5de1b2e4f76a4a9b93412e862059cb69b36e4940a37650f
4
- data.tar.gz: d89a0f0b13c2e994c063f6659f8e4456bbc695e701b0934d5686be005f0ca618
3
+ metadata.gz: 7c9eb96e4d8465b539ddee7df568a3b9c37bdfb55e9b8b24ad3c5969a53c0e32
4
+ data.tar.gz: b31a028a5f3dbdf18ba8f846bb0636ee77e967a1b96ebd858fef93cd882ec0d1
5
5
  SHA512:
6
- metadata.gz: 582c91f202e59201d654e1f3582070ee8c8a43797d44ca5c387a2732220b48c34487856175748eb4be480f418f94be3c4ee1abc03e601fbffef2b267f08957bd
7
- data.tar.gz: e3798419615b5a8ac29ba9e824ca2fa6fa030567902edda06e1ac0d890d36c57db14f878f3da75a498ee6238d32e796d04d43310d1f027f700a62fd2c3bd74e8
6
+ metadata.gz: d150983fdebfba187569b988f957ca111030bc5617a64338cb90f13a9a4c635ab6fd10b88ada8f396615adb7d13cbf1bcef38decd9453962eb39bb16524864fb
7
+ data.tar.gz: 8ddff51d4154bf41ee332bb6b8cd8eefcb7ad2c3d70040b2eebaeedd97fe4bdd602188750b36e6febaed02c8d225d385b455e62e21e8ffdd2ab5836d2f312950
@@ -30,6 +30,22 @@ module Lato
30
30
  false
31
31
  end
32
32
 
33
+ def limit_requests(limit = 10, time_window = 10.minutes)
34
+ cache_key = "Lato::Sessionable/limit_requests/#{controller_name}/#{action_name}/#{request.remote_ip}"
35
+ attempts = Rails.cache.read(cache_key) || 0
36
+
37
+ attempts += 1
38
+ Rails.cache.write(cache_key, attempts, expires_in: time_window)
39
+ return true unless attempts >= limit
40
+
41
+ respond_to do |format|
42
+ format.html { render plain: "Too many requests, please wait #{time_window.to_i / 60} minutes to retry.", status: :too_many_requests }
43
+ format.json { render json: {}, status: :too_many_requests }
44
+ end
45
+
46
+ false
47
+ end
48
+
33
49
  def session_create(user_id)
34
50
  cookies.encrypted[:lato_session] = { value: Lato::Session.generate_session_per_user(user_id), expires: Lato.config.session_lifetime.from_now }
35
51
  @session = Lato::Session.new(cookies.encrypted[:lato_session])
@@ -35,22 +35,6 @@ module Lato
35
35
  I18n.locale = @session.user.locale || I18n.default_locale
36
36
  end
37
37
 
38
- # This method limit the number of requests for a specific action.
39
- # Usage: before_action :limit_requests, only: %i[:action_name]
40
- def limit_requests(limit = 10, time_window = 10.minutes)
41
- cache_key = "Lato::ApplicationController.limit_requests/#{controller_name}/#{action_name}/#{request.remote_ip}"
42
- attempts = Rails.cache.read(cache_key) || 0
43
-
44
- attempts += 1
45
- Rails.cache.write(cache_key, attempts, expires_in: time_window)
46
- return unless attempts >= limit
47
-
48
- respond_to do |format|
49
- format.html { render plain: "Too many requests, please wait #{time_window.to_i / 60} minutes to retry.", status: :too_many_requests }
50
- format.json { render json: {}, status: :too_many_requests }
51
- end
52
- end
53
-
54
38
  def respond_to_with_not_found
55
39
  respond_to do |format|
56
40
  format.html { render plain: '', status: :not_found }
data/lib/lato/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Lato
2
- VERSION = "0.1.59"
2
+ VERSION = "0.1.60"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.59
4
+ version: 0.1.60
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregorio Galante