Module: ActsAsTextcaptcha::Textcaptcha::InstanceMethods
- Defined in:
- lib/acts_as_textcaptcha/textcaptcha.rb
Instance Method Summary collapse
-
#perform_textcaptcha? ⇒ Boolean
override this method to toggle textcaptcha spam checking altogether, default is on (true).
-
#textcaptcha ⇒ Object
generate textcaptcha question and encrypt possible spam_answers.
Instance Method Details
#perform_textcaptcha? ⇒ Boolean
override this method to toggle textcaptcha spam checking altogether, default is on (true)
65 66 67 |
# File 'lib/acts_as_textcaptcha/textcaptcha.rb', line 65 def perform_textcaptcha? true end |
#textcaptcha ⇒ Object
generate textcaptcha question and encrypt possible spam_answers
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/acts_as_textcaptcha/textcaptcha.rb', line 70 def textcaptcha show_deprecation_warning return if !perform_textcaptcha? || validate_spam_answer self.spam_answer = nil if textcaptcha_config unless BCrypt::Engine.valid_salt?(textcaptcha_config[:bcrypt_salt]) raise BCrypt::Errors::InvalidSalt.new "you must specify a valid BCrypt Salt in your acts_as_textcaptcha options, get a salt from irb/console with\nrequire 'bcrypt';BCrypt::Engine.generate_salt\n\n(Please check Gem README for more details)\n" end if textcaptcha_config[:api_key] begin uri_parser = URI.const_defined?(:Parser) ? URI::Parser.new : URI # URI.parse is deprecated in 1.9.2 response = Net::HTTP.get(uri_parser.parse("http://textcaptcha.com/api/#{textcaptcha_config[:api_key]}")) if response.empty? raise Textcaptcha::BadResponse else parse_textcaptcha_xml(response) end return rescue SocketError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Errno::ECONNREFUSED, Errno::ETIMEDOUT, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, URI::InvalidURIError, REXML::ParseException, Textcaptcha::BadResponse # rescue from these errors and continue end end # fall back to textcaptcha_config questions if they are configured correctly if textcaptcha_config[:questions] random_question = textcaptcha_config[:questions][rand(textcaptcha_config[:questions].size)].symbolize_keys! if random_question[:question] && random_question[:answers] self.spam_question = random_question[:question] self.spam_answers = encrypt_answers(random_question[:answers].split(',').map!{ |answer| md5_answer(answer) }) end end unless self.spam_question && self.spam_answers self.spam_question = 'ActsAsTextcaptcha >> no API key (or questions) set and/or the textcaptcha service is currently unavailable (answer ok to bypass)' self.spam_answers = 'ok' end end end |