Skip to content

Commit

Permalink
Add refresh button
Browse files Browse the repository at this point in the history
  • Loading branch information
freemanoid committed Nov 30, 2013
1 parent 1339465 commit cc68800
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ backward compatibility with previous versions of Rails.
* Implementation is as easy as just writing a single line in your view. "<%= show_simple_captcha %>" within the 'form' tags.
* Flexible DOM and CSS handling(There is a separate view partial for rednering SimpleCaptcha DOM elements).
* Automated removal of 1 hour old unmatched simple_captcha data.
* Refresh button to refresh captcha

==Requirements

Expand Down
4 changes: 4 additions & 0 deletions lib/generators/templates/partial.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@
<div class='simple_captcha_label'>
<%%= simple_captcha_options[:label] %>
</div>

<div class='simple_captcha_refresh_button'>
<%%= simple_captcha_options[:refresh_button] %>
</div>
</div>
40 changes: 32 additions & 8 deletions lib/simple_captcha/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
module SimpleCaptcha
class Middleware
include SimpleCaptcha::ImageHelpers

include SimpleCaptcha::ViewHelper

DEFAULT_SEND_FILE_OPTIONS = {
:type => 'application/octet-stream'.freeze,
:disposition => 'attachment'.freeze,
Expand All @@ -15,7 +16,12 @@ def initialize(app, options={})

def call(env) # :nodoc:
if env["REQUEST_METHOD"] == "GET" && captcha_path?(env['PATH_INFO'])
make_image(env)
request = Rack::Request.new(env)
if request.params.present? && request.params['code'].present?
make_image(env)
else
refresh_code(env)
end
else
@app.call(env)
end
Expand All @@ -26,17 +32,17 @@ def make_image(env, headers = {}, status = 404)
request = Rack::Request.new(env)
code = request.params["code"]
body = []
if !code.blank? && Utils::simple_captcha_value(code)

if Utils::simple_captcha_value(code)
#status, headers, body = @app.call(env)
#status = 200
#body = generate_simple_captcha_image(code)
#headers['Content-Type'] = 'image/jpeg'

return send_file(generate_simple_captcha_image(code), :type => 'image/jpeg', :disposition => 'inline', :filename => 'simple_captcha.jpg')

send_file(generate_simple_captcha_image(code), :type => 'image/jpeg', :disposition => 'inline', :filename => 'simple_captcha.jpg')
else
[status, headers, body]
end

[status, headers, body]
end

def captcha_path?(request_path)
Expand All @@ -54,5 +60,23 @@ def send_file(path, options = {})

[status, headers, response_body]
end

def refresh_code(env)
request = Rack::Request.new(env)

request.session.delete :captcha
key = simple_captcha_key(nil, request)
options = {}
options[:field_value] = set_simple_captcha_data(key, options)
url = simple_captcha_image_url(key, options)

status = 200
body = %Q{
$("#captcha_image").attr('src', '#{url}');
$("#captcha_key").attr('value', '#{key}');
}
headers = {'Content-Type' => 'text/javascript; charset=utf-8', "Content-Disposition" => "inline; filename='captcha.js'", "Content-Length" => body.length.to_s}
[status, headers, [body]]
end
end
end
35 changes: 26 additions & 9 deletions lib/simple_captcha/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def show_simple_captcha(options={})
defaults = {
:image => simple_captcha_image(key, options),
:label => options[:label] || I18n.t('simple_captcha.label'),
:field => simple_captcha_field(options)
:field => simple_captcha_field(options),
:refresh_button => simple_captcha_refresh_button(options)
}

render :partial => 'simple_captcha/simple_captcha', :locals => { :simple_captcha_options => defaults }
Expand All @@ -63,10 +64,18 @@ def simple_captcha_image(simple_captcha_key, options = {})

query = defaults.collect{ |key, value| "#{key}=#{value}" }.join('&')
url = "#{ENV['RAILS_RELATIVE_URL_ROOT']}/simple_captcha?code=#{simple_captcha_key}&#{query}"
tag('img', :src => url, :alt => 'captcha')

tag('img', :src => url, :alt => 'captcha', :id => 'captcha_image')
end


def simple_captcha_image_url(simple_captcha_key, options = {})
defaults = {}
defaults[:time] = options[:time] || Time.now.to_i

query = defaults.collect{ |key, value| "#{key}=#{value}" }.join('&')
"#{ENV['RAILS_RELATIVE_URL_ROOT']}/simple_captcha?code=#{simple_captcha_key}&#{query}"
end

def simple_captcha_field(options={})
html = {:autocomplete => 'off', :required => 'required'}
html.merge!(options[:input_html] || {})
Expand All @@ -81,6 +90,14 @@ def simple_captcha_field(options={})
end
end

def simple_captcha_refresh_button(options={})
html = {remote: true}
html.merge!(options[:refresh_button_html] || {})
text = options[:refresh_button_text] || :refresh

link_to(text, "#{ENV['RAILS_RELATIVE_URL_ROOT']}/simple_captcha", html)
end

def set_simple_captcha_data(key, options={})
code_type = options[:code_type]

Expand All @@ -103,13 +120,13 @@ def generate_simple_captcha_data(code)

return value
end
def simple_captcha_key(key_name = nil)

def simple_captcha_key(key_name = nil, request = request)
if key_name.nil?
session[:captcha] ||= SimpleCaptcha::Utils.generate_key(session[:id].to_s, 'captcha')
request.session[:captcha] ||= SimpleCaptcha::Utils.generate_key(request.session[:id].to_s, 'captcha')
else
SimpleCaptcha::Utils.generate_key(session[:id].to_s, key_name)
SimpleCaptcha::Utils.generate_key(request.session[:id].to_s, key_name)
end
end
end
end
end

0 comments on commit cc68800

Please sign in to comment.