Module: AdCopy::ControllerMethods
- Included in:
- ActionController::Base
- Defined in:
- lib/controller_methods.rb
Overview
Methods within this module will be included in ActionController::Base
Instance Method Summary collapse
-
#verify_adcopy_puzzle(options = {}) ⇒ Object
Sends a POST request to the AdCopy server in order to verify the user’s input.
Instance Method Details
#verify_adcopy_puzzle(options = {}) ⇒ Object
Sends a POST request to the AdCopy server in order to verify the user’s input. Returns true if the user’s input is valid, false otherwise
Options:
:validate_response-
Whether or not the AdCopy authenticator should be used to validate the server’s response. If this is set to
trueand the validation fails, an AdCopyError is raised. :timeout-
The amount of time in seconds before the request should timeout. If the request times out, a Timeout::Error is raised.
:model-
An ActiveRecord model. If verification fails, an error will be added to this model.
:error_message-
A custom error message (to be used in conjunction with
:model) to be used if verification fails.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/controller_methods.rb', line 14 def verify_adcopy_puzzle( = {}) AdCopy::check_for_keys! = { :validate_response => true, :timeout => 5, :model => nil, :error_message => nil }.merge() #Send POST to AdCopy response = nil Timeout::timeout([:timeout]) do response = Net::HTTP.post_form URI.parse("#{AdCopy::VERIFY_SERVER}/papi/verify"), { "privatekey" => AdCopy::CONFIG['V_KEY'], "challenge" => params[:adcopy_challenge], "response" => params[:adcopy_response], "remoteip" => request.remote_ip } end answer, error, authenticator = response.body.split("\n") #validate the response if [:validate_response] && authenticator != Digest::SHA1.hexdigest("#{answer}#{params[:adcopy_challenge]}#{AdCopy::CONFIG['H_KEY']}") raise AdCopyError, "AdCopy Error: Unable to Validate Response" end if answer.downcase == "true" return true else #Add error to the model if [:model] [:model].valid? [:model].errors.add_to_base [:error_message] || "AdCopy Puzzle: #{error}" end return false end end |