Class: Contrast::Utils::RoutesSent

Inherits:
Object
  • Object
show all
Defined in:
lib/contrast/utils/routes_sent.rb

Overview

This is the RoutesSent class, which determines whether observed routes can be sent to TeamServer. Routes that have not been seen (according to the cache) can be sent, as well as any route that # has been seen but not within the time limit.

Constant Summary collapse

ROUTES_LIMIT =

include Contrast::Components::Logger::InstanceMethods

500
TIME_LIMIT_IN_SECONDS =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRoutesSent

Returns a new instance of RoutesSent.



19
20
21
# File 'lib/contrast/utils/routes_sent.rb', line 19

def initialize
  @cache = {}
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



17
18
19
# File 'lib/contrast/utils/routes_sent.rb', line 17

def cache
  @cache
end

Instance Method Details

#sendable?(route) ⇒ boolean

Determine whether the provided route can be sent to TeamServer.

Parameters:

Returns:

  • (boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/contrast/utils/routes_sent.rb', line 27

def sendable? route
  return false unless route
  return false unless route.signature && !route.signature.blank?
  return false unless route.url && !route.url.blank?

  route_hash = route.hash_id

  # If hash doesn't exist in @cache...
  #   - Add hash to @cache (with Time.now)
  #   - Clear oldest entries (if more than ROUTES_LIMIT)
  #   - Return *true*
  unless cache.key?(route_hash)
    cache[route_hash] = Time.now
    remove_oldest_entries!
    return true
  end

  # If hash exists in @cache...
  #   - Return *true* if more than a minute since time recorded for hash
  #   - Return *false* if not than a minute since time recorded for hash
  return false unless Time.now.to_i - cache.fetch(route_hash, 0).to_i > TIME_LIMIT_IN_SECONDS

  cache[route_hash] = Time.now
  true
end