Class: VisualCaptcha::Captcha
- Inherits:
-
Object
- Object
- VisualCaptcha::Captcha
- Defined in:
- lib/visual_captcha/captcha.rb
Instance Method Summary collapse
- #all_audio_options ⇒ Object
- #all_image_options ⇒ Object
- #frontend_data ⇒ Object
-
#generate(number_of_options = 5) ⇒ Object
Generate a new valid option.
-
#initialize(session, assets_path = nil, default_images = nil, default_audios = nil) ⇒ Captcha
constructor
A new instance of Captcha.
- #selected_image_at_index(index) ⇒ Object
- #selected_images ⇒ Object
-
#stream_audio(headers, file_type = 'mp3') ⇒ Object
Stream audio file.
-
#stream_image(headers, index, is_retina) ⇒ Object
Stream image file given an index in the session visualCaptcha images array.
- #valid_audio_option ⇒ Object
- #valid_image_option ⇒ Object
- #validate_audio(sent_option) ⇒ Object
- #validate_image(sent_option) ⇒ Object
Constructor Details
#initialize(session, assets_path = nil, default_images = nil, default_audios = nil) ⇒ Captcha
Returns a new instance of Captcha.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/visual_captcha/captcha.rb', line 9 def initialize(session, assets_path = nil, default_images = nil, default_audios = nil) @session = session @assets_path = assets_path @assets_path ||= File.join VisualCaptcha.root, 'assets' @image_options = default_images @image_options ||= JSON.load File.read("#{@assets_path}/images.json") @audio_options = default_audios @audio_options ||= JSON.load File.read("#{@assets_path}/audios.json") end |
Instance Method Details
#all_audio_options ⇒ Object
118 119 120 |
# File 'lib/visual_captcha/captcha.rb', line 118 def @audio_options end |
#all_image_options ⇒ Object
114 115 116 |
# File 'lib/visual_captcha/captcha.rb', line 114 def @image_options end |
#frontend_data ⇒ Object
94 95 96 |
# File 'lib/visual_captcha/captcha.rb', line 94 def frontend_data @session.get 'frontendData' end |
#generate(number_of_options = 5) ⇒ Object
Generate a new valid option
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/visual_captcha/captcha.rb', line 24 def generate( = 5) @session.clear = .to_i = 4 if < 4 images = .sample images.each do |image| image['value'] = SecureRandom.hex 10 end @session.set 'images', images @session.set 'validImageOption', selected_images.sample @session.set 'validAudioOption', .sample @session.set 'frontendData', { 'values' => selected_images.map { |i| i['value'] }, 'imageName' => valid_image_option['name'], 'imageFieldName' => SecureRandom.hex(10), 'audioFieldName' => SecureRandom.hex(10) } end |
#selected_image_at_index(index) ⇒ Object
89 90 91 92 |
# File 'lib/visual_captcha/captcha.rb', line 89 def selected_image_at_index(index) images = selected_images images[index] unless images.nil? end |
#selected_images ⇒ Object
85 86 87 |
# File 'lib/visual_captcha/captcha.rb', line 85 def selected_images @session.get 'images' end |
#stream_audio(headers, file_type = 'mp3') ⇒ Object
Stream audio file
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/visual_captcha/captcha.rb', line 52 def stream_audio(headers, file_type = 'mp3') audio_option = valid_audio_option return nil if audio_option.nil? audio_file_name = "#{audio_option['path']}" audio_file_path = "#{@assets_path}/audios/#{audio_file_name}" if file_type == 'ogg' audio_file_path.gsub! /\.mp3/i, '.ogg' content_type = 'application/ogg' else content_type = 'audio/mpeg' end read_file headers, audio_file_path, content_type end |
#stream_image(headers, index, is_retina) ⇒ Object
Stream image file given an index in the session visualCaptcha images array
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/visual_captcha/captcha.rb', line 74 def stream_image(headers, index, is_retina) image_option = selected_image_at_index index.to_i return nil if image_option.nil? image_file_name = "#{image_option['path']}" image_file_name.gsub! /\.png/i, '@2x.png' if (is_retina.to_i >= 1) image_file_path = "#{@assets_path}/images/#{image_file_name}" read_file headers, image_file_path, 'image/png' end |
#valid_audio_option ⇒ Object
102 103 104 |
# File 'lib/visual_captcha/captcha.rb', line 102 def valid_audio_option @session.get 'validAudioOption' end |
#valid_image_option ⇒ Object
98 99 100 |
# File 'lib/visual_captcha/captcha.rb', line 98 def valid_image_option @session.get 'validImageOption' end |
#validate_audio(sent_option) ⇒ Object
110 111 112 |
# File 'lib/visual_captcha/captcha.rb', line 110 def validate_audio(sent_option) sent_option == valid_audio_option['value'] end |
#validate_image(sent_option) ⇒ Object
106 107 108 |
# File 'lib/visual_captcha/captcha.rb', line 106 def validate_image(sent_option) sent_option == valid_image_option['value'] end |