Module: Textcaptcha
- Defined in:
- lib/textcaptcha/configuration.rb,
lib/textcaptcha.rb,
lib/textcaptcha/version.rb
Overview
:nodoc:
Defined Under Namespace
Classes: Configuration
Constant Summary collapse
- URL =
The API URL for textcaptcha
'http://api.textcaptcha.com/'
- VERSION =
"0.0.2"
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the value of attribute configuration.
Class Method Summary collapse
-
.configure {|configuration| ... } ⇒ Object
Configure variables.
-
.obtain ⇒ Object
(also: get)
Obtain the question and answer(s).
-
.valid?(answer, answers) ⇒ Boolean
Returns true if the answer is valid.
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
11 12 13 |
# File 'lib/textcaptcha.rb', line 11 def configuration @configuration end |
Class Method Details
.configure {|configuration| ... } ⇒ Object
Configure variables. See Textcaptcha::Configuration
14 15 16 17 |
# File 'lib/textcaptcha.rb', line 14 def configure self.configuration ||= Configuration.new yield configuration end |
.obtain ⇒ Object Also known as: get
Obtain the question and answer(s). Returns a hash with the question and answers. The answers are MD5 hashed values to be checked after submission.
Textcaptcha.obtain # => { :question => “What’s eight - 7?”, :answers => [ “c4ca4238a0b923820dcc509a6f75849b”, “f97c5d29941bfb1b2fdab0874906ab82” ] }
22 23 24 25 26 |
# File 'lib/textcaptcha.rb', line 22 def obtain api_response = HTTParty.get(URL << self.configuration.api_key) answers = api_response['captcha']['answer'].is_a?(String) ? [ api_response['captcha']['answer'] ] : api_response['captcha']['answer'] { :question => api_response['captcha']['question'], :answers => answers } end |
.valid?(answer, answers) ⇒ Boolean
Returns true if the answer is valid.
Parameters
-
answer
- The answer provided by the user. -
answers
- The possible correct answers returned fromobtain
.
34 35 36 |
# File 'lib/textcaptcha.rb', line 34 def valid?(answer, answers) answers.include?(Digest::MD5.hexdigest(answer.to_s.lstrip.rstrip)) end |