Module: Recaptcha

Defined in:
lib/recaptcha.rb,
lib/recaptcha/helpers.rb,
lib/recaptcha/railtie.rb,
lib/recaptcha/version.rb,
lib/recaptcha/configuration.rb,
lib/recaptcha/adapters/view_methods.rb,
lib/recaptcha/adapters/controller_methods.rb

Defined Under Namespace

Modules: Adapters, Helpers Classes: Configuration, Railtie, RecaptchaError, VerifyError

Constant Summary collapse

DEFAULT_TIMEOUT =
3
RESPONSE_LIMIT =
4000
VERSION =
'5.5.0'

Class Method Summary collapse

Class Method Details

.action_valid?(action, expected_action) ⇒ Boolean

Returns:

  • (Boolean)

90
91
92
93
94
95
# File 'lib/recaptcha.rb', line 90

def self.action_valid?(action, expected_action)
  case expected_action
  when nil, FalseClass then true
  else action == expected_action
  end
end

.api_verification(verify_hash, timeout: nil) ⇒ Object


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/recaptcha.rb', line 108

def self.api_verification(verify_hash, timeout: nil)
  timeout ||= DEFAULT_TIMEOUT
  http = if configuration.proxy
    proxy_server = URI.parse(configuration.proxy)
    Net::HTTP::Proxy(proxy_server.host, proxy_server.port, proxy_server.user, proxy_server.password)
  else
    Net::HTTP
  end
  query = URI.encode_www_form(verify_hash)
  uri = URI.parse(configuration.verify_url + '?' + query)
  http_instance = http.new(uri.host, uri.port)
  http_instance.read_timeout = http_instance.open_timeout = timeout
  http_instance.use_ssl = true if uri.port == 443
  request = Net::HTTP::Get.new(uri.request_uri)
  JSON.parse(http_instance.request(request).body)
end

.configurationObject

Gives access to the current Configuration.


26
27
28
# File 'lib/recaptcha.rb', line 26

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)

36
37
38
39
# File 'lib/recaptcha.rb', line 36

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

.hostname_valid?(hostname, validation) ⇒ Boolean

Returns:

  • (Boolean)

80
81
82
83
84
85
86
87
88
# File 'lib/recaptcha.rb', line 80

def self.hostname_valid?(hostname, validation)
  validation ||= configuration.hostname

  case validation
  when nil, FalseClass then true
  when String then validation == hostname
  else validation.call(hostname)
  end
end

.invalid_response?(resp) ⇒ Boolean

Returns:

  • (Boolean)

58
59
60
# File 'lib/recaptcha.rb', line 58

def self.invalid_response?(resp)
  resp.empty? || resp.length > RESPONSE_LIMIT
end

.score_above_threshold?(score, minimum_score) ⇒ Boolean

Returns true iff score is greater or equal to (>=) minimum_score, or if no minimum_score was specified

Returns:

  • (Boolean)

98
99
100
101
102
103
104
105
106
# File 'lib/recaptcha.rb', line 98

def self.score_above_threshold?(score, minimum_score)
  return true if minimum_score.nil?
  return false if score.nil?

  case minimum_score
  when nil, FalseClass then true
  else score >= minimum_score
  end
end

.skip_env?(env) ⇒ Boolean

Returns:

  • (Boolean)

54
55
56
# File 'lib/recaptcha.rb', line 54

def self.skip_env?(env)
  configuration.skip_verify_env.include?(env || configuration.default_env)
end

.verify_via_api_call(response, options) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/recaptcha.rb', line 62

def self.verify_via_api_call(response, options)
  secret_key = options.fetch(:secret_key) { configuration.secret_key! }
  verify_hash = { 'secret' => secret_key, 'response' => response }
  verify_hash['remoteip'] = options[:remote_ip] if options.key?(:remote_ip)

  reply = api_verification(verify_hash, timeout: options[:timeout])
  success = reply['success'].to_s == 'true' &&
    hostname_valid?(reply['hostname'], options[:hostname]) &&
    action_valid?(reply['action'], options[:action]) &&
    score_above_threshold?(reply['score'], options[:minimum_score])

  if options[:with_reply] == true
    return success, reply
  else
    return success
  end
end

.with_configuration(config) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/recaptcha.rb', line 41

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