Class: CaptchaManager

Inherits:
Object
  • Object
show all
Defined in:
lib/trustcaptcha/captcha_manager.rb

Defined Under Namespace

Classes: SecretKeyInvalidException, VerificationNotFinishedException, VerificationNotFoundException, VerificationTokenInvalidException

Class Method Summary collapse

Class Method Details

.fetch_verification_result(verification_token, access_token) ⇒ Object



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
52
53
54
55
# File 'lib/trustcaptcha/captcha_manager.rb', line 27

def self.fetch_verification_result(verification_token, access_token)
  url = URI("https://api.trustcomponent.com/verifications/#{verification_token.verification_id}/assessments")
  headers = {
    "tc-authorization" => access_token,
    "tc-library-language" => "ruby",
    "tc-library-version" => "2.0"
  }

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.open_timeout = 3
  http.read_timeout = 5

  request = Net::HTTP::Get.new(url.request_uri, headers)
  response = http.request(request)

  case response.code.to_i
  when 403
    raise SecretKeyInvalidException, "Secret key is invalid"
  when 404
    raise VerificationNotFoundException, "Verification not found"
  when 423
    raise VerificationNotFinishedException, "Verification not finished"
  else
    raise "Failed to retrieve verification result: HTTP #{response.code}" unless response.is_a?(Net::HTTPSuccess)
  end

  VerificationResult.from_json(response.body)
end

.get_verification_result(secret_key, base64_verification_token) ⇒ Object



14
15
16
17
# File 'lib/trustcaptcha/captcha_manager.rb', line 14

def self.get_verification_result(secret_key, base64_verification_token)
  verification_token = get_verification_token(base64_verification_token)
  fetch_verification_result(verification_token, secret_key)
end

.get_verification_token(base64_verification_token) ⇒ Object



21
22
23
24
25
# File 'lib/trustcaptcha/captcha_manager.rb', line 21

def self.get_verification_token(base64_verification_token)
  VerificationToken.from_base64(base64_verification_token)
rescue StandardError => e
  raise VerificationTokenInvalidException, "Invalid verification token: #{e.message}"
end