visual_captcha 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,146 @@
1
- visualCaptcha-rubyGem
2
- =====================
1
+ [![Build Status](https://travis-ci.org/emotionLoop/visualCaptcha-rubyGem.svg?flat=true&branch=master)](https://travis-ci.org/emotionLoop/visualCaptcha-rubyGem)
2
+ [![Codacy](https://www.codacy.com/project/badge/4d1f731df8ea4dfe99b51032f92fc371)](https://www.codacy.com/app/bruno-bernardino/visualCaptcha-rubyGem)
3
+ [![Code Climate](https://codeclimate.com/github/emotionLoop/visualCaptcha-rubyGem/badges/gpa.svg)](https://codeclimate.com/github/emotionLoop/visualCaptcha-rubyGem)
4
+
5
+ # visualCaptcha-rubyGem
3
6
 
4
7
  RubyGem package for visualCaptcha's backend service
8
+
9
+
10
+ ## Installation with Gem
11
+
12
+ You need Ruby 1.9.3+ installed.
13
+ ```
14
+ gem install visual_captcha
15
+ ```
16
+
17
+ ## Run tests
18
+
19
+ You need Bundler and Rake installed and then you can run
20
+ ```
21
+ bundle install && rake
22
+ ```
23
+
24
+
25
+ ## Usage
26
+
27
+ ### Initialization
28
+
29
+ You have to initialize a session for visualCaptcha to inject the data it needs. You'll need this variable to start and verify visualCaptcha as well.
30
+
31
+ ```
32
+ @session = VisualCaptcha::Session.new session, @namespace
33
+ ```
34
+ Where:
35
+
36
+ - `@namespace` is optional. It's a string and defaults to 'visualcaptcha'. You'll need to specifically set this if you're using more than one visualCaptcha instance in the same page, so the code can identify from which one is the validation coming from.
37
+
38
+
39
+ ### Setting Routes for the front-end
40
+
41
+ You also need to set routes for `/start/:howmany`, `/image/:index`, and `/audio/:type`. These will usually look like:
42
+
43
+ ```ruby
44
+ get '/start/:how_many' do
45
+ captcha = VisualCaptcha::Captcha.new @session
46
+ captcha.generate params[:how_many]
47
+
48
+ json captcha.frontend_data
49
+ end
50
+
51
+ get '/audio/?:type?' do
52
+ type = params[:type]
53
+ type = 'mp3' if type != 'ogg'
54
+
55
+ captcha = VisualCaptcha::Captcha.new @session
56
+
57
+ if (@body = captcha.stream_audio @headers, type)
58
+ body @body
59
+ else
60
+ not_found
61
+ end
62
+ end
63
+
64
+ get '/image/:index' do
65
+ captcha = VisualCaptcha::Captcha.new @session
66
+
67
+ if (@body = captcha.stream_image @headers, params[:index], params[:retina])
68
+ body @body
69
+ else
70
+ not_found
71
+ end
72
+ end
73
+ ```
74
+
75
+ ### Validating the image/audio
76
+
77
+ Here's how it'll usually look:
78
+
79
+ ```ruby
80
+ @session = VisualCaptcha::Session.new session
81
+ captcha = VisualCaptcha::Captcha.new @session
82
+ frontend_data = captcha.frontend_data()
83
+
84
+ # If an image field name was submitted, try to validate it
85
+ if ( image_answer = params[ frontend_data[ 'imageFieldName' ] ] )
86
+ if captcha.validate_image image_answer
87
+ # Image was valid.
88
+ else
89
+ # Image was submitted, but wrong.
90
+ end
91
+ elsif ( audio_answer = params[ frontend_data[ 'audioFieldName' ] ] )
92
+ if captcha.validate_audio audio_answer.downcase
93
+ # Audio answer was valid.
94
+ else
95
+ # Audio was submitted, but wrong.
96
+ end
97
+ else
98
+ # Apparently no fields were submitted, so the captcha wasn't filled.
99
+ end
100
+ ```
101
+
102
+ ### VisualCaptcha::Session properties
103
+
104
+ - `:session`, Object — The object that will hold the session data for visualCaptcha.
105
+ - `:namespace`, String — This is private and will hold the namespace for each visualCaptcha instance. Defaults to 'visualcaptcha'.
106
+
107
+ ### VisualCaptcha::Session methods
108
+
109
+ - `initialize( :session, :namespace )` — Initialize the visualCaptcha session.
110
+ - `clear()` — Will clear the session for the current namespace.
111
+ - `get( :key )` — Will return a value for the session's `:key`.
112
+ - `set( :key, :value )` — Set the `:value` for the session's `:key`.
113
+
114
+
115
+ ### VisualCaptcha::Captcha properties
116
+
117
+ - `@session`, Object that will have a reference for the session object.
118
+ It will have .visualCaptcha.images, .visualCaptcha.audios, .visualCaptcha.validImageOption, and .visualCaptcha.validAudioOption.
119
+ - `@assets_path`, Assets path. By default, it will be './assets'
120
+ - `@image_options`, All the image options.
121
+ These can be easily overwritten or extended using addImageOptions( <Array> ), or replaceImageOptions( <Array> ). By default, they're populated using the ./images.json file
122
+ - `@audio_options`, All the audio options.
123
+ These can be easily overwritten or extended using addAudioOptions( <Array> ), or replaceAudioOptions( <Array> ). By default, they're populated using the ./audios.json file
124
+
125
+ ### VisualCaptcha::Captcha methods
126
+
127
+ You'll find more documentation on the code itself, but here's the simple list for reference.
128
+
129
+ - `initialize(session, assets_path = nil, default_images = nil, default_audios = nil)` — Initialize the visualCaptcha object.
130
+ - `generate(number_of_options = 5)` — Will generate a new valid option, within a `:numberOfOptions`.
131
+ - `stream_audio(headers, file_type = 'mp3')` — Stream audio file.
132
+ - `stream_image(headers, index, is_retina)` — Stream image file given an index in the session visualCaptcha images array.
133
+ - `frontend_data()` — Get data to be used by the frontend.
134
+ - `valid_image_option()` — Get the current validImageOption.
135
+ - `valid_audio_option()` — Get the current validAudioOption.
136
+ - `validate_image( sent_option )` — Validate the sent image value with the validImageOption.
137
+ - `validate_audio( sent_option )` — Validate the sent audio value with the validAudioOption.
138
+ - `selected_images()` — Return generated image options.
139
+ - `getAudioOption()` — Alias for getValidAudioOption.
140
+ - `all_image_options()` — Return all the image options.
141
+ - `all_audio_options()` — Return all the audio options.
142
+
143
+
144
+ ## License
145
+
146
+ MIT. Check the [LICENSE](LICENSE) file.
@@ -2,6 +2,10 @@ require 'json'
2
2
  require 'securerandom'
3
3
 
4
4
  class VisualCaptcha::Captcha
5
+ # @param session is the default session object
6
+ # @param assets_path is optional. Defaults to 'assets'. The path is relative to /
7
+ # @param default_images is optional. Defaults to the array inside ./images.json. The path is relative to ./images/
8
+ # @param default_audios is optional. Defaults to the array inside ./audios.json. The path is relative to ./audios/
5
9
  def initialize(session, assets_path = nil, default_images = nil, default_audios = nil)
6
10
  @session = session
7
11
 
@@ -15,11 +19,13 @@ class VisualCaptcha::Captcha
15
19
  @audio_options ||= JSON.load File.read("#{@assets_path}/audios.json")
16
20
  end
17
21
 
22
+ # Generate a new valid option
23
+ # @param numberOfOptions is optional. Defaults to 5
18
24
  def generate(number_of_options = 5)
19
25
  @session.clear
20
26
 
21
27
  number_of_options = number_of_options.to_i
22
- number_of_options = 2 if number_of_options < 2
28
+ number_of_options = 4 if number_of_options < 4
23
29
 
24
30
  images = all_image_options.sample number_of_options
25
31
  images.each do |image|
@@ -40,6 +46,9 @@ class VisualCaptcha::Captcha
40
46
  }
41
47
  end
42
48
 
49
+ # Stream audio file
50
+ # @param headers object. used to store http headers for streaming
51
+ # @param fileType defaults to 'mp3', can also be 'ogg'
43
52
  def stream_audio(headers, file_type = 'mp3')
44
53
  audio_option = valid_audio_option
45
54
  return nil if audio_option.nil?
@@ -58,6 +67,10 @@ class VisualCaptcha::Captcha
58
67
  read_file headers, audio_file_path, content_type
59
68
  end
60
69
 
70
+ # Stream image file given an index in the session visualCaptcha images array
71
+ # @param headers object. used to store http headers for streaming
72
+ # @param index of the image in the session images array to send
73
+ # @paran isRetina boolean. Defaults to false
61
74
  def stream_image(headers, index, is_retina)
62
75
  image_option = selected_image_at_index index.to_i
63
76
  return nil if image_option.nil?
@@ -116,6 +129,11 @@ class VisualCaptcha::Captcha
116
129
  headers['Pragma'] = 'no-cache'
117
130
  headers['Expires'] = '0'
118
131
 
119
- File.read file_path
132
+ file_contents = File.read file_path
133
+
134
+ # Add some noise randomly, so images can't be saved and matched easily by filesize or checksum
135
+ file_contents += SecureRandom.hex(SecureRandom.random_number(1500))
136
+
137
+ return file_contents
120
138
  end
121
139
  end
@@ -1,3 +1,3 @@
1
1
  module VisualCaptcha
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,69 +1,78 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visual_captcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - emotionLoop
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-01-30 00:00:00.000000000 Z
12
+ date: 2015-07-12 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: json
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - ">="
19
+ - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
21
  version: '0'
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
- - - ">="
27
+ - - ! '>='
25
28
  - !ruby/object:Gem::Version
26
29
  version: '0'
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: bundler
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
- - - "~>"
35
+ - - ~>
32
36
  - !ruby/object:Gem::Version
33
37
  version: '1.3'
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
- - - "~>"
43
+ - - ~>
39
44
  - !ruby/object:Gem::Version
40
45
  version: '1.3'
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: rake
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
- - - ">="
51
+ - - ! '>='
46
52
  - !ruby/object:Gem::Version
47
53
  version: '0'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
- - - ">="
59
+ - - ! '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  - !ruby/object:Gem::Dependency
56
63
  name: rspec
57
64
  requirement: !ruby/object:Gem::Requirement
65
+ none: false
58
66
  requirements:
59
- - - ">="
67
+ - - ! '>='
60
68
  - !ruby/object:Gem::Version
61
69
  version: '0'
62
70
  type: :development
63
71
  prerelease: false
64
72
  version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
65
74
  requirements:
66
- - - ">="
75
+ - - ! '>='
67
76
  - !ruby/object:Gem::Version
68
77
  version: '0'
69
78
  description: RubyGem package for visualCaptcha's backend service
@@ -72,7 +81,7 @@ executables: []
72
81
  extensions: []
73
82
  extra_rdoc_files: []
74
83
  files:
75
- - ".gitignore"
84
+ - .gitignore
76
85
  - Gemfile
77
86
  - LICENSE
78
87
  - README.md
@@ -204,26 +213,27 @@ files:
204
213
  homepage: http://emotionloop.com
205
214
  licenses:
206
215
  - MIT
207
- metadata: {}
208
216
  post_install_message:
209
217
  rdoc_options: []
210
218
  require_paths:
211
219
  - lib
212
220
  required_ruby_version: !ruby/object:Gem::Requirement
221
+ none: false
213
222
  requirements:
214
- - - ">="
223
+ - - ! '>='
215
224
  - !ruby/object:Gem::Version
216
225
  version: '0'
217
226
  required_rubygems_version: !ruby/object:Gem::Requirement
227
+ none: false
218
228
  requirements:
219
- - - ">="
229
+ - - ! '>='
220
230
  - !ruby/object:Gem::Version
221
231
  version: '0'
222
232
  requirements: []
223
233
  rubyforge_project:
224
- rubygems_version: 2.0.14
234
+ rubygems_version: 1.8.23
225
235
  signing_key:
226
- specification_version: 4
236
+ specification_version: 3
227
237
  summary: visualCaptcha RubyGem Package
228
238
  test_files:
229
239
  - spec/lib/visual_captcha/captcha_spec.rb
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: b8982b9d2f00d6f0b0ba6365d1ec6113b8aa5416
4
- data.tar.gz: 1cc363dd419a646bb07f998cb3bd61de35a82ea7
5
- SHA512:
6
- metadata.gz: a8df46eb4212c3ac44e56cc15a3c72bc4d53fcc37a98e1fb42859672eca3c76f90e2671b7858a310643cff8d1abc6c73a3c2a9bb81be491fec886a2551f7d50b
7
- data.tar.gz: 238135b202959da80c35f2d7d5270fc76b647dad5e0a32af07b4a6718f71091a572d2ce8f9d569621cba7593197dfb60f49fe83b389d892abbd7e6fd60889975