Module: Turnstile
- Defined in:
- lib/turnstile.rb,
lib/turnstile/helpers.rb,
lib/turnstile/railtie.rb,
lib/turnstile/version.rb,
lib/turnstile/configuration.rb,
lib/turnstile/adapters/view_methods.rb,
lib/turnstile/adapters/controller_methods.rb
Defined Under Namespace
Modules: Adapters, Helpers
Classes: Configuration, Railtie, TurnstileError, VerifyError
Constant Summary
collapse
- DEFAULT_TIMEOUT =
3
- VERSION =
"0.1.0"
Class Method Summary
collapse
Class Method Details
.action_valid?(action, expected_action) ⇒ Boolean
89
90
91
92
93
94
|
# File 'lib/turnstile.rb', line 89
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
111
112
113
114
115
116
117
|
# File 'lib/turnstile.rb', line 111
def self.api_verification(verify_hash, timeout: nil)
uri = URI.parse(configuration.verify_url)
http_instance = http_client_for(uri: uri, timeout: nil)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data verify_hash
JSON.parse(http_instance.request(request).body)
end
|
.configuration ⇒ Object
Gives access to the current Configuration.
26
27
28
|
# File 'lib/turnstile.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/turnstile.rb', line 36
def self.configure
config = configuration
yield(config)
end
|
.hostname_valid?(hostname, validation) ⇒ Boolean
79
80
81
82
83
84
85
86
87
|
# File 'lib/turnstile.rb', line 79
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
|
.http_client_for(uri:, timeout: nil) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/turnstile.rb', line 96
def self.http_client_for(uri:, 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
instance = http.new(uri.host, uri.port)
instance.read_timeout = instance.open_timeout = timeout
instance.use_ssl = true if uri.port == 443
instance
end
|
.invalid_response?(resp) ⇒ Boolean
58
59
60
|
# File 'lib/turnstile.rb', line 58
def self.invalid_response?(resp)
resp.empty? || resp.length > configuration.response_limit
end
|
.skip_env?(env) ⇒ Boolean
54
55
56
|
# File 'lib/turnstile.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
|
# File 'lib/turnstile.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])
if options[:with_reply] == true
[success, reply]
else
success
end
end
|
.with_configuration(config) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/turnstile.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
|