Last active
August 29, 2015 14:11
-
-
Save angelodlfrtr/4cafecdbc91908fd7870 to your computer and use it in GitHub Desktop.
Simple class for google recaptcha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rest_client' | |
require 'json' | |
module Google | |
class ReCaptcha | |
attr_accessor :endpoint, :secret, :response, :result | |
def initialize args={} | |
load_config(args) | |
verify | |
end | |
def verify | |
prms = { | |
:secret => self.secret, | |
:response => self.response | |
} | |
response = RestClient.get self.endpoint, { params: prms } | |
self.result = JSON.parse(response)['success'] | |
end | |
private | |
def load_config args | |
[:secret, :response].each do |e| | |
raise ArgumentError, 'Invalid params' unless args.has_key?(e) | |
self.send("#{e}=", args[e]) | |
end | |
self.endpoint = 'https://www.google.com/recaptcha/api/siteverify' | |
end | |
end | |
end | |
response = new Google::ReCaptcha({ | |
:response => 'lalalala', | |
:secret => 'lililili' | |
}) | |
if response.result | |
puts "Yeppeeee" | |
else | |
raise StandardError, 'Standard wtf' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment