sinatra-formkeeper 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +16 -0
- data/LICENSE.txt +22 -0
- data/README.md +44 -0
- data/Rakefile +7 -0
- data/lib/sinatra/formkeeper/version.rb +5 -0
- data/lib/sinatra/formkeeper.rb +128 -0
- data/sinatra-formkeeper.gemspec +19 -0
- data/spec/app.rb +47 -0
- data/spec/app_spec.rb +99 -0
- data/spec/messages.yaml +19 -0
- data/spec/spec_helper.rb +12 -0
- metadata +62 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in sinatra-formkeeper.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
gem "hpricot"
|
7
|
+
gem "rack"
|
8
|
+
gem "sinatra"
|
9
|
+
gem "formkeeper", "~> 0.0.4"
|
10
|
+
|
11
|
+
group :development, :test do
|
12
|
+
gem "rspec"
|
13
|
+
gem "rake"
|
14
|
+
gem "rack-test"
|
15
|
+
end
|
16
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 lyokato
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Sinatra::FormKeeper
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'sinatra-formkeeper'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install sinatra-formkeeper
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Getting Started
|
22
|
+
|
23
|
+
require 'sinatra/formkeeper'
|
24
|
+
|
25
|
+
get '/login' do
|
26
|
+
form do
|
27
|
+
filters :strip, :my_filter
|
28
|
+
field :username, :present => true, :length => 4..8
|
29
|
+
field :password, :present => true, :length => 4..8
|
30
|
+
end
|
31
|
+
if form.failed?
|
32
|
+
"login failed"
|
33
|
+
else
|
34
|
+
"login success " + form[:username] + ":" + form[:password]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
1. Fork it
|
41
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
42
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
43
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
44
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (C) 2012 Lyo Kato, <lyo.kato _at_ gmail.com>.
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
|
23
|
+
require "sinatra/formkeeper/version"
|
24
|
+
require 'formkeeper'
|
25
|
+
|
26
|
+
module Sinatra
|
27
|
+
# = Sinatra::FormKeeper
|
28
|
+
#
|
29
|
+
# Example:
|
30
|
+
#
|
31
|
+
# require 'sinatra/base'
|
32
|
+
# require 'sinatra/formkeeper'
|
33
|
+
#
|
34
|
+
# module MyApp < Sinatra::Base
|
35
|
+
#
|
36
|
+
# register Sinatra::FormKeeper
|
37
|
+
#
|
38
|
+
# def post '/login' do
|
39
|
+
#
|
40
|
+
# form do
|
41
|
+
# filters :strip
|
42
|
+
# field :username, :present => true, :length => 4..8, :ascii => true
|
43
|
+
# field :password, :present => true, :length => 4..8, :ascii => true
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# # check if validation has failed or not
|
47
|
+
# if form.failed?
|
48
|
+
# output = erb :login
|
49
|
+
# fill_in_form(output)
|
50
|
+
# else
|
51
|
+
# # you can use filtered, valid parameters through form[key]
|
52
|
+
# authenticate(form[:username], form[:password])
|
53
|
+
# ...
|
54
|
+
# redirect '/mypage'
|
55
|
+
# end
|
56
|
+
# end
|
57
|
+
# end
|
58
|
+
#
|
59
|
+
# @@ login
|
60
|
+
# <html>
|
61
|
+
# <head><title>Login</title></head>
|
62
|
+
# <body>
|
63
|
+
# <% if form.failed? %>
|
64
|
+
# <p>Found invalid input, check it and try again.</p>
|
65
|
+
# <% end %>
|
66
|
+
# <form action="/login" method="post">
|
67
|
+
# <label>name</label><input type="text" name="username" /><br />
|
68
|
+
# <label>password</label><input type="password" name="password" /><br />
|
69
|
+
# <input type="submit" value="login">
|
70
|
+
# </form>
|
71
|
+
# </body>
|
72
|
+
# </html>
|
73
|
+
#
|
74
|
+
module FormKeeper
|
75
|
+
|
76
|
+
def form_filter(name, &block)
|
77
|
+
::FormKeeper::Validator.register_filter name,
|
78
|
+
::FormKeeper::Filter::Custom.new(block)
|
79
|
+
end
|
80
|
+
|
81
|
+
def form_constraint(name, &block)
|
82
|
+
::FormKeeper::Validator.register_constraint name,
|
83
|
+
::FormKeeper::Constraint::Custom.new(block)
|
84
|
+
end
|
85
|
+
|
86
|
+
def form_messages(arg)
|
87
|
+
case arg
|
88
|
+
when String
|
89
|
+
messages = ::FormKeeper::Messages.from_file(arg)
|
90
|
+
when Hash
|
91
|
+
messages = ::FormKeeper::Messages.new(arg)
|
92
|
+
else
|
93
|
+
raise ArgumentError.new
|
94
|
+
end
|
95
|
+
set :form_failure_messages, messages
|
96
|
+
end
|
97
|
+
module Helpers
|
98
|
+
def reset_form
|
99
|
+
@form_report = ::FormKeeper::Report.new
|
100
|
+
end
|
101
|
+
def form(&block)
|
102
|
+
if block
|
103
|
+
rule = ::FormKeeper::Rule.new
|
104
|
+
rule.instance_eval(&block)
|
105
|
+
messages = settings.form_failure_messages
|
106
|
+
@form_report =
|
107
|
+
settings.form_validator.validate(params, rule, messages)
|
108
|
+
end
|
109
|
+
@form_report
|
110
|
+
end
|
111
|
+
def fill_in_form(output, others={})
|
112
|
+
filled = settings.form_respondent.fill_up(output, params.merge(others))
|
113
|
+
output.replace(filled)
|
114
|
+
output
|
115
|
+
end
|
116
|
+
end
|
117
|
+
def self.registered(app)
|
118
|
+
app.helpers Helpers
|
119
|
+
app.set :form_validator, ::FormKeeper::Validator.new
|
120
|
+
app.set :form_respondent, ::FormKeeper::Respondent.new
|
121
|
+
app.set :form_failure_messages, nil
|
122
|
+
app.before do
|
123
|
+
reset_form
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
register FormKeeper
|
128
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sinatra/formkeeper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "sinatra-formkeeper"
|
8
|
+
gem.version = Sinatra::FormKeeper::VERSION
|
9
|
+
gem.authors = ["lyokato"]
|
10
|
+
gem.email = ["lyo.kato@gmail.com"]
|
11
|
+
gem.description = %q{Sinatra extension which handles stuff around HTML forms}
|
12
|
+
gem.summary = %q{This module provides you a easy way for form-validation and fill-in-form}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
data/spec/app.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'sinatra'
|
2
|
+
require 'sinatra/formkeeper'
|
3
|
+
|
4
|
+
form_messages File.expand_path(File.join(File.dirname(__FILE__), 'messages.yaml'))
|
5
|
+
|
6
|
+
form_filter :my_filter do |value|
|
7
|
+
value.capitalize
|
8
|
+
end
|
9
|
+
|
10
|
+
get '/login' do
|
11
|
+
erb :login
|
12
|
+
end
|
13
|
+
|
14
|
+
post '/login' do
|
15
|
+
form do
|
16
|
+
filters :strip, :my_filter
|
17
|
+
field :username, :present => true, :length => 4..8
|
18
|
+
field :password, :present => true, :length => 4..8
|
19
|
+
end
|
20
|
+
if form.failed?
|
21
|
+
output = erb :login
|
22
|
+
fill_in_form(output)
|
23
|
+
else
|
24
|
+
"login success " + form[:username] + ":" + form[:password]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
__END__
|
29
|
+
|
30
|
+
@@ login
|
31
|
+
<html>
|
32
|
+
<head>Login</head>
|
33
|
+
<body>
|
34
|
+
<% if form.failed? %>
|
35
|
+
<p>found invalid params</p>
|
36
|
+
<ul>
|
37
|
+
<% form.messages(:login).each do |message| %> <li><%= message %></li>
|
38
|
+
<% end %> </ul>
|
39
|
+
<% end %>
|
40
|
+
<form action="/login" method="post">
|
41
|
+
<input type="text" name="username" />
|
42
|
+
<input type="text" name="password" />
|
43
|
+
<input type="submit" value="login" />
|
44
|
+
</form>
|
45
|
+
</body>
|
46
|
+
</html>
|
47
|
+
|
data/spec/app_spec.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sinatra::FormKeeper do
|
4
|
+
include Rack::Test::Methods
|
5
|
+
def app
|
6
|
+
@app ||= Sinatra::Application
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "form validation" do
|
10
|
+
before { get '/login' }
|
11
|
+
subject { last_response }
|
12
|
+
it "responds" do
|
13
|
+
should be_ok
|
14
|
+
end
|
15
|
+
it "returns repsonse body" do
|
16
|
+
subject.body.should == <<-HTML
|
17
|
+
<html>
|
18
|
+
<head>Login</head>
|
19
|
+
<body>
|
20
|
+
<form action="/login" method="post">
|
21
|
+
<input type="text" name="username" />
|
22
|
+
<input type="text" name="password" />
|
23
|
+
<input type="submit" value="login" />
|
24
|
+
</form>
|
25
|
+
</body>
|
26
|
+
</html>
|
27
|
+
|
28
|
+
HTML
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "form validation with valid params" do
|
33
|
+
before { post '/login', :username => ' john', :password => 'foobar' }
|
34
|
+
subject { last_response }
|
35
|
+
it "responds" do
|
36
|
+
should be_ok
|
37
|
+
end
|
38
|
+
it "returns repsonse body" do
|
39
|
+
subject.body.should == "login success John:Foobar"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "form validation with invalid params" do
|
44
|
+
before { post '/login', :username => 'John', :password => 'Foo' }
|
45
|
+
subject { last_response }
|
46
|
+
it "responds" do
|
47
|
+
should be_ok
|
48
|
+
end
|
49
|
+
it "returns repsonse body" do
|
50
|
+
subject.body.should == <<-HTML
|
51
|
+
<html>
|
52
|
+
<head>Login</head>
|
53
|
+
<body>
|
54
|
+
<p>found invalid params</p>
|
55
|
+
<ul>
|
56
|
+
<li>Password's length should be between 4 and 8</li>
|
57
|
+
</ul>
|
58
|
+
<form action="/login" method="post">
|
59
|
+
<input type="text" name="username" value="John" />
|
60
|
+
<input type="text" name="password" value="Foo" />
|
61
|
+
<input type="submit" value="login" />
|
62
|
+
</form>
|
63
|
+
</body>
|
64
|
+
</html>
|
65
|
+
|
66
|
+
HTML
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "form validation with multiple invalid params" do
|
72
|
+
before { post '/login', :username => 'Bar', :password => 'Foo' }
|
73
|
+
subject { last_response }
|
74
|
+
it "responds" do
|
75
|
+
should be_ok
|
76
|
+
end
|
77
|
+
it "returns repsonse body" do
|
78
|
+
subject.body.should == <<-HTML
|
79
|
+
<html>
|
80
|
+
<head>Login</head>
|
81
|
+
<body>
|
82
|
+
<p>found invalid params</p>
|
83
|
+
<ul>
|
84
|
+
<li>Name's length should be between 4 and 8</li>
|
85
|
+
<li>Password's length should be between 4 and 8</li>
|
86
|
+
</ul>
|
87
|
+
<form action="/login" method="post">
|
88
|
+
<input type="text" name="username" value="Bar" />
|
89
|
+
<input type="text" name="password" value="Foo" />
|
90
|
+
<input type="submit" value="login" />
|
91
|
+
</form>
|
92
|
+
</body>
|
93
|
+
</html>
|
94
|
+
|
95
|
+
HTML
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
end
|
data/spec/messages.yaml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
---
|
2
|
+
login:
|
3
|
+
username:
|
4
|
+
present: You must input name
|
5
|
+
ascii: Name should be ASCII
|
6
|
+
length: Name's length should be between 4 and 8
|
7
|
+
DEFAULT: Input your name correctly!
|
8
|
+
password:
|
9
|
+
present: You must input password
|
10
|
+
ascii: Password should be ASCII
|
11
|
+
length: Password's length should be between 4 and 8
|
12
|
+
DEFAULT:
|
13
|
+
username:
|
14
|
+
ascii: Name should be ASCII
|
15
|
+
length: Name's length should be between 4 and 8
|
16
|
+
DEFAULT: Input your name correctly!
|
17
|
+
password:
|
18
|
+
ascii: Password should be ASCII
|
19
|
+
length: Password's length should be between 4 and 8
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-formkeeper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- lyokato
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-23 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Sinatra extension which handles stuff around HTML forms
|
15
|
+
email:
|
16
|
+
- lyo.kato@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rspec
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/sinatra/formkeeper.rb
|
28
|
+
- lib/sinatra/formkeeper/version.rb
|
29
|
+
- sinatra-formkeeper.gemspec
|
30
|
+
- spec/app.rb
|
31
|
+
- spec/app_spec.rb
|
32
|
+
- spec/messages.yaml
|
33
|
+
- spec/spec_helper.rb
|
34
|
+
homepage: ''
|
35
|
+
licenses: []
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.8.24
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: This module provides you a easy way for form-validation and fill-in-form
|
58
|
+
test_files:
|
59
|
+
- spec/app.rb
|
60
|
+
- spec/app_spec.rb
|
61
|
+
- spec/messages.yaml
|
62
|
+
- spec/spec_helper.rb
|