Class: BrandCaptcha

Inherits:
Object
  • Object
show all
Defined in:
lib/brand_captcha.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

API_HOST =
'api.pontamedia.net'
VERIFY_PATH =
'/verify.php'
CHALLENGE_PATH =
'/challenge.php'
Error =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.check_answer(remote_ip, challenge, response, extra_params = {}) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
38
# File 'lib/brand_captcha.rb', line 29

def self.check_answer remote_ip, challenge, response, extra_params={}
  raise Error.new('To use BrandCaptcha you must get an API key') if configuration.private_key.to_s.empty?
  raise Error.new('For security reasons, you must pass the remote ip to BrandCaptcha') if remote_ip.to_s.empty?
  return false if challenge.to_s.empty? or response.to_s.empty?

  params = { privatekey: configuration.private_key, remoteip: remote_ip, challenge: challenge, response: response }.merge extra_params
  uri = URI('http://' + API_HOST + VERIFY_PATH)
  res = Net::HTTP.post_form(uri, params)
  res.body =~ /true/
end

.configurationObject



12
13
14
# File 'lib/brand_captcha.rb', line 12

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



16
17
18
19
# File 'lib/brand_captcha.rb', line 16

def self.configure
  self.configuration
  yield(configuration)
end

.widget(error = nil) ⇒ Object

Raises:



21
22
23
24
25
26
27
# File 'lib/brand_captcha.rb', line 21

def self.widget error=nil
  raise Error.new('To use BrandCaptcha you must get an API Key') if configuration.public_key.to_s.empty?
  url = '//' + API_HOST + CHALLENGE_PATH
  error_part = error ? "&error=#{error}" : ""

  "<script type='text/javascript' src='#{url}?k=#{configuration.public_key}#{error_part}'></script>";
end