Class: Shugoi::ApiClient

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

Overview

Client HTTP vers l'API Shugoi (parité avec fetch dans le module Node).

Constant Summary collapse

TIMEOUT =
5.0

Instance Method Summary collapse

Constructor Details

#initialize(base_url, debug: false) ⇒ ApiClient

Returns a new instance of ApiClient.



12
13
14
15
# File 'lib/shugoi/api_client.rb', line 12

def initialize(base_url, debug: false)
  @base_url = base_url.to_s
  @debug = debug
end

Instance Method Details

#fetch_guard_detect(site_key, secret = nil) ⇒ Object

GET /guard-detect?key=...&raw=1&cb=...&sig=... → texte du guard



30
31
32
33
34
35
36
37
38
# File 'lib/shugoi/api_client.rb', line 30

def fetch_guard_detect(site_key, secret = nil)
  cb = Utils.now_ms
  query = { key: site_key, raw: 1, cb: cb.to_s }
  query[:sig] = Utils.hmac_hex(secret, cb.to_s) if secret && !secret.empty?
  body = get_raw("/guard-detect", query)
  body
rescue StandardError
  nil
end

#fetch_whitelist(site_key) ⇒ Object

GET /whitelist?key=... → { whitelistedMachines, detectionFlags, skipPaths }



18
19
20
21
22
23
24
25
26
27
# File 'lib/shugoi/api_client.rb', line 18

def fetch_whitelist(site_key)
  json = get("/whitelist", key: site_key)
  {
    whitelist: json["whitelistedMachines"] || [],
    flags: json["detectionFlags"] || json["flags"] || {},
    skip_paths: json["skipPaths"] || []
  }
rescue StandardError
  { whitelist: [], flags: {}, skip_paths: [] }
end

#post_event(site_key, reason, machine_id = "") ⇒ Object

POST /event — enregistrement d'un événement (block, headless…)



41
42
43
44
45
46
# File 'lib/shugoi/api_client.rb', line 41

def post_event(site_key, reason, machine_id = "")
  payload = { siteKey: site_key, reason: reason, machineId: machine_id }
  post_json("/event", payload)
rescue StandardError
  nil
end

#validate_key(site_key, secret) ⇒ Object

POST /validate-key — valide la clé



49
50
51
52
53
54
55
# File 'lib/shugoi/api_client.rb', line 49

def validate_key(site_key, secret)
  payload = { siteKey: site_key, secret: secret }
  json = post_json("/validate-key", payload)
  json
rescue StandardError
  { "valid" => false, "reason" => "network" }
end