Class: Shugoi::ApiClient

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

Constant Summary collapse

TIMEOUT =
5.0

Instance Method Summary collapse

Constructor Details

#initialize(base_url, debug: false) ⇒ ApiClient

Returns a new instance of ApiClient.



9
10
11
12
# File 'lib/shugoi/api_client.rb', line 9

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

Instance Method Details

#check_rate_limit(site_key, ip, user_agent = "") ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/shugoi/api_client.rb', line 51

def check_rate_limit(site_key, ip, user_agent = "")
  payload = {
    siteKey: site_key,
    scope: "edge_ip",
    ip: ip,
    metadata: { ip: ip, userAgent: user_agent || "", middleware: true }
  }
  post_json("/rate-limit-check", payload)
rescue StandardError
  { "allowed" => true }
end

#fetch_guard_detect(site_key, secret = nil) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/shugoi/api_client.rb', line 28

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?
  get_raw("/guard-detect", query)
rescue StandardError
  nil
end

#fetch_whitelist(site_key, secret = nil) ⇒ Object



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

def fetch_whitelist(site_key, secret = nil)
  cb = SecureRandom.hex(4)
  params = { key: site_key, cb: cb.to_s }
  params[:sig] = Utils.hmac_hex(secret, cb.to_s) if secret && !secret.empty?
  json = get("/whitelist", params)
  {
    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



37
38
39
40
41
42
# File 'lib/shugoi/api_client.rb', line 37

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



44
45
46
47
48
49
# File 'lib/shugoi/api_client.rb', line 44

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