Class: Shugoi::KeyValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/shugoi/key_validator.rb

Constant Summary collapse

WARN_INTERVAL_MS =
3_600_000

Instance Method Summary collapse

Constructor Details

#initialize(config, api_client) ⇒ KeyValidator

Returns a new instance of KeyValidator.



5
6
7
8
9
10
11
# File 'lib/shugoi/key_validator.rb', line 5

def initialize(config, api_client)
  @config = config
  @api_client = api_client
  @valid = false
  @failed = false
  @warned_at = 0
end

Instance Method Details

#validateObject



13
14
15
16
17
18
19
20
21
# File 'lib/shugoi/key_validator.rb', line 13

def validate
  return unless @config.secret
  return if @valid || @failed

  @valid = @api_client.validate_key(@config.site_key, @config.secret)["valid"] == true
  @failed = !@valid
rescue StandardError
  @failed = true
end

#warn_if_failedObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/shugoi/key_validator.rb', line 23

def warn_if_failed
  return unless @config.secret && @failed
  return if Utils.now_ms - @warned_at < WARN_INTERVAL_MS

  @warned_at = Utils.now_ms
  warn(
    "[shugoi] La validation de la clé a échoué pour le siteKey #{@config.site_key}.\n" \
    "[shugoi] La protection reste active, mais cette installation n'est pas authentifiée.\n" \
    "[shugoi] Vérifiez `site_key` et `secret` : https://shugoi.com/docs#validation"
  )
  @api_client.post_event(@config.site_key, "validation_failed", "")
end