catptcha 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/MIT-LICENSE +20 -0
- data/README +28 -0
- data/Rakefile +23 -0
- data/catptcha.gemspec +17 -0
- data/generators/catptcha_migration/catptcha_migration_generator.rb +7 -0
- data/generators/catptcha_migration/templates/migration.rb +21 -0
- data/install.rb +1 -0
- data/lib/catptcha/photo.rb +5 -0
- data/lib/catptcha/photo_group.rb +65 -0
- data/lib/catptcha/seed.rb +29 -0
- data/lib/catptcha.rb +19 -0
- data/lib/tasks/catptcha.rake +7 -0
- data/rails/init.rb +0 -0
- data/uninstall.rb +1 -0
- metadata +111 -0
data/.gitignore
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 ENTP
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# Catptcha
|
2
|
+
|
3
|
+
A visual captcha for web sites. The user is shown a grid of 9 images and is
|
4
|
+
asked to click the kittens. Distributed in the form of a Rails 2.3 plugin.
|
5
|
+
|
6
|
+
This is based on a concept from [KittenAuth](http://thepcspy.com/kittenauth/).
|
7
|
+
|
8
|
+
This is still experimental and incomplete. Don't use it.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add the gem to your Gemfile and optionally require the rake task.
|
13
|
+
|
14
|
+
load 'tasks/catptcha.rake'
|
15
|
+
|
16
|
+
Put your flickr keys in config/flickr.yml, see flickr.example.yml.
|
17
|
+
|
18
|
+
Generate the migration:
|
19
|
+
|
20
|
+
./script/generate catptcha_migration add_catptcha_tables
|
21
|
+
|
22
|
+
Seed the images:
|
23
|
+
|
24
|
+
rake catptcha:seed
|
25
|
+
|
26
|
+
## Author
|
27
|
+
|
28
|
+
Copyright (c) 2012 ENTP, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rdoc/task'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the catptcha plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the catptcha plugin.'
|
17
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'Catptcha'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
data/catptcha.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
$:.unshift File.expand_path("./lib")
|
2
|
+
require 'catptcha'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "catptcha"
|
6
|
+
s.version = Catptcha::VERSION
|
7
|
+
s.summary = 'Visual catptcha plugin for Rails 2.3'
|
8
|
+
s.description = <<-EOD
|
9
|
+
Choose the kitten!
|
10
|
+
EOD
|
11
|
+
s.add_dependency "commonthread-flickr_fu", "0.3.0"
|
12
|
+
s.add_dependency "activerecord", "~>2.3"
|
13
|
+
s.authors = ['Zack Hobson']
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- test/*`.split("\n")
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class <%= class_name %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :catptcha_photos do |t|
|
4
|
+
t.belongs_to :photo_group
|
5
|
+
t.string :url
|
6
|
+
t.index :catptcha_group_id
|
7
|
+
end
|
8
|
+
create_table :catptcha_photo_groups do |t|
|
9
|
+
t.string :name
|
10
|
+
t.string :flickr_group_id
|
11
|
+
t.timestamp :photos_updated_at
|
12
|
+
t.integer :photos_count
|
13
|
+
t.boolean :kittens
|
14
|
+
t.index :kittens
|
15
|
+
end
|
16
|
+
end
|
17
|
+
def self.down
|
18
|
+
drop_table :catptcha_photos
|
19
|
+
drop_table :catptcha_photo_groups
|
20
|
+
end
|
21
|
+
end
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# * flickr_group_id
|
2
|
+
# * photos_updated_at
|
3
|
+
# * photos_count
|
4
|
+
class Catptcha::PhotoGroup < ActiveRecord::Base
|
5
|
+
set_table_name 'catptcha_photo_groups'
|
6
|
+
|
7
|
+
validates_uniqueness_of :name
|
8
|
+
validates_uniqueness_of :flickr_group_id
|
9
|
+
|
10
|
+
has_many :photos, :class_name => 'Catptcha::Photo'
|
11
|
+
named_scope :kittens, :conditions => {:kittens => true}
|
12
|
+
named_scope :not_kittens, :conditions => {:kittens => false}
|
13
|
+
|
14
|
+
def self.photos_count
|
15
|
+
all.inject(0) {|a,g| a += (g.photos_count || 0) }
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.random count
|
19
|
+
# TODO use a single query
|
20
|
+
results = []
|
21
|
+
count.times do
|
22
|
+
conditions = ['photo_group_id in (?)', all.map(&:id)]
|
23
|
+
if results.any?
|
24
|
+
conditions.first << ' and catptcha_photos.id not in (?)'
|
25
|
+
conditions << results.map(&:id)
|
26
|
+
end
|
27
|
+
results << Photo.first(
|
28
|
+
:conditions => conditions,
|
29
|
+
:offset => rand(photos_count).to_i
|
30
|
+
)
|
31
|
+
end
|
32
|
+
results
|
33
|
+
end
|
34
|
+
|
35
|
+
def update_images flickr
|
36
|
+
get_photos(flickr) do |photo|
|
37
|
+
photos.create(:url => photo.url(:square))
|
38
|
+
end
|
39
|
+
update_attributes(:photos_updated_at => DateTime.now.utc)
|
40
|
+
end
|
41
|
+
|
42
|
+
# fetches up to 2000 photos
|
43
|
+
def get_photos flickr
|
44
|
+
params = {
|
45
|
+
:group_id => flickr_group_id, :safe_search => 1, :content_type => 1,
|
46
|
+
:per_page => 500, :page => 1
|
47
|
+
}
|
48
|
+
params[:min_upload_date] = photos_updated_at if photos_updated_at
|
49
|
+
|
50
|
+
total_pages = nil
|
51
|
+
loop do
|
52
|
+
result = flickr.photos.search(params)
|
53
|
+
result.each do |photo|
|
54
|
+
yield photo
|
55
|
+
end
|
56
|
+
if total_pages.nil?
|
57
|
+
total_pages = [result.pages, 4].min
|
58
|
+
else
|
59
|
+
params[:page] += 1
|
60
|
+
break if params[:page] > total_pages
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'flickr_fu'
|
2
|
+
|
3
|
+
module Catptcha::Seed
|
4
|
+
SEED_GROUP_KITTENS = {
|
5
|
+
:cutekittens => '99442622@N00' # http://www.flickr.com/groups/cutekittens/
|
6
|
+
}
|
7
|
+
SEED_GROUP_NOTKITTENS = {
|
8
|
+
:birdsphotos => '670107@N23', # http://www.flickr.com/groups/birdsphotos/
|
9
|
+
:lovehorses => '44159230@N00' # http://www.flickr.com/groups/lovehorses/
|
10
|
+
}
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def flickr
|
14
|
+
@flickr ||= Flickr.new(Rails.root.join("config/flickr.yml").to_s)
|
15
|
+
end
|
16
|
+
|
17
|
+
def update
|
18
|
+
SEED_GROUP_KITTENS.each do |name, id|
|
19
|
+
Catptcha::PhotoGroup.create(:name => name.to_s, :flickr_group_id => id, :kittens => true)
|
20
|
+
end
|
21
|
+
SEED_GROUP_NOTKITTENS.each do |name, id|
|
22
|
+
Catptcha::PhotoGroup.create(:name => name.to_s, :flickr_group_id => id, :kittens => false)
|
23
|
+
end
|
24
|
+
Catptcha::PhotoGroup.all.each do |group|
|
25
|
+
group.update_images flickr
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/catptcha.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Catptcha
|
2
|
+
VERSION = '0.0.2'
|
3
|
+
|
4
|
+
class << self
|
5
|
+
# Get a list of 3 kitten photos
|
6
|
+
def kittens
|
7
|
+
PhotoGroup.kittens.random(3)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Get a list of 6 non-kitten photos
|
11
|
+
def not_kittens
|
12
|
+
PhotoGroup.not_kittens.random(6)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
autoload :Photo, 'catptcha/photo'
|
17
|
+
autoload :PhotoGroup, 'catptcha/photo_group'
|
18
|
+
autoload :Seed, 'catptcha/seed'
|
19
|
+
end
|
data/rails/init.rb
ADDED
File without changes
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: catptcha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Zack Hobson
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-05-16 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: commonthread-flickr_fu
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - "="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 19
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 3
|
33
|
+
- 0
|
34
|
+
version: 0.3.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: activerecord
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 5
|
46
|
+
segments:
|
47
|
+
- 2
|
48
|
+
- 3
|
49
|
+
version: "2.3"
|
50
|
+
type: :runtime
|
51
|
+
version_requirements: *id002
|
52
|
+
description: " Choose the kitten!\n"
|
53
|
+
email:
|
54
|
+
executables: []
|
55
|
+
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
extra_rdoc_files: []
|
59
|
+
|
60
|
+
files:
|
61
|
+
- .gitignore
|
62
|
+
- MIT-LICENSE
|
63
|
+
- README
|
64
|
+
- Rakefile
|
65
|
+
- catptcha.gemspec
|
66
|
+
- generators/catptcha_migration/catptcha_migration_generator.rb
|
67
|
+
- generators/catptcha_migration/templates/migration.rb
|
68
|
+
- install.rb
|
69
|
+
- lib/catptcha.rb
|
70
|
+
- lib/catptcha/photo.rb
|
71
|
+
- lib/catptcha/photo_group.rb
|
72
|
+
- lib/catptcha/seed.rb
|
73
|
+
- lib/tasks/catptcha.rake
|
74
|
+
- rails/init.rb
|
75
|
+
- uninstall.rb
|
76
|
+
has_rdoc: true
|
77
|
+
homepage:
|
78
|
+
licenses: []
|
79
|
+
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
hash: 3
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
hash: 3
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
version: "0"
|
103
|
+
requirements: []
|
104
|
+
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 1.6.2
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: Visual catptcha plugin for Rails 2.3
|
110
|
+
test_files: []
|
111
|
+
|