Module: Recaptcha

Defined in:
lib/recaptcha.rb,
lib/recaptcha/verify.rb,
lib/recaptcha/version.rb,
lib/recaptcha/client_helper.rb,
lib/recaptcha/configuration.rb

Defined Under Namespace

Modules: ClientHelper, Verify Classes: Configuration, RecaptchaError, VerifyError

Constant Summary collapse

CONFIG =
{
  'server_url' => 'https://www.google.com/recaptcha/api.js',
  'verify_url' => 'https://www.google.com/recaptcha/api/siteverify'
}.freeze
USE_SSL_BY_DEFAULT =
false
HANDLE_TIMEOUTS_GRACEFULLY =
true
DEFAULT_TIMEOUT =
3
VERSION =
"4.6.6".freeze

Class Method Summary collapse

Class Method Details

.configurationObject

Gives access to the current Configuration.



19
20
21
# File 'lib/recaptcha.rb', line 19

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

.configure {|config| ... } ⇒ Object

Allows easy setting of multiple configuration options. See Configuration for all available options. – The temp assignment is only used to get a nicer rdoc. Feel free to remove this hack. ++

Yields:

  • (config)


29
30
31
32
# File 'lib/recaptcha.rb', line 29

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

.get(verify_hash, options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/recaptcha.rb', line 47

def self.get(verify_hash, options)
  http = if Recaptcha.configuration.proxy
    proxy_server = URI.parse(Recaptcha.configuration.proxy)
    Net::HTTP::Proxy(proxy_server.host, proxy_server.port, proxy_server.user, proxy_server.password)
  else
    Net::HTTP
  end
  query = Rack::Utils.build_query(verify_hash)
  uri = URI.parse(Recaptcha.configuration.verify_url + '?' + query)
  http_instance = http.new(uri.host, uri.port)
  http_instance.read_timeout = http_instance.open_timeout = options[:timeout] || DEFAULT_TIMEOUT
  http_instance.use_ssl = true if uri.port == 443
  request = Net::HTTP::Get.new(uri.request_uri)
  http_instance.request(request).body
end

.i18n(key, default) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/recaptcha.rb', line 63

def self.i18n(key, default)
  if defined?(I18n)
    I18n.translate(key, :default => default)
  else
    default
  end
end

.with_configuration(config) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/recaptcha.rb', line 34

def self.with_configuration(config)
  original_config = {}

  config.each do |key, value|
    original_config[key] = configuration.send(key)
    configuration.send("#{key}=", value)
  end

  yield if block_given?
ensure
  original_config.each { |key, value| configuration.send("#{key}=", value) }
end