Module: FriendlyCaptcha::ControllerHelpers

Defined in:
lib/friendly_captcha/controller_helpers.rb

Instance Method Summary collapse

Instance Method Details

#friendly_captcha_response_token(options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/friendly_captcha/controller_helpers.rb', line 33

def friendly_captcha_response_token(options = {})
  field_name = options[:field_name] || 'frc-captcha-response'
  response_param = params[field_name]
  
  if response_param.is_a?(String)
    response_param.strip
  else
    ''
  end
end

#verify_friendly_captcha(options = {}) ⇒ Object



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
# File 'lib/friendly_captcha/controller_helpers.rb', line 7

def verify_friendly_captcha(options = {})
  response_token = friendly_captcha_response_token(options)
  
  return { success: false, error: "No captcha response provided" } if response_token.empty?
  
  result = FriendlyCaptcha::HttpCall.new.call(
    url: options[:endpoint] || FriendlyCaptcha.config.verification_endpoint,
    body: {
      response: response_token,
      sitekey: options[:sitekey] || FriendlyCaptcha.config.site_key
    },
    headers: {
      'X-API-Key' => options[:api_key] || FriendlyCaptcha.config.api_key,
      'Content-Type' => 'application/json'
    }
  )
  
  case result
  when FriendlyCaptcha::HttpCall::Success
    status_code, response_body = result.value!
    parse_verification_response(status_code, response_body)
  when FriendlyCaptcha::HttpCall::Failure
    handle_verification_failure(result.failure)
  end
end