Class: VisualCaptcha::Captcha

Inherits:
Object
  • Object
show all
Defined in:
lib/visual_captcha/captcha.rb

Instance Method Summary collapse

Constructor Details

#initialize(session, assets_path = nil, default_images = nil, default_audios = nil) ⇒ Captcha

Returns a new instance of Captcha.

Parameters:

  • session

    is the default session object

  • assets_path (defaults to: nil)

    is optional. Defaults to ‘assets’. The path is relative to /

  • default_images (defaults to: nil)

    is optional. Defaults to the array inside ./images.json. The path is relative to ./images/

  • default_audios (defaults to: nil)

    is optional. Defaults to the array inside ./audios.json. The path is relative to ./audios/



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_optionsObject



118
119
120
# File 'lib/visual_captcha/captcha.rb', line 118

def all_audio_options
  @audio_options
end

#all_image_optionsObject



114
115
116
# File 'lib/visual_captcha/captcha.rb', line 114

def all_image_options
  @image_options
end

#frontend_dataObject



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

Parameters:

  • numberOfOptions

    is optional. Defaults to 5



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(number_of_options = 5)
  @session.clear

  number_of_options = number_of_options.to_i
  number_of_options = 4 if number_of_options < 4

  images = all_image_options.sample number_of_options
  images.each do |image|
    image['value'] = SecureRandom.hex 10
  end

  @session.set 'images', images

  @session.set 'validImageOption', selected_images.sample

  @session.set 'validAudioOption', all_audio_options.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_imagesObject



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

Parameters:

  • headers

    object. used to store http headers for streaming

  • fileType

    defaults to ‘mp3’, can also be ‘ogg’



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

Parameters:

  • headers

    object. used to store http headers for streaming

  • index

    of the image in the session images array to send



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_optionObject



102
103
104
# File 'lib/visual_captcha/captcha.rb', line 102

def valid_audio_option
  @session.get 'validAudioOption'
end

#valid_image_optionObject



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