Class: Arcaptcha::Captcha
- Inherits:
-
Object
- Object
- Arcaptcha::Captcha
- Defined in:
- lib/arcaptcha/captcha.rb
Overview
Base class for validating and displaying captcha for Arcaptcha.
Instance Attribute Summary collapse
-
#secret_key ⇒ Object
readonly
Returns the value of attribute secret_key.
-
#site_key ⇒ Object
readonly
Returns the value of attribute site_key.
Class Method Summary collapse
-
.display_tag ⇒ Object
Print API script tag.
Instance Method Summary collapse
-
#display ⇒ Object
Print captcha HTML tag.
-
#initialize(site_key, secret_key) ⇒ Captcha
constructor
A new instance of Captcha.
-
#validate(body) ⇒ Object
Parse and validate response body.
- #verify(challenge_id) ⇒ Object
Constructor Details
#initialize(site_key, secret_key) ⇒ Captcha
Returns a new instance of Captcha.
8 9 10 11 |
# File 'lib/arcaptcha/captcha.rb', line 8 def initialize(site_key, secret_key) @site_key = site_key @secret_key = secret_key end |
Instance Attribute Details
#secret_key ⇒ Object (readonly)
Returns the value of attribute secret_key.
6 7 8 |
# File 'lib/arcaptcha/captcha.rb', line 6 def secret_key @secret_key end |
#site_key ⇒ Object (readonly)
Returns the value of attribute site_key.
6 7 8 |
# File 'lib/arcaptcha/captcha.rb', line 6 def site_key @site_key end |
Class Method Details
.display_tag ⇒ Object
Print API script tag
46 47 48 |
# File 'lib/arcaptcha/captcha.rb', line 46 def self.display_tag "<script src='https://widget.arcaptcha.ir/1/api.js' async defer></script>" end |
Instance Method Details
#display ⇒ Object
Print captcha HTML tag
41 42 43 |
# File 'lib/arcaptcha/captcha.rb', line 41 def display "<div class='arcaptcha' data-site-key='#{@site_key}'></div>" end |
#validate(body) ⇒ Object
Parse and validate response body
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/arcaptcha/captcha.rb', line 28 def validate(body) raise ArgumentError, "body cannot be empty" if body.nil? || body.empty? parsed = JSON.parse(body) if parsed.key? "success" true else false end end |
#verify(challenge_id) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/arcaptcha/captcha.rb', line 13 def verify(challenge_id) raise ArgumentError, "challenge_id cannot be empty" if challenge_id.nil? || challenge_id.empty? verify_url = "https://api.arcaptcha.ir/arcaptcha/api/verify" data = { site_key: @site_key, secret_key: @secret_key, challenge_id: challenge_id } response = Faraday.post(verify_url) do |req| req.headers["Content-Type"] = "application/json" req.body = data.to_json end validate(response.body) end |