Class: RateLimitedSearch

Inherits:
Common::RedisStore show all
Defined in:
app/models/rate_limited_search.rb

Defined Under Namespace

Classes: RateLimitedError

Constant Summary collapse

COUNT_LIMIT =
3

Constants inherited from Common::RedisStore

Common::RedisStore::REQ_CLASS_INSTANCE_VARS

Class Method Summary collapse

Methods inherited from Common::RedisStore

create, delete, #destroy, #destroyed?, exists?, #expire, find, find_or_build, #initialize, #initialize_dup, keys, #persisted?, pop, redis_key, redis_store, redis_ttl, #save, #save!, #ttl, #update, #update!

Constructor Details

This class inherits a constructor from Common::RedisStore

Class Method Details

.create_or_increment_count(search_params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/rate_limited_search.rb', line 16

def self.create_or_increment_count(search_params)
  hashed_params = Digest::SHA2.hexdigest(search_params)

  rate_limited_search = find(hashed_params)

  if rate_limited_search
    raise RateLimitedError if rate_limited_search.count >= COUNT_LIMIT

    rate_limited_search.count += 1
    rate_limited_search.save!
  else
    create(search_params: hashed_params)
  end
end