Module: SolveMedia::ControllerMethods

Defined in:
lib/solvemedia/controller_methods.rb

Instance Method Summary collapse

Instance Method Details

#verify_solvemedia_puzzle(options = {}) ⇒ Boolean

Controller method to verify a Solve Media puzzle. Assumes a form with the puzzle is being processed by the calling method.

Calls SolveMedia.verify internally.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :validate_response (Boolean) — default: true

    Validate the response from the Solve Media server

  • :timeout (Integer) — default: 5

    Time in seconds to wait for a response form the Solve Media server

  • :model (Object<ActiveRecord::Base>)

    ActiveRecord model object to which error is added

  • :error_message (String)

    Custom error message to add to the model. Does nothing if :model is not present

Returns:

  • (Boolean)

    Was the user’s answer correct?

Raises:

  • (AdCopyError)

    if validate_response is true and the response cannot be verified

  • (Timeout::Error)

    if the request to the verification server takes longer than expected



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/solvemedia/controller_methods.rb', line 26

def verify_solvemedia_puzzle(options={})
  ver_options = { :validate_response => options[:validate_response] || true,
                  :timeout           => options[:timeout] || 5
  }
  verified = SolveMedia.verify(params[:adcopy_challenge], params[:adcopy_response], VKEY, HKEY, request.remote_ip)
  if options[:model] && !verified
    options[:model].valid?
    options[:model].errors.add(:base, options[:error_message] || "Please fill out the Solve Media puzzle")
  end

  return verified
end