Module: Auth::Centric::Captcha::Retrieve

Included in:
SecurityCaptcha
Defined in:
lib/auth/centric/captcha/retrieve.rb

Instance Method Summary collapse

Instance Method Details

#find_or_create_pathObject



34
35
36
# File 'lib/auth/centric/captcha/retrieve.rb', line 34

def find_or_create_path
  [host, 'api/v1/security_captchas'].join('/')
end

#retrieve_captchaObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/auth/centric/captcha/retrieve.rb', line 7

def retrieve_captcha
  return true unless enabled?

  payload = {
    security_captcha: {
      ip: @ip_address,
      session_id: @session_id
    }
  }

  http = HTTP
           .timeout(timeout_seconds)
           .headers(apikey:)
           .post(find_or_create_path, json: payload)

  case http.status
    when 200..202
      JSON.parse(http.body)['data']['attributes'].except('session_id', 'ip')
    when 422
      raise Error, http.body.to_s
    else
      raise Error, "#{http.status}: #{http.body}"
  end
rescue HTTP::TimeoutError
  true
end