restapi 0.0.4 → 0.0.5

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.
Files changed (49) hide show
  1. data/Gemfile +0 -1
  2. data/Gemfile.lock +0 -10
  3. data/README.rdoc +18 -12
  4. data/app/controllers/restapi/restapis_controller.rb +28 -1
  5. data/app/views/layouts/restapi/restapi.html.erb +1 -0
  6. data/app/views/restapi/restapis/_params.html.erb +22 -0
  7. data/app/views/restapi/restapis/_params_plain.html.erb +16 -0
  8. data/app/views/restapi/restapis/index.html.erb +5 -5
  9. data/app/views/restapi/restapis/method.html.erb +8 -4
  10. data/app/views/restapi/restapis/plain.html.erb +70 -0
  11. data/app/views/restapi/restapis/resource.html.erb +16 -5
  12. data/app/views/restapi/restapis/static.html.erb +4 -6
  13. data/lib/restapi.rb +2 -1
  14. data/lib/restapi/application.rb +72 -22
  15. data/lib/restapi/client/generator.rb +104 -0
  16. data/lib/restapi/client/template/Gemfile.tt +5 -0
  17. data/lib/restapi/client/template/README.tt +3 -0
  18. data/lib/restapi/client/template/base.rb.tt +33 -0
  19. data/lib/restapi/client/template/bin.rb.tt +110 -0
  20. data/lib/restapi/client/template/cli.rb.tt +25 -0
  21. data/lib/restapi/client/template/cli_command.rb.tt +129 -0
  22. data/lib/restapi/client/template/client.rb.tt +10 -0
  23. data/lib/restapi/client/template/resource.rb.tt +17 -0
  24. data/lib/restapi/dsl_definition.rb +20 -2
  25. data/lib/restapi/error_description.rb +8 -2
  26. data/lib/restapi/extractor.rb +143 -0
  27. data/lib/restapi/extractor/collector.rb +113 -0
  28. data/lib/restapi/extractor/recorder.rb +122 -0
  29. data/lib/restapi/extractor/writer.rb +356 -0
  30. data/lib/restapi/helpers.rb +10 -5
  31. data/lib/restapi/markup.rb +12 -12
  32. data/lib/restapi/method_description.rb +52 -8
  33. data/lib/restapi/param_description.rb +6 -5
  34. data/lib/restapi/railtie.rb +1 -1
  35. data/lib/restapi/resource_description.rb +1 -1
  36. data/lib/restapi/restapi_module.rb +43 -0
  37. data/lib/restapi/validator.rb +70 -3
  38. data/lib/restapi/version.rb +1 -1
  39. data/lib/tasks/restapi.rake +120 -121
  40. data/restapi.gemspec +0 -2
  41. data/spec/controllers/restapis_controller_spec.rb +41 -6
  42. data/spec/controllers/users_controller_spec.rb +51 -12
  43. data/spec/dummy/app/controllers/application_controller.rb +0 -2
  44. data/spec/dummy/app/controllers/twitter_example_controller.rb +4 -9
  45. data/spec/dummy/app/controllers/users_controller.rb +13 -6
  46. data/spec/dummy/config/initializers/restapi.rb +7 -0
  47. data/spec/dummy/doc/restapi_examples.yml +28 -0
  48. metadata +49 -76
  49. data/app/helpers/restapi/restapis_helper.rb +0 -31
@@ -17,6 +17,4 @@ Gem::Specification.new do |s|
17
17
  s.require_paths = ["lib"]
18
18
 
19
19
  s.add_development_dependency "rspec-rails"
20
- s.add_development_dependency "rcov"
21
- s.add_development_dependency "weary"
22
20
  end
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'fileutils'
2
3
 
3
4
  describe Restapi::RestapisController do
4
5
 
@@ -16,13 +17,12 @@ describe Restapi::RestapisController do
16
17
 
17
18
  RSpec::Matchers.define :reload_documentation do
18
19
  match do
20
+ Restapi.should_receive(:reload_documentation)
21
+ get :index
19
22
  begin
20
- orig = Restapi.get_resource_description("users")._short_description.dup
21
- Restapi.get_resource_description("users")._short_description << 'Modified'
22
- get :index
23
- ret = Restapi.get_resource_description("users")._short_description == orig
24
- ensure
25
- Restapi.get_resource_description("users")._short_description.gsub!('Modified', "")
23
+ RSpec::Mocks.verify
24
+ rescue RSpec::Mocks::MockExpectationError
25
+ false
26
26
  end
27
27
  end
28
28
 
@@ -94,4 +94,39 @@ describe Restapi::RestapisController do
94
94
  it { should_not reload_documentation }
95
95
  end
96
96
  end
97
+
98
+ describe "documentation cache" do
99
+
100
+ let(:cache_dir) { File.join(Rails.root, "tmp", "restapi-cache") }
101
+
102
+ before do
103
+ FileUtils.rm_r(cache_dir) if File.exists?(cache_dir)
104
+ FileUtils.mkdir_p(File.join(cache_dir, "apidoc", "resource"))
105
+ File.open(File.join(cache_dir, "apidoc.html"), "w") { |f| f << "apidoc.html cache" }
106
+ File.open(File.join(cache_dir, "apidoc.json"), "w") { |f| f << "apidoc.json cache" }
107
+ File.open(File.join(cache_dir, "apidoc", "resource.html"), "w") { |f| f << "resource.html cache" }
108
+ File.open(File.join(cache_dir, "apidoc", "resource", "method.html"), "w") { |f| f << "method.html cache" }
109
+
110
+ Restapi.configuration.use_cache = true
111
+ Restapi.configuration.cache_dir = cache_dir
112
+ end
113
+
114
+ after do
115
+ FileUtils.rm_r(cache_dir) if File.exists?(cache_dir)
116
+ end
117
+
118
+ it "uses the file in cache dir instead of generating the content on runtime" do
119
+ get :index
120
+ response.body.should == "apidoc.html cache"
121
+ get :index, :format => "html"
122
+ response.body.should == "apidoc.html cache"
123
+ get :index, :format => "json"
124
+ response.body.should == "apidoc.json cache"
125
+ get :index, :format => "html", :resource => "resource"
126
+ response.body.should == "resource.html cache"
127
+ get :index, :format => "html", :resource => "resource", :method => "method"
128
+ response.body.should == "method.html cache"
129
+ end
130
+
131
+ end
97
132
  end
@@ -21,7 +21,7 @@ describe UsersController do
21
21
 
22
22
  it "should contain all resource methods" do
23
23
  methods = subject._methods
24
- methods.count.should == 4
24
+ methods.count.should == 5
25
25
  methods.should include("users#show")
26
26
  methods.should include("users#create")
27
27
  methods.should include("users#index")
@@ -147,7 +147,7 @@ describe UsersController do
147
147
  end
148
148
 
149
149
  it "should work with Hash validator" do
150
- post :create, :user => { :username => "root", :password => "12345", :membership => "standard" }
150
+ post :create, :user => { :name => "root", :pass => "12345", :membership => "standard" }
151
151
  assert_response :success
152
152
 
153
153
  a = Restapi[UsersController, :create]
@@ -156,19 +156,19 @@ describe UsersController do
156
156
  param.first.validator.class.should eq(Restapi::Validator::HashValidator)
157
157
  hash_params = param.first.validator.hash_params_ordered
158
158
  hash_params.count.should == 3
159
- hash_params[0].name == :username
160
- hash_params[1].name == :password
159
+ hash_params[0].name == :name
160
+ hash_params[1].name == :pass
161
161
  hash_params[2].name == :membership
162
162
 
163
163
  lambda {
164
- post :create, :user => { :username => "root", :password => "12345", :membership => "____" }
164
+ post :create, :user => { :name => "root", :pass => "12345", :membership => "____" }
165
165
  }.should raise_error(ArgumentError, / membership /)
166
166
 
167
167
  lambda {
168
- post :create, :user => { :username => "root" }
169
- }.should raise_error(ArgumentError, / password /)
168
+ post :create, :user => { :name => "root" }
169
+ }.should raise_error(ArgumentError, / pass /)
170
170
 
171
- post :create, :user => { :username => "root", :password => "pwd" }
171
+ post :create, :user => { :name => "root", :pass => "pwd" }
172
172
  assert_response :success
173
173
  end
174
174
 
@@ -186,8 +186,8 @@ describe UsersController do
186
186
  it "should allow nil when allow_nil is set to true" do
187
187
  post :create,
188
188
  :user => {
189
- :username => "root",
190
- :password => "12345",
189
+ :name => "root",
190
+ :pass => "12345",
191
191
  :membership => "standard",
192
192
  },
193
193
  :facts => nil
@@ -220,6 +220,29 @@ describe UsersController do
220
220
  b.full_description.length.should be > 400
221
221
  end
222
222
 
223
+ context "contain :see option" do
224
+
225
+ context "the key is valid" do
226
+ it "should contain reference to another method" do
227
+ api = Restapi[UsersController, :see_another]
228
+ api.see.should eq('users#create')
229
+ Restapi['users#see_another'].should eq(Restapi[UsersController, :see_another])
230
+ api.see_url.should eq(Restapi[UsersController, :create].doc_url)
231
+ end
232
+ end
233
+
234
+ context "the key is not valid" do
235
+ it "should raise exception" do
236
+ api = Restapi[UsersController, :see_another]
237
+ api.instance_variable_set :@see, 'doesnot#exist'
238
+ lambda {
239
+ api.see_url
240
+ }.should raise_error(ArgumentError, /does not exist/)
241
+ api.instance_variable_set :@see, nil
242
+ end
243
+ end
244
+ end
245
+
223
246
  it "should contain possible errors description" do
224
247
  a = Restapi.get_method_description(UsersController, :show)
225
248
 
@@ -276,13 +299,13 @@ describe UsersController do
276
299
  :allow_nil => false,
277
300
  :validator=>"Parameter has to be String.",
278
301
  :description=>"\n<p>Username for login</p>\n",
279
- :name=>"username", :full_name=>"resource_param[username]",
302
+ :name=>"ausername", :full_name=>"resource_param[ausername]",
280
303
  :expected_type=>"string"},
281
304
  {:required=>true,
282
305
  :allow_nil => false,
283
306
  :validator=>"Parameter has to be String.",
284
307
  :description=>"\n<p>Password for login</p>\n",
285
- :name=>"password", :full_name=>"resource_param[password]",
308
+ :name=>"apassword", :full_name=>"resource_param[apassword]",
286
309
  :expected_type=>"string"}
287
310
  ]
288
311
  },
@@ -311,6 +334,22 @@ describe UsersController do
311
334
 
312
335
  end
313
336
 
337
+ describe "examples" do
338
+
339
+ it "should be able to load examples from yml file" do
340
+ Restapi.get_method_description(UsersController, :show).examples.should == [<<EOS1, <<EOS2].map(&:chomp)
341
+ GET /users/14?verbose=true
342
+ 200
343
+ {
344
+ "name": "Test User"
345
+ }
346
+ EOS1
347
+ GET /users/15
348
+ 404
349
+ EOS2
350
+ end
351
+ end
352
+
314
353
  describe "param description" do
315
354
 
316
355
  it "should contain all specified information" do
@@ -3,6 +3,4 @@ class ApplicationController < ActionController::Base
3
3
  resource_description do
4
4
  param :oauth, String, :desc => "Authorization", :required => false
5
5
  end
6
-
7
- protect_from_forgery
8
6
  end
@@ -150,21 +150,16 @@ class TwitterExampleController < ApplicationController
150
150
  render :text => 'search'
151
151
  end
152
152
 
153
- api :GET, '/twitter_example/show', 'Returns extended information of a given user, specified by ID or screen name as per the required id parameter.'
154
- param :user_id, Integer, :required => true, :desc => <<-EOS
153
+ api :GET, '/twitter_example/:id', 'Returns extended information of a given user, specified by ID or screen name as per the required id parameter.'
154
+ param :id, :number, :required => true, :desc => <<-EOS
155
155
  The ID of the user for whom to return results for. Either an id or screen_name is required for this method.
156
- _Note_: Specifies the ID of the user to befriend. Helpful for disambiguating when a valid user ID is also a valid screen name.
157
156
  EOS
158
- param :screen_name, String, :desc => 'The screen name of the user for whom to return results for. Either a id or screen_name is required for this method.'
157
+ param :screen_name, String, :desc => 'The screen name of the user for...'
159
158
  description <<-EDOC
160
159
  Returns extended information of a given user, specified by ID or screen name as per the required id parameter. The author's most recent status will be returned inline.
161
-
162
- You must be following a protected user to be able to see their most recent status update. If you don't follow a protected user, or request this method without autenticating, the users status will be removed.
163
-
164
- The URL pattern <tt>/version/twitter_example/show/:screen_name_or_user_id.format</tt> is still accepted but not recommended. As a sequence of numbers is a valid screen name we recommend using the +screen_name+ or +user_id+ parameter instead.
165
160
  EDOC
166
161
  def show
167
- render :text => 'show'
162
+ render :text => "show #{params}"
168
163
  end
169
164
 
170
165
  api :GET, '/twitter_example/contributors', 'Returns an array of users who can contribute to the specified account.'
@@ -7,8 +7,8 @@ class UsersController < ApplicationController
7
7
  version '1.0 - 3.4.2012'
8
8
  param :id, Fixnum, :desc => "User ID", :required => false
9
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
10
+ param :ausername, String, :desc => "Username for login", :required => true
11
+ param :apassword, String, :desc => "Password for login", :required => true
12
12
  end
13
13
  description <<-EOS
14
14
  == Long description
@@ -163,8 +163,8 @@ class UsersController < ApplicationController
163
163
  More builder documentation can be found at http://builder.rubyforge.org.
164
164
  eos
165
165
  api :GET, "/users/:id", "Show user profile"
166
- error :code => 401, :desc => "Unauthorized"
167
- error :code => 404, :desc => "Not Found"
166
+ error 401, "Unauthorized"
167
+ error :code => 404, :description => "Not Found"
168
168
  param :id, Integer, :desc => "user id", :required => true
169
169
  param :session, String, :desc => "user is logged in", :required => true
170
170
  param :regexp_param, /^[0-9]* years/, :desc => "regexp param"
@@ -188,8 +188,8 @@ class UsersController < ApplicationController
188
188
 
189
189
  api :POST, "/users", "Create user"
190
190
  param :user, Hash, :desc => "User info", :required => true do
191
- param :username, String, :desc => "Username for login", :required => true
192
- param :password, String, :desc => "Password for login", :required => true
191
+ param :name, String, :desc => "Username for login", :required => true
192
+ param :pass, String, :desc => "Password for login", :required => true
193
193
  param :membership, ["standard","premium"], :desc => "User membership"
194
194
  end
195
195
  param :facts, Hash, :desc => "Additional optional facts about the user", :allow_nil => true
@@ -213,4 +213,11 @@ class UsersController < ApplicationController
213
213
  def two_urls
214
214
  render :text => 'List of users'
215
215
  end
216
+
217
+ api :GET, '/users/see_another', 'Boring method'
218
+ see 'users#create'
219
+ desc 'This method is boring, look at users#create'
220
+ def see_another
221
+ render :text => 'This is very similar to create action'
222
+ end
216
223
  end
@@ -4,6 +4,13 @@ Restapi.configure do |config|
4
4
  config.doc_base_url = "/apidoc"
5
5
  config.api_base_url = "/api"
6
6
 
7
+ # set to true to turn on/off the cache. To generate the cache use:
8
+ #
9
+ # rake restapi:cache
10
+ #
11
+ # config.use_cache = Rails.env.production?
12
+ # config.cache_dir = File.join(Rails.root, "public", "restapi-cache") # optional
13
+
7
14
  # set to enable/disable reloading controllers (and the documentation with it),
8
15
  # by default enabled in development
9
16
  # config.reload_controllers = false
@@ -0,0 +1,28 @@
1
+ ---
2
+ users#show:
3
+ - verb: :GET
4
+ path: /users/15
5
+ query:
6
+ request_data:
7
+ response_data:
8
+ code: 404
9
+ show_in_doc: 2
10
+ recorded: true
11
+ - verb: :GET
12
+ path: /users/14
13
+ query: "verbose=true"
14
+ request_data:
15
+ response_data:
16
+ name: Test User
17
+ code: 200
18
+ show_in_doc: 1
19
+ recorded: true
20
+ - verb: :GET
21
+ path: /users/15
22
+ query: "verbose=true"
23
+ request_data:
24
+ response_data:
25
+ name: Test User
26
+ code: 200
27
+ show_in_doc: 0
28
+ recorded: true
metadata CHANGED
@@ -1,74 +1,39 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: restapi
3
- version: !ruby/object:Gem::Version
4
- hash: 23
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 4
10
- version: 0.0.4
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Pavel Pokorny
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-05-16 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-07-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: rspec-rails
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
32
22
  type: :development
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: rcov
36
23
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
38
25
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
- version: "0"
46
- type: :development
47
- version_requirements: *id002
48
- - !ruby/object:Gem::Dependency
49
- name: weary
50
- prerelease: false
51
- requirement: &id003 !ruby/object:Gem::Requirement
52
- none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- hash: 3
57
- segments:
58
- - 0
59
- version: "0"
60
- type: :development
61
- version_requirements: *id003
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
62
30
  description: Maintain your API documentation up to date!
63
- email:
31
+ email:
64
32
  - pajkycz@gmail.com
65
33
  executables: []
66
-
67
34
  extensions: []
68
-
69
35
  extra_rdoc_files: []
70
-
71
- files:
36
+ files:
72
37
  - .autotest
73
38
  - .gitignore
74
39
  - .rspec
@@ -80,7 +45,6 @@ files:
80
45
  - README.rdoc
81
46
  - Rakefile
82
47
  - app/controllers/restapi/restapis_controller.rb
83
- - app/helpers/restapi/restapis_helper.rb
84
48
  - app/public/restapi/javascripts/bundled/bootstrap-collapse.js
85
49
  - app/public/restapi/javascripts/bundled/bootstrap.js
86
50
  - app/public/restapi/javascripts/bundled/jquery-1.7.2.js
@@ -91,14 +55,30 @@ files:
91
55
  - app/public/restapi/stylesheets/bundled/bootstrap.min.css
92
56
  - app/public/restapi/stylesheets/bundled/prettify.css
93
57
  - app/views/layouts/restapi/restapi.html.erb
58
+ - app/views/restapi/restapis/_params.html.erb
59
+ - app/views/restapi/restapis/_params_plain.html.erb
94
60
  - app/views/restapi/restapis/index.html.erb
95
61
  - app/views/restapi/restapis/method.html.erb
62
+ - app/views/restapi/restapis/plain.html.erb
96
63
  - app/views/restapi/restapis/resource.html.erb
97
64
  - app/views/restapi/restapis/static.html.erb
98
65
  - lib/restapi.rb
99
66
  - lib/restapi/application.rb
67
+ - lib/restapi/client/generator.rb
68
+ - lib/restapi/client/template/Gemfile.tt
69
+ - lib/restapi/client/template/README.tt
70
+ - lib/restapi/client/template/base.rb.tt
71
+ - lib/restapi/client/template/bin.rb.tt
72
+ - lib/restapi/client/template/cli.rb.tt
73
+ - lib/restapi/client/template/cli_command.rb.tt
74
+ - lib/restapi/client/template/client.rb.tt
75
+ - lib/restapi/client/template/resource.rb.tt
100
76
  - lib/restapi/dsl_definition.rb
101
77
  - lib/restapi/error_description.rb
78
+ - lib/restapi/extractor.rb
79
+ - lib/restapi/extractor/collector.rb
80
+ - lib/restapi/extractor/recorder.rb
81
+ - lib/restapi/extractor/writer.rb
102
82
  - lib/restapi/helpers.rb
103
83
  - lib/restapi/markup.rb
104
84
  - lib/restapi/method_description.rb
@@ -135,6 +115,7 @@ files:
135
115
  - spec/dummy/config/initializers/session_store.rb
136
116
  - spec/dummy/config/locales/en.yml
137
117
  - spec/dummy/config/routes.rb
118
+ - spec/dummy/doc/restapi_examples.yml
138
119
  - spec/dummy/public/404.html
139
120
  - spec/dummy/public/422.html
140
121
  - spec/dummy/public/500.html
@@ -150,38 +131,29 @@ files:
150
131
  - spec/spec_helper.rb
151
132
  homepage: http://github.com/Pajk/rails-restapi
152
133
  licenses: []
153
-
154
134
  post_install_message:
155
135
  rdoc_options: []
156
-
157
- require_paths:
136
+ require_paths:
158
137
  - lib
159
- required_ruby_version: !ruby/object:Gem::Requirement
138
+ required_ruby_version: !ruby/object:Gem::Requirement
160
139
  none: false
161
- requirements:
162
- - - ">="
163
- - !ruby/object:Gem::Version
164
- hash: 3
165
- segments:
166
- - 0
167
- version: "0"
168
- required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
145
  none: false
170
- requirements:
171
- - - ">="
172
- - !ruby/object:Gem::Version
173
- hash: 3
174
- segments:
175
- - 0
176
- version: "0"
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
177
150
  requirements: []
178
-
179
151
  rubyforge_project:
180
152
  rubygems_version: 1.8.23
181
153
  signing_key:
182
154
  specification_version: 3
183
155
  summary: REST API documentation tool
184
- test_files:
156
+ test_files:
185
157
  - spec/controllers/restapis_controller_spec.rb
186
158
  - spec/controllers/users_controller_spec.rb
187
159
  - spec/dummy/Rakefile
@@ -205,6 +177,7 @@ test_files:
205
177
  - spec/dummy/config/initializers/session_store.rb
206
178
  - spec/dummy/config/locales/en.yml
207
179
  - spec/dummy/config/routes.rb
180
+ - spec/dummy/doc/restapi_examples.yml
208
181
  - spec/dummy/public/404.html
209
182
  - spec/dummy/public/422.html
210
183
  - spec/dummy/public/500.html