simple_captcha2 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f4023b32b748c4923af4ffa42624e3f3c325b23
4
- data.tar.gz: cd25c309a52d752a882c4f7438ee62cf44ac1791
3
+ metadata.gz: eaae4d5f2ddd3caa10ef19b27e3792fd10dfff15
4
+ data.tar.gz: 95060e2be4aec6e2a3cde73f811fdca235aacd1b
5
5
  SHA512:
6
- metadata.gz: 0294813a816747ab51b76fd753bd7c0be3cf0492889e1bddc614401d30649d21900ed2f4754940d61a4e6b9e8c92f6947108bb22624023a0aff3c3cf972a9c8e
7
- data.tar.gz: ae404e7cc38bfd4b7cefa3dc6b14d811ee786f447296c0180a62df899b0063b3bf7d4f840187caef67ff75d7e6415f6d02a5018c967af5f313b3a830b2d43013
6
+ metadata.gz: 41c2f4d97a08574b12fd6ab816ac180d69f0c2476f4eb31527082fe3b47cbec4aff724b399aa683e0682f9287b198f47bdadf21354cc56ea7480038c5c58f50c
7
+ data.tar.gz: 6c049534725511c94c2fa734265bd143dbc833e577f51004bc895956889ad46f9015b495ee6b850c76b0d383e332efe82d5f3aa1b1e8f761f2fb4576218fcd55
data/README.md CHANGED
@@ -153,6 +153,7 @@ fill_in 'captcha', with: SimpleCaptcha::SimpleCaptchaData.first.value
153
153
  * ``:label`` - provides the custom text b/w the image and the text field, the default is "type the code from the image"
154
154
  * ``:object`` - the name of the object of the model class, to implement the model based captcha.
155
155
  * ``:code_type`` - return numeric only if set to 'numeric'
156
+ * ``:multiple`` - allow to use the same captcha in multiple forms in one page. True for the first appaerance and false for the rest.
156
157
 
157
158
  ###Global options
158
159
 
@@ -1,4 +1,4 @@
1
- <style type="text/CSS">
1
+ <style type="text/css">
2
2
  .simple_captcha{border: 1px solid #ccc; padding: 5px !important;}
3
3
  .simple_captcha,
4
4
  .simple_captcha div{display: table;}
@@ -26,12 +26,16 @@
26
26
  <div class='simple_captcha_image'>
27
27
  <%%= simple_captcha_options[:image] %>
28
28
  </div>
29
-
29
+
30
30
  <div class='simple_captcha_field'>
31
31
  <%%= simple_captcha_options[:field] %>
32
32
  </div>
33
-
33
+
34
34
  <div class='simple_captcha_label'>
35
35
  <%%= simple_captcha_options[:label] %>
36
36
  </div>
37
+
38
+ <div class='simple_captcha_refresh_button'>
39
+ <%%= simple_captcha_options[:refresh_button] %>
40
+ </div>
37
41
  </div>
@@ -14,8 +14,9 @@ module SimpleCaptcha
14
14
  autoload :FormBuilder, 'simple_captcha/form_builder'
15
15
  autoload :CustomFormBuilder, 'simple_captcha/formtastic'
16
16
 
17
+ autoload :ModelHelpers, 'simple_captcha/model_helpers'
18
+
17
19
  if defined?(ActiveRecord)
18
- autoload :ModelHelpers, 'simple_captcha/active_record'
19
20
  autoload :SimpleCaptchaData, 'simple_captcha/simple_captcha_data'
20
21
  else
21
22
  autoload :SimpleCaptchaData, 'simple_captcha/simple_captcha_data_mongoid.rb'
@@ -2,47 +2,53 @@
2
2
  module SimpleCaptcha
3
3
  class Middleware
4
4
  include SimpleCaptcha::ImageHelpers
5
-
5
+ include SimpleCaptcha::ViewHelper
6
+
6
7
  DEFAULT_SEND_FILE_OPTIONS = {
7
8
  :type => 'application/octet-stream'.freeze,
8
9
  :disposition => 'attachment'.freeze,
9
10
  }.freeze
10
-
11
+
11
12
  def initialize(app, options={})
12
13
  @app = app
13
14
  self
14
15
  end
15
-
16
+
16
17
  def call(env) # :nodoc:
17
18
  if env["REQUEST_METHOD"] == "GET" && captcha_path?(env['PATH_INFO'])
18
- make_image(env)
19
+ request = Rack::Request.new(env)
20
+ if request.params.present? && request.params['code'].present?
21
+ make_image(env)
22
+ else
23
+ refresh_code(env)
24
+ end
19
25
  else
20
26
  @app.call(env)
21
27
  end
22
28
  end
23
-
29
+
24
30
  protected
25
31
  def make_image(env, headers = {}, status = 404)
26
32
  request = Rack::Request.new(env)
27
33
  code = request.params["code"]
28
34
  body = []
29
-
30
- if !code.blank? && Utils::simple_captcha_value(code)
35
+
36
+ if Utils::simple_captcha_value(code)
31
37
  #status, headers, body = @app.call(env)
32
38
  #status = 200
33
39
  #body = generate_simple_captcha_image(code)
34
40
  #headers['Content-Type'] = 'image/jpeg'
35
-
36
- return send_file(generate_simple_captcha_image(code), :type => 'image/jpeg', :disposition => 'inline', :filename => 'simple_captcha.jpg')
41
+
42
+ send_file(generate_simple_captcha_image(code), :type => 'image/jpeg', :disposition => 'inline', :filename => 'simple_captcha.jpg')
43
+ else
44
+ [status, headers, body]
37
45
  end
38
-
39
- [status, headers, body]
40
46
  end
41
-
47
+
42
48
  def captcha_path?(request_path)
43
49
  request_path.include?('/simple_captcha')
44
50
  end
45
-
51
+
46
52
  def send_file(path, options = {})
47
53
  raise MissingFile, "Cannot read file #{path}" unless File.file?(path) and File.readable?(path)
48
54
 
@@ -51,8 +57,28 @@ module SimpleCaptcha
51
57
  status = options[:status] || 200
52
58
  headers = {"Content-Disposition" => "#{options[:disposition]}; filename='#{options[:filename]}'", "Content-Type" => options[:type], 'Content-Transfer-Encoding' => 'binary', 'Cache-Control' => 'private'}
53
59
  response_body = File.open(path, "rb")
54
-
60
+
55
61
  [status, headers, response_body]
56
62
  end
63
+
64
+ def refresh_code(env)
65
+ request = Rack::Request.new(env)
66
+
67
+ request.session.delete :captcha
68
+ key = simple_captcha_key(nil, request)
69
+ options = {}
70
+ options[:field_value] = set_simple_captcha_data(key, options)
71
+ url = simple_captcha_image_url(key, options)
72
+
73
+ status = 200
74
+ id = request.params['id']
75
+
76
+ body = %Q{
77
+ $("##{id}").attr('src', '#{url}');
78
+ $("#captcha_key").attr('value', '#{key}');
79
+ }
80
+ headers = {'Content-Type' => 'text/javascript; charset=utf-8', "Content-Disposition" => "inline; filename='captcha.js'", "Content-Length" => body.length.to_s}
81
+ [status, headers, [body]]
82
+ end
57
83
  end
58
84
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleCaptcha
2
- VERSION = "0.3.1".freeze
2
+ VERSION = "0.3.2".freeze
3
3
  end
@@ -44,14 +44,21 @@ module SimpleCaptcha #:nodoc
44
44
  # All Feedbacks/CommentS/Issues/Queries are welcome.
45
45
  def show_simple_captcha(options={})
46
46
  key = simple_captcha_key(options[:object])
47
- options[:field_value] = set_simple_captcha_data(key, options)
48
-
47
+ if options[:multiple] === false
48
+ # It's not the first captcha, we only need to return the key
49
+ options[:field_value] = key
50
+ else
51
+ # It's the first captcha in the page, we generate a new key
52
+ options[:field_value] = set_simple_captcha_data(key, options)
53
+ end
54
+
49
55
  defaults = {
50
- :image => simple_captcha_image(key, options),
51
- :label => options[:label] || I18n.t('simple_captcha.label'),
52
- :field => simple_captcha_field(options)
53
- }
54
-
56
+ :image => simple_captcha_image(key, options),
57
+ :label => I18n.t('simple_captcha.label'),
58
+ :field => simple_captcha_field(options),
59
+ :refresh_button => simple_captcha_refresh_button(options),
60
+ }.merge(options)
61
+
55
62
  render :partial => 'simple_captcha/simple_captcha', :locals => { :simple_captcha_options => defaults }
56
63
  end
57
64
 
@@ -60,18 +67,27 @@ module SimpleCaptcha #:nodoc
60
67
  def simple_captcha_image(simple_captcha_key, options = {})
61
68
  defaults = {}
62
69
  defaults[:time] = options[:time] || Time.now.to_i
63
-
70
+
64
71
  query = defaults.collect{ |key, value| "#{key}=#{value}" }.join('&')
65
72
  url = "#{ENV['RAILS_RELATIVE_URL_ROOT']}/simple_captcha?code=#{simple_captcha_key}&#{query}"
66
-
67
- tag('img', :src => url, :alt => 'captcha')
73
+
74
+ id = simple_captcha_image_id(options)
75
+ tag('img', :src => url, :alt => 'captcha', :id => id)
68
76
  end
69
-
77
+
78
+ def simple_captcha_image_url(simple_captcha_key, options = {})
79
+ defaults = {}
80
+ defaults[:time] = options[:time] || Time.now.to_i
81
+
82
+ query = defaults.collect{ |key, value| "#{key}=#{value}" }.join('&')
83
+ "#{ENV['RAILS_RELATIVE_URL_ROOT']}/simple_captcha?code=#{simple_captcha_key}&#{query}"
84
+ end
85
+
70
86
  def simple_captcha_field(options={})
71
87
  html = {:autocomplete => 'off', :required => 'required'}
72
88
  html.merge!(options[:input_html] || {})
73
89
  html[:placeholder] = options[:placeholder] || I18n.t('simple_captcha.placeholder')
74
-
90
+
75
91
  if options[:object]
76
92
  text_field(options[:object], :captcha, html.merge(:value => '')) +
77
93
  hidden_field(options[:object], :captcha_key, {:value => options[:field_value]})
@@ -81,35 +97,49 @@ module SimpleCaptcha #:nodoc
81
97
  end
82
98
  end
83
99
 
100
+ def simple_captcha_refresh_button(options={})
101
+ html = {remote: true}
102
+ html.merge!(options[:refresh_button_html] || {})
103
+
104
+ text = options[:refresh_button_text] || I18n.t('simple_captcha.refresh_button_text', default: 'Refresh')
105
+
106
+ link_to(text, "#{ENV['RAILS_RELATIVE_URL_ROOT']}/simple_captcha?id=#{simple_captcha_image_id(options)}", html)
107
+ end
108
+
109
+ def simple_captcha_image_id(options={})
110
+ "simple_captcha-#{options[:field_value][0..10]}"
111
+ end
112
+
84
113
  def set_simple_captcha_data(key, options={})
85
114
  code_type = options[:code_type]
86
-
115
+
87
116
  value = generate_simple_captcha_data(code_type)
88
117
  data = SimpleCaptcha::SimpleCaptchaData.get_data(key)
89
118
  data.value = value
90
119
  data.save
91
120
  key
92
121
  end
93
-
122
+
94
123
  def generate_simple_captcha_data(code)
95
124
  value = ''
96
-
125
+
97
126
  case code
98
- when 'numeric' then
127
+ when 'numeric' then
99
128
  SimpleCaptcha.length.times{value << (48 + rand(10)).chr}
100
129
  else
101
130
  SimpleCaptcha.length.times{value << (65 + rand(26)).chr}
102
131
  end
103
-
132
+
104
133
  return value
105
134
  end
106
-
107
- def simple_captcha_key(key_name = nil)
135
+
136
+ def simple_captcha_key(key_name = nil, request = request)
137
+ local_session = request.try(:session) || session
108
138
  if key_name.nil?
109
- session[:captcha] ||= SimpleCaptcha::Utils.generate_key(session[:id].to_s, 'captcha')
139
+ local_session[:captcha] ||= SimpleCaptcha::Utils.generate_key(local_session[:id].to_s, 'captcha')
110
140
  else
111
- SimpleCaptcha::Utils.generate_key(session[:id].to_s, key_name)
141
+ SimpleCaptcha::Utils.generate_key(local_session[:id].to_s, key_name)
112
142
  end
113
- end
143
+ end
114
144
  end
115
145
  end
@@ -0,0 +1 @@
1
+ require 'simple_captcha'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_captcha2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavlo Galeta
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-05-24 00:00:00.000000000 Z
13
+ date: 2014-06-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -54,6 +54,20 @@ dependencies:
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
56
  version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: poltergeist
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
57
71
  - !ruby/object:Gem::Dependency
58
72
  name: pry
59
73
  requirement: !ruby/object:Gem::Requirement
@@ -83,18 +97,19 @@ files:
83
97
  - lib/generators/templates/migration.rb
84
98
  - lib/generators/templates/partial.erb
85
99
  - lib/simple_captcha.rb
86
- - lib/simple_captcha/active_record.rb
87
100
  - lib/simple_captcha/controller.rb
88
101
  - lib/simple_captcha/engine.rb
89
102
  - lib/simple_captcha/form_builder.rb
90
103
  - lib/simple_captcha/formtastic.rb
91
104
  - lib/simple_captcha/image.rb
92
105
  - lib/simple_captcha/middleware.rb
106
+ - lib/simple_captcha/model_helpers.rb
93
107
  - lib/simple_captcha/simple_captcha_data.rb
94
108
  - lib/simple_captcha/simple_captcha_data_mongoid.rb
95
109
  - lib/simple_captcha/utils.rb
96
110
  - lib/simple_captcha/version.rb
97
111
  - lib/simple_captcha/view.rb
112
+ - lib/simple_captcha2.rb
98
113
  homepage: http://github.com/pludoni/simple-captcha
99
114
  licenses: []
100
115
  metadata: {}