Module: Rack::Picatcha::Helpers
- Defined in:
- lib/rack/picatcha/helpers.rb
Constant Summary collapse
- DEFAULT =
{ :height => 300, :width => 500, :row => 3, :cols => 5, :format => '2', :style => '#2a1f19', :link => '1', :img_size => '75', :noise_level => '2', :noise_type => '0', :lang => 'en', :lang_override => '0', :ssl => false }
Instance Method Summary collapse
-
#picatcha_tag(type = :noscript, options = {}) ⇒ Object
Helper method to output a picatcha form.
-
#picatcha_valid?(options = {}) ⇒ Boolean
Helper to return whether the picatcha was accepted.
Instance Method Details
#picatcha_tag(type = :noscript, options = {}) ⇒ Object
Helper method to output a picatcha form. Some of the available types you can have are:
:challenge - Returns a javascript picatcha form :noscript - Return a non-javascript picatcha form :ajax - Return a ajax picatcha form
You also have a few available options:
For :challenge and :noscript
:public_key - Set the public key. Overrides the key set in Middleware option
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rack/picatcha/helpers.rb', line 34 def picatcha_tag(type= :noscript, ={}) # Default options = DEFAULT.merge() [:public_key] ||= Rack::Picatcha.public_key path = [:ssl] ? Rack::Picatcha::API_SECURE_URL : Rack::Picatcha::API_URL raise PicatchaError, "No public key specified." unless [:public_key].to_s != '' error = [:error] ||= (defined? flash ? flash[:picatcha_error] : "") html = "" elm_id = "picatcha" html << <<-EOS <script type="text/javascript" src="#{path}/static/client/jquery.min.js"></script> <script type="text/javascript" src="#{path}/static/client/picatcha.js"></script> <link href="#{path}/static/client/picatcha.css" rel="stylesheet" type="text/css"> <script>Picatcha.PUBLIC_KEY="#{[:public_key]}"; Picatcha.setCustomization({"format":"#{[:format]}","color":"#{[:style]}","link":"#{[:link]}","image_size":"#{[:img_size]}","lang":"#{[:lang]}","langOverride":"#{[:lang_override]}","noise_level":"#{[:noise_level]}","noise_type":"#{[:noise_type]}"}); window.onload=function(){Picatcha.create("#{elm_id}", {});};</script> <div id="#{elm_id}"></div> EOS if [:display] %{<script type="text/javascript"> var PicatchaOptions = #{[:display].to_json}; </script>}.gsub(/^ +/, '') else '' end + html end |
#picatcha_valid?(options = {}) ⇒ Boolean
Helper to return whether the picatcha was accepted.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rack/picatcha/helpers.rb', line 65 def picatcha_valid?(={}) # LOGGER.debug "def picatchs_valid? options=#{options}" picatchadata = [:picatcha] || "" private_key = [:private_key] || Rack::Picatcha.private_key if picatchadata.to_s == "" then test = Rack::Picatcha.test_mode test.nil? ? request.env['picatcha.valid'] : test else retval, msg = Rack::Picatcha.verify_picatcha({ :private_key => private_key, :ipaddr => request.ip, :picatcha => picatchadata }) return retval end end |