Module: Hcaptcha
- Defined in:
- lib/hcaptcha.rb,
lib/hcaptcha/helpers.rb,
lib/hcaptcha/railtie.rb,
lib/hcaptcha/version.rb,
lib/hcaptcha/configuration.rb,
lib/hcaptcha/adapters/view_methods.rb,
lib/hcaptcha/adapters/controller_methods.rb
Defined Under Namespace
Modules: Adapters, Helpers
Classes: Configuration, HcaptchaError, Railtie, VerifyError
Constant Summary
collapse
- DEFAULT_TIMEOUT =
3
- RESPONSE_LIMIT =
32767
- VERSION =
'7.1.0'
Class Method Summary
collapse
Class Method Details
.action_valid?(action, expected_action) ⇒ Boolean
84
85
86
87
88
89
|
# File 'lib/hcaptcha.rb', line 84
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: DEFAULT_TIMEOUT) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/hcaptcha.rb', line 102
def self.api_verification(verify_hash, 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
|
.configuration ⇒ Object
Gives access to the current Configuration.
26
27
28
|
# File 'lib/hcaptcha.rb', line 26
def self.configuration
@configuration ||= Configuration.new
end
|
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. ++
36
37
38
39
|
# File 'lib/hcaptcha.rb', line 36
def self.configure
config = configuration
yield(config)
end
|
.hostname_valid?(hostname, validation) ⇒ Boolean
74
75
76
77
78
79
80
81
82
|
# File 'lib/hcaptcha.rb', line 74
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
58
59
60
|
# File 'lib/hcaptcha.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
92
93
94
95
96
97
98
99
100
|
# File 'lib/hcaptcha.rb', line 92
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
54
55
56
|
# File 'lib/hcaptcha.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
|
# File 'lib/hcaptcha.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])
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])
end
|
.with_configuration(config) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/hcaptcha.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
|