apipie-rails 0.0.20 → 0.0.21
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -0
- data/README.rst +7 -0
- data/lib/apipie/configuration.rb +16 -2
- data/lib/apipie/dsl_definition.rb +7 -9
- data/lib/apipie/markup.rb +5 -1
- data/lib/apipie/validator.rb +6 -1
- data/lib/apipie/version.rb +1 -1
- data/spec/controllers/users_controller_spec.rb +34 -3
- metadata +2 -2
data/CHANGELOG
CHANGED
data/README.rst
CHANGED
@@ -478,6 +478,13 @@ default_version
|
|
478
478
|
validate
|
479
479
|
Parameters validation is turned off when set to false.
|
480
480
|
|
481
|
+
validate_value
|
482
|
+
Check the value of params against specified validators (true by
|
483
|
+
default)
|
484
|
+
|
485
|
+
validate_presence
|
486
|
+
Check the params presence against the documentation.
|
487
|
+
|
481
488
|
app_info
|
482
489
|
Application long description.
|
483
490
|
|
data/lib/apipie/configuration.rb
CHANGED
@@ -2,8 +2,10 @@ module Apipie
|
|
2
2
|
class Configuration
|
3
3
|
|
4
4
|
attr_accessor :app_name, :app_info, :copyright, :markup, :disqus_shortname,
|
5
|
-
:
|
6
|
-
:default_version, :debug, :version_in_url, :namespaced_resources
|
5
|
+
:api_base_url, :doc_base_url, :required_by_default, :layout,
|
6
|
+
:default_version, :debug, :version_in_url, :namespaced_resources,
|
7
|
+
:validate, :validate_value, :validate_presence
|
8
|
+
|
7
9
|
|
8
10
|
alias_method :validate?, :validate
|
9
11
|
alias_method :required_by_default?, :required_by_default
|
@@ -24,6 +26,16 @@ module Apipie
|
|
24
26
|
return @reload_controllers && @api_controllers_matcher
|
25
27
|
end
|
26
28
|
|
29
|
+
def validate_value
|
30
|
+
return (validate? && @validate_value)
|
31
|
+
end
|
32
|
+
alias_method :validate_value?, :validate_value
|
33
|
+
|
34
|
+
def validate_presence
|
35
|
+
return (validate? && @validate_presence)
|
36
|
+
end
|
37
|
+
alias_method :validate_presence?, :validate_presence
|
38
|
+
|
27
39
|
# set to true if you want to use pregenerated documentation cache and avoid
|
28
40
|
# generating the documentation on runtime (usefull for production
|
29
41
|
# environment).
|
@@ -100,6 +112,8 @@ module Apipie
|
|
100
112
|
@app_info = HashWithIndifferentAccess.new
|
101
113
|
@copyright = nil
|
102
114
|
@validate = true
|
115
|
+
@validate_value = true
|
116
|
+
@validate_presence = true
|
103
117
|
@required_by_default = false
|
104
118
|
@api_base_url = HashWithIndifferentAccess.new
|
105
119
|
@doc_base_url = "/apipie"
|
@@ -175,19 +175,17 @@ module Apipie
|
|
175
175
|
|
176
176
|
define_method(description.method) do |*args|
|
177
177
|
|
178
|
-
if Apipie.configuration.
|
178
|
+
if Apipie.configuration.validate_presence?
|
179
179
|
description.params.each do |_, param|
|
180
|
-
|
181
180
|
# check if required parameters are present
|
182
|
-
if param.required && !params.has_key?(param.name)
|
183
|
-
|
184
|
-
|
181
|
+
raise ParamMissing.new(param.name) if param.required && !params.has_key?(param.name)
|
182
|
+
end
|
183
|
+
end
|
185
184
|
|
185
|
+
if Apipie.configuration.validate_value?
|
186
|
+
description.params.each do |_, param|
|
186
187
|
# params validations
|
187
|
-
if params.has_key?(param.name)
|
188
|
-
param.validate(params[:"#{param.name}"])
|
189
|
-
end
|
190
|
-
|
188
|
+
param.validate(params[:"#{param.name}"]) if params.has_key?(param.name)
|
191
189
|
end
|
192
190
|
end
|
193
191
|
|
data/lib/apipie/markup.rb
CHANGED
@@ -7,7 +7,11 @@ module Apipie
|
|
7
7
|
def initialize
|
8
8
|
require 'rdoc'
|
9
9
|
require 'rdoc/markup/to_html'
|
10
|
-
|
10
|
+
if Gem::Version.new(::RDoc::VERSION) < Gem::Version.new('4.0.0')
|
11
|
+
@rdoc ||= ::RDoc::Markup::ToHtml.new()
|
12
|
+
else
|
13
|
+
@rdoc ||= ::RDoc::Markup::ToHtml.new(::RDoc::Options.new)
|
14
|
+
end
|
11
15
|
end
|
12
16
|
|
13
17
|
def to_html(text)
|
data/lib/apipie/validator.rb
CHANGED
@@ -205,7 +205,12 @@ module Apipie
|
|
205
205
|
def validate(value)
|
206
206
|
if @hash_params
|
207
207
|
@hash_params.each do |k, p|
|
208
|
-
|
208
|
+
if Apipie.configuration.validate_presence?
|
209
|
+
raise ParamMissing.new(k) if p.required && !value.has_key?(k)
|
210
|
+
end
|
211
|
+
if Apipie.configuration.validate_value?
|
212
|
+
p.validate(value[k]) if value.has_key?(k)
|
213
|
+
end
|
209
214
|
end
|
210
215
|
end
|
211
216
|
return true
|
data/lib/apipie/version.rb
CHANGED
@@ -57,7 +57,11 @@ describe UsersController do
|
|
57
57
|
describe "validators" do
|
58
58
|
|
59
59
|
context "validations are disabled" do
|
60
|
-
before
|
60
|
+
before do
|
61
|
+
Apipie.configuration.validate = false
|
62
|
+
Apipie.configuration.validate_value = true
|
63
|
+
Apipie.configuration.validate_presence = true
|
64
|
+
end
|
61
65
|
|
62
66
|
it "should reply to valid request" do
|
63
67
|
get :show, :id => '5', :session => "secret_hash"
|
@@ -70,9 +74,36 @@ describe UsersController do
|
|
70
74
|
|
71
75
|
end
|
72
76
|
|
77
|
+
context "only presence validations are enabled" do
|
78
|
+
before do
|
79
|
+
Apipie.configuration.validate = true
|
80
|
+
Apipie.configuration.validate_value = false
|
81
|
+
Apipie.configuration.validate_presence = true
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should reply to valid request" do
|
85
|
+
lambda { get :show, :id => 5, :session => "secret_hash" }.should_not raise_error
|
86
|
+
assert_response :success
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should fail if required parameter is missing" do
|
90
|
+
lambda { get :show, :id => 5 }.should raise_error(Apipie::ParamMissing, /\bsession\b/)
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should pass if required parameter has wrong type" do
|
94
|
+
lambda { get :show, :id => 5, :session => "secret_hash" }.should_not raise_error
|
95
|
+
lambda { get :show, :id => "ten", :session => "secret_hash" }.should_not raise_error
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
73
100
|
|
74
101
|
context "validations are enabled" do
|
75
|
-
before
|
102
|
+
before do
|
103
|
+
Apipie.configuration.validate = true
|
104
|
+
Apipie.configuration.validate_value = true
|
105
|
+
Apipie.configuration.validate_presence = true
|
106
|
+
end
|
76
107
|
|
77
108
|
it "should reply to valid request" do
|
78
109
|
get :show, :id => '5', :session => "secret_hash"
|
@@ -166,7 +197,7 @@ describe UsersController do
|
|
166
197
|
|
167
198
|
lambda {
|
168
199
|
post :create, :user => { :name => "root" }
|
169
|
-
}.should raise_error(Apipie::
|
200
|
+
}.should raise_error(Apipie::ParamMissing, /pass/)
|
170
201
|
|
171
202
|
post :create, :user => { :name => "root", :pass => "pwd" }
|
172
203
|
assert_response :success
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: apipie-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.21
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-03-
|
13
|
+
date: 2013-03-29 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rails
|