restapi 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +5 -0
- data/Gemfile +4 -7
- data/Gemfile.lock +5 -25
- data/README.rdoc +3 -0
- data/app/controllers/restapi/restapis_controller.rb +3 -12
- data/app/public/restapi/javascripts/jst.js +41 -22
- data/app/public/restapi/javascripts/restapi.js +4 -5
- data/app/public/restapi/javascripts/routers/documentation_router.js +13 -8
- data/lib/restapi.rb +1 -2
- data/lib/restapi/application.rb +28 -23
- data/lib/restapi/dsl_definition.rb +8 -12
- data/lib/restapi/helpers.rb +2 -2
- data/lib/restapi/markup.rb +45 -0
- data/lib/restapi/method_description.rb +82 -16
- data/lib/restapi/param_description.rb +36 -11
- data/lib/restapi/resource_description.rb +18 -10
- data/lib/restapi/restapi_module.rb +6 -7
- data/lib/restapi/static_dispatcher.rb +2 -0
- data/lib/restapi/validator.rb +40 -31
- data/lib/restapi/version.rb +1 -1
- data/restapi.gemspec +1 -1
- data/spec/controllers/users_controller_spec.rb +156 -32
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/dogs_controller.rb +0 -2
- data/spec/dummy/app/controllers/users_controller.rb +20 -5
- data/spec/dummy/config/database.yml +2 -3
- data/spec/dummy/config/environment.rb +3 -0
- data/spec/dummy/config/initializers/restapi.rb +6 -2
- data/spec/dummy/config/routes.rb +11 -64
- metadata +7 -21
- data/spec/dummy/app/views/users/doc.html.erb +0 -24
data/lib/restapi/version.rb
CHANGED
data/restapi.gemspec
CHANGED
@@ -6,14 +6,18 @@ describe UsersController do
|
|
6
6
|
it "should be described" do
|
7
7
|
a = Restapi.get_resource_description(UsersController)
|
8
8
|
|
9
|
+
#puts Restapi.method_descriptions['users#show'].inspect
|
9
10
|
a._short_description.should eq('Site members')
|
10
|
-
|
11
|
-
a._methods.should
|
12
|
-
|
11
|
+
a._methods.count.should == 4
|
12
|
+
a._methods.should include("users#show")
|
13
|
+
a._methods.should include("users#create")
|
14
|
+
a._methods.should include("users#index")
|
15
|
+
a._methods.should include("users#two_urls")
|
16
|
+
md = a._params_ordered.find { |p| p.name == :id }
|
13
17
|
md.should_not be(nil)
|
14
18
|
md.name.should eq(:id)
|
15
19
|
md.desc.should eq("\n<p>User ID</p>\n")
|
16
|
-
md.required.should eq(
|
20
|
+
md.required.should eq(false)
|
17
21
|
md.validator.class.should eq(Restapi::Validator::TypeValidator)
|
18
22
|
a._id.should eq('users')
|
19
23
|
a._path.should eq('/users')
|
@@ -46,15 +50,17 @@ describe UsersController do
|
|
46
50
|
a.should eq(b)
|
47
51
|
|
48
52
|
a.method.should eq(:show)
|
49
|
-
a.resource.should eq('users')
|
53
|
+
a.resource._id.should eq('users')
|
50
54
|
a.errors[0].code.should eq(401)
|
51
55
|
a.errors[0].description.should eq("Unauthorized")
|
52
56
|
a.errors[1].code.should eq(404)
|
53
57
|
a.errors[1].description.should eq("Not Found")
|
54
58
|
|
55
|
-
a.
|
56
|
-
a.
|
57
|
-
a.
|
59
|
+
a.apis.count.should == 1
|
60
|
+
a.apis.first.short_description.should eq("Show user profile")
|
61
|
+
a.apis.first.api_url.should
|
62
|
+
eq("#{Restapi.configuration.api_base_url}/users/:id")
|
63
|
+
a.apis.first.http_method.should eq("GET")
|
58
64
|
|
59
65
|
param = a.params[:session]
|
60
66
|
param.required.should eq(true)
|
@@ -78,12 +84,14 @@ describe UsersController do
|
|
78
84
|
param.desc.should eq("\n<p>regexp param</p>\n")
|
79
85
|
param.required.should eq(false)
|
80
86
|
param.validator.class.should be(Restapi::Validator::RegexpValidator)
|
81
|
-
param.validator.instance_variable_get("@regexp").should
|
87
|
+
param.validator.instance_variable_get("@regexp").should
|
88
|
+
eq(/^[0-9]* years/)
|
82
89
|
|
83
90
|
param = a.params[:array_param]
|
84
91
|
param.desc.should eq("\n<p>array validator</p>\n")
|
85
92
|
param.validator.class.should be(Restapi::Validator::ArrayValidator)
|
86
|
-
param.validator.instance_variable_get("@array").should
|
93
|
+
param.validator.instance_variable_get("@array").should
|
94
|
+
eq([100, "one", "two", 1, 2])
|
87
95
|
|
88
96
|
param = a.params[:proc_param]
|
89
97
|
param.desc.should eq("\n<p>proc validator</p>\n")
|
@@ -93,12 +101,27 @@ describe UsersController do
|
|
93
101
|
end
|
94
102
|
|
95
103
|
it "should yell ArgumentError if id is not a number" do
|
96
|
-
lambda {
|
104
|
+
lambda {
|
105
|
+
get :show,
|
106
|
+
:id => "not a number",
|
107
|
+
:session => "secret_hash"
|
108
|
+
}.should raise_error(ArgumentError)
|
97
109
|
end
|
98
110
|
|
99
111
|
it "should yell ArgumentError if float_param is not a float" do
|
100
|
-
lambda {
|
101
|
-
|
112
|
+
lambda {
|
113
|
+
get :show,
|
114
|
+
:id => 5,
|
115
|
+
:session => "secret_hash",
|
116
|
+
:float_param => "234.2.34"
|
117
|
+
}.should raise_error(ArgumentError)
|
118
|
+
|
119
|
+
lambda {
|
120
|
+
get :show,
|
121
|
+
:id => 5,
|
122
|
+
:session => "secret_hash",
|
123
|
+
:float_param => "no float here"
|
124
|
+
}.should raise_error(ArgumentError)
|
102
125
|
end
|
103
126
|
|
104
127
|
it "should understand float validator" do
|
@@ -109,13 +132,19 @@ describe UsersController do
|
|
109
132
|
end
|
110
133
|
|
111
134
|
it "should understand regexp validator" do
|
112
|
-
get :show,
|
135
|
+
get :show,
|
136
|
+
:id => 5,
|
137
|
+
:session => "secret_hash",
|
138
|
+
:regexp_param => "24 years"
|
113
139
|
assert_response :success
|
114
140
|
end
|
115
141
|
|
116
142
|
it "should validate with regexp validator" do
|
117
143
|
lambda {
|
118
|
-
get :show,
|
144
|
+
get :show,
|
145
|
+
:id => 5,
|
146
|
+
:session => "secret_hash",
|
147
|
+
:regexp_param => "ten years"
|
119
148
|
}.should raise_error(ArgumentError)
|
120
149
|
end
|
121
150
|
|
@@ -126,23 +155,38 @@ describe UsersController do
|
|
126
155
|
assert_response :success
|
127
156
|
get :show, :id => 5, :session => "secret_hash", :array_param => 1
|
128
157
|
assert_response :success
|
129
|
-
get :show, :id => 5, :session => "secret_hash", :
|
158
|
+
get :show, :id => 5, :session => "secret_hash", :boolean_param => false
|
130
159
|
assert_response :success
|
131
160
|
end
|
132
161
|
|
133
162
|
it "should raise ArgumentError with array validator" do
|
134
|
-
lambda {
|
135
|
-
|
163
|
+
lambda {
|
164
|
+
get :show,
|
165
|
+
:id => 5,
|
166
|
+
:session => "secret_hash",
|
167
|
+
:array_param => "blabla"
|
168
|
+
}.should raise_error(ArgumentError)
|
136
169
|
|
137
|
-
lambda {
|
138
|
-
|
170
|
+
lambda {
|
171
|
+
get :show,
|
172
|
+
:id => 5,
|
173
|
+
:session => "secret_hash",
|
174
|
+
:array_param => 3
|
175
|
+
}.should raise_error(ArgumentError)
|
139
176
|
end
|
140
177
|
|
141
178
|
it "should validate with Proc validator" do
|
142
|
-
lambda {
|
143
|
-
|
179
|
+
lambda {
|
180
|
+
get :show,
|
181
|
+
:id => 5,
|
182
|
+
:session => "secret_hash",
|
183
|
+
:proc_param => "asdgsag"
|
184
|
+
}.should raise_error(ArgumentError)
|
144
185
|
|
145
|
-
get :show,
|
186
|
+
get :show,
|
187
|
+
:id => 5,
|
188
|
+
:session => "secret_hash",
|
189
|
+
:proc_param => "param value"
|
146
190
|
assert_response :success
|
147
191
|
end
|
148
192
|
|
@@ -151,25 +195,105 @@ describe UsersController do
|
|
151
195
|
describe "POST create" do
|
152
196
|
|
153
197
|
it "should understand hash validator" do
|
154
|
-
post :create,
|
198
|
+
post :create,
|
199
|
+
:user => {
|
200
|
+
:username => "root",
|
201
|
+
:password => "12345",
|
202
|
+
:membership => "standard"
|
203
|
+
}
|
155
204
|
assert_response :success
|
156
205
|
|
157
206
|
a = Restapi[UsersController, :create]
|
158
|
-
a.
|
159
|
-
a.
|
160
|
-
|
207
|
+
a.apis.count.should == 1
|
208
|
+
api = a.apis.first
|
209
|
+
api.short_description.should eq("Create user")
|
210
|
+
api.api_url.should eq("/api/users")
|
211
|
+
api.http_method.should eq("POST")
|
161
212
|
|
162
|
-
lambda {
|
163
|
-
|
213
|
+
lambda {
|
214
|
+
post :create,
|
215
|
+
:user => {
|
216
|
+
:username => "root",
|
217
|
+
:password => "12345",
|
218
|
+
:membership => "____"
|
219
|
+
}
|
220
|
+
}.should raise_error(ArgumentError)
|
164
221
|
|
165
|
-
lambda {
|
166
|
-
|
222
|
+
lambda {
|
223
|
+
post :create,
|
224
|
+
:user => { :username => "root" }
|
225
|
+
}.should raise_error(ArgumentError)
|
167
226
|
|
168
|
-
post :create,
|
227
|
+
post :create,
|
228
|
+
:user => { :username => "root", :password => "pwd" }
|
169
229
|
assert_response :success
|
170
230
|
|
171
231
|
end
|
172
232
|
|
233
|
+
it "it should support Hash validator without specifying keys" do
|
234
|
+
Restapi[UsersController, :create].to_json[:params].should include(:name => "facts",
|
235
|
+
:validator => "Parameter has to be Hash.",
|
236
|
+
:description => "\n<p>Additional optional facts about the user</p>\n",
|
237
|
+
:required => false)
|
238
|
+
end
|
239
|
+
|
240
|
+
end
|
241
|
+
|
242
|
+
describe 'two_urls' do
|
243
|
+
|
244
|
+
it "should store all api method description" do
|
245
|
+
method_description = Restapi[UsersController, :two_urls]
|
246
|
+
method_description.class.should be(Restapi::MethodDescription)
|
247
|
+
method_description.apis.count.should == 2
|
248
|
+
apis = method_description.apis
|
249
|
+
a1 = apis.first
|
250
|
+
a2 = apis.second
|
251
|
+
|
252
|
+
a1.short_description.should eq('Get company users')
|
253
|
+
a1.api_url.should eq('/api/company_users')
|
254
|
+
a1.http_method.should eq('GET')
|
255
|
+
a2.short_description.should eq('Get users working in given company')
|
256
|
+
a2.api_url.should eq('/api/company/:id/users')
|
257
|
+
a2.http_method.should eq('GET')
|
258
|
+
end
|
259
|
+
|
260
|
+
it "should be described by valid json" do
|
261
|
+
json_hash = {
|
262
|
+
:errors => [],
|
263
|
+
:doc_url => "#{Restapi.configuration.doc_base_url}#users/two_urls",
|
264
|
+
:full_description => '',
|
265
|
+
:params => [{:required=>false,
|
266
|
+
:validator=>"Parameter has to be String.",
|
267
|
+
:description=>"\n<p>Authorization</p>\n", :name=>"oauth"},
|
268
|
+
{:required=>true,
|
269
|
+
:validator=>"Parameter has to be String.",
|
270
|
+
:description=>"\n<p>Username for login</p>\n",
|
271
|
+
:name=>"resource_param[username]"},
|
272
|
+
{:required=>true,
|
273
|
+
:validator=>"Parameter has to be String.",
|
274
|
+
:description=>"\n<p>Password for login</p>\n",
|
275
|
+
:name=>"resource_param[password]"},
|
276
|
+
{:required=>false, :validator=>"Parameter has to be Integer.",
|
277
|
+
:description=>"\n<p>Company ID</p>\n",
|
278
|
+
:name=>"id"},
|
279
|
+
],
|
280
|
+
:name => :two_urls,
|
281
|
+
:apis => [
|
282
|
+
{
|
283
|
+
:http_method => 'GET',
|
284
|
+
:short_description => 'Get company users',
|
285
|
+
:api_url => "#{Restapi.configuration.api_base_url}/company_users"
|
286
|
+
},{
|
287
|
+
:http_method => 'GET',
|
288
|
+
:short_description => 'Get users working in given company',
|
289
|
+
:api_url =>"#{Restapi.configuration.api_base_url}/company/:id/users"
|
290
|
+
}
|
291
|
+
]
|
292
|
+
}
|
293
|
+
|
294
|
+
Restapi[UsersController, :two_urls].to_json.should eq(json_hash)
|
295
|
+
end
|
296
|
+
|
173
297
|
end
|
174
298
|
|
175
299
|
end
|
@@ -5,7 +5,11 @@ class UsersController < ApplicationController
|
|
5
5
|
short 'Site members'
|
6
6
|
path '/users'
|
7
7
|
version '1.0 - 3.4.2012'
|
8
|
-
param :id, Fixnum, :desc => "User ID", :required =>
|
8
|
+
param :id, Fixnum, :desc => "User ID", :required => false
|
9
|
+
param :resource_param, Hash, :desc => 'Param description for all methods' do
|
10
|
+
param :username, String, :desc => "Username for login", :required => true
|
11
|
+
param :password, String, :desc => "Password for login", :required => true
|
12
|
+
end
|
9
13
|
description <<-EOS
|
10
14
|
== Long description
|
11
15
|
|
@@ -43,6 +47,7 @@ class UsersController < ApplicationController
|
|
43
47
|
param :float_param, Float, :desc => "float param"
|
44
48
|
param :regexp_param, /^[0-9]* years/, :desc => "regexp param"
|
45
49
|
param :array_param, [100, "one", "two", 1, 2], :desc => "array validator"
|
50
|
+
param :boolean_param, [true, false], :desc => "array validator with boolean"
|
46
51
|
param :proc_param, lambda { |val|
|
47
52
|
val == "param value" ? true : "The only good value is 'param value'."
|
48
53
|
}, :desc => "proc validator"
|
@@ -191,6 +196,7 @@ class UsersController < ApplicationController
|
|
191
196
|
param :password, String, :desc => "Password for login", :required => true
|
192
197
|
param :membership, ["standard","premium"], :desc => "User membership"
|
193
198
|
end
|
199
|
+
param :facts, Hash, :desc => "Additional optional facts about the user"
|
194
200
|
def create
|
195
201
|
render :text => "OK"
|
196
202
|
end
|
@@ -201,11 +207,20 @@ class UsersController < ApplicationController
|
|
201
207
|
error :code => 401, :desc => "Unauthorized"
|
202
208
|
error :code => 404, :desc => "Not Found"
|
203
209
|
desc "List all users."
|
210
|
+
param :oauth, nil,
|
211
|
+
:desc => "Hide this global param (eg dont need auth here)"
|
204
212
|
def index
|
205
213
|
render :text => "List of users"
|
206
214
|
end
|
207
|
-
|
208
|
-
|
209
|
-
|
215
|
+
|
216
|
+
api :desc => 'Get company users',
|
217
|
+
:path => '/company_users',
|
218
|
+
:method => 'GET'
|
219
|
+
api :desc => 'Get users working in given company',
|
220
|
+
:path => '/company/:id/users',
|
221
|
+
:method => 'GET'
|
222
|
+
param :id, Integer, :desc => "Company ID"
|
223
|
+
def two_urls
|
224
|
+
render :text => 'List of users'
|
210
225
|
end
|
211
|
-
end
|
226
|
+
end
|
@@ -24,8 +24,12 @@ Restapi.configure do |config|
|
|
24
24
|
"Welcome aboard: You're riding Ruby on Rails!"
|
25
25
|
EOS
|
26
26
|
config.copyright = "© 2012 Pavel Pokorny"
|
27
|
-
config.doc_base_url = "/
|
27
|
+
config.doc_base_url = "/dokumentace"
|
28
28
|
config.api_base_url = "/api"
|
29
|
-
config.
|
29
|
+
# config.markup = choose one of:
|
30
|
+
# Restapi::Markup::RDoc.new [default]
|
31
|
+
# Restapi::Markup::Markdown.new
|
32
|
+
# Restapi::Markup::Textile.new
|
33
|
+
# or provide another class with to_html(text) instance method
|
30
34
|
# config.validate = false
|
31
35
|
end
|
data/spec/dummy/config/routes.rb
CHANGED
@@ -1,69 +1,16 @@
|
|
1
1
|
Dummy::Application.routes.draw do
|
2
|
-
resources :users do
|
3
|
-
collection do
|
4
|
-
get :doc
|
5
|
-
end
|
6
|
-
end
|
7
2
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
3
|
+
scope ENV['RAILS_RELATIVE_URL_ROOT'] || '/' do
|
4
|
+
|
5
|
+
resources :users do
|
6
|
+
collection do
|
7
|
+
get :doc
|
8
|
+
end
|
9
|
+
end
|
12
10
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
# Sample of regular route:
|
17
|
-
# match 'products/:id' => 'catalog#view'
|
18
|
-
# Keep in mind you can assign values other than :controller and :action
|
19
|
-
|
20
|
-
# Sample of named route:
|
21
|
-
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
|
22
|
-
# This route can be invoked with purchase_url(:id => product.id)
|
23
|
-
|
24
|
-
# Sample resource route (maps HTTP verbs to controller actions automatically):
|
25
|
-
# resources :products
|
11
|
+
resources :dogs
|
12
|
+
resources :twitter_example
|
26
13
|
|
27
|
-
|
28
|
-
|
29
|
-
# member do
|
30
|
-
# get 'short'
|
31
|
-
# post 'toggle'
|
32
|
-
# end
|
33
|
-
#
|
34
|
-
# collection do
|
35
|
-
# get 'sold'
|
36
|
-
# end
|
37
|
-
# end
|
38
|
-
|
39
|
-
# Sample resource route with sub-resources:
|
40
|
-
# resources :products do
|
41
|
-
# resources :comments, :sales
|
42
|
-
# resource :seller
|
43
|
-
# end
|
44
|
-
|
45
|
-
# Sample resource route with more complex sub-resources
|
46
|
-
# resources :products do
|
47
|
-
# resources :comments
|
48
|
-
# resources :sales do
|
49
|
-
# get 'recent', :on => :collection
|
50
|
-
# end
|
51
|
-
# end
|
52
|
-
|
53
|
-
# Sample resource route within a namespace:
|
54
|
-
# namespace :admin do
|
55
|
-
# # Directs /admin/products/* to Admin::ProductsController
|
56
|
-
# # (app/controllers/admin/products_controller.rb)
|
57
|
-
# resources :products
|
58
|
-
# end
|
59
|
-
|
60
|
-
# You can have the root of your site routed with "root"
|
61
|
-
# just remember to delete public/index.html.
|
62
|
-
# root :to => "welcome#index"
|
63
|
-
|
64
|
-
# See how all your routes lay out with "rake routes"
|
65
|
-
|
66
|
-
# This is a legacy wild controller route that's not recommended for RESTful applications.
|
67
|
-
# Note: This route will make all actions in every controller accessible via GET requests.
|
68
|
-
# match ':controller(/:action(/:id(.:format)))'
|
14
|
+
restapi
|
15
|
+
end
|
69
16
|
end
|