apipie-rails 0.0.13 → 0.0.14

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 (63) hide show
  1. data/Gemfile.lock +5 -3
  2. data/README.rst +688 -0
  3. data/apipie-rails.gemspec +2 -4
  4. data/app/controllers/apipie/apipies_controller.rb +73 -41
  5. data/app/views/apipie/apipies/index.html.erb +10 -3
  6. data/app/views/apipie/apipies/method.html.erb +5 -2
  7. data/app/views/apipie/apipies/resource.html.erb +10 -3
  8. data/lib/apipie-rails.rb +1 -0
  9. data/lib/apipie/apipie_module.rb +28 -79
  10. data/lib/apipie/application.rb +194 -97
  11. data/lib/apipie/client/generator.rb +5 -4
  12. data/lib/apipie/configuration.rb +112 -0
  13. data/lib/apipie/dsl_definition.rb +93 -16
  14. data/lib/apipie/extractor.rb +12 -5
  15. data/lib/apipie/extractor/collector.rb +1 -1
  16. data/lib/apipie/extractor/recorder.rb +2 -1
  17. data/lib/apipie/extractor/writer.rb +11 -4
  18. data/lib/apipie/helpers.rb +15 -4
  19. data/lib/apipie/markup.rb +3 -4
  20. data/lib/apipie/method_description.rb +28 -22
  21. data/lib/apipie/resource_description.rb +44 -42
  22. data/lib/apipie/routing.rb +3 -1
  23. data/lib/apipie/static_dispatcher.rb +0 -1
  24. data/lib/apipie/version.rb +1 -1
  25. data/lib/generators/apipie/install/README +6 -0
  26. data/lib/generators/apipie/install/install_generator.rb +25 -0
  27. data/lib/generators/apipie/install/templates/initializer.rb.erb +7 -0
  28. data/lib/tasks/apipie.rake +31 -39
  29. data/spec/controllers/api/v1/architectures_controller_spec.rb +30 -0
  30. data/spec/controllers/api/v2/architectures_controller_spec.rb +12 -0
  31. data/spec/controllers/apipies_controller_spec.rb +18 -12
  32. data/spec/controllers/users_controller_spec.rb +56 -19
  33. data/spec/dummy/app/controllers/api/base_controller.rb +4 -0
  34. data/spec/dummy/app/controllers/api/v1/architectures_controller.rb +32 -0
  35. data/spec/dummy/app/controllers/api/v1/base_controller.rb +11 -0
  36. data/spec/dummy/app/controllers/api/v2/architectures_controller.rb +32 -0
  37. data/spec/dummy/app/controllers/api/v2/base_controller.rb +11 -0
  38. data/spec/dummy/app/controllers/twitter_example_controller.rb +1 -1
  39. data/spec/dummy/app/controllers/users_controller.rb +2 -3
  40. data/spec/dummy/config/initializers/apipie.rb +29 -6
  41. data/spec/lib/method_description_spec.rb +29 -0
  42. data/spec/lib/param_description_spec.rb +41 -0
  43. data/spec/lib/resource_description_spec.rb +28 -0
  44. data/spec/spec_helper.rb +1 -1
  45. metadata +99 -164
  46. data/README.rdoc +0 -367
  47. data/lib/apipie/client/base.rb +0 -133
  48. data/lib/apipie/client/cli_command.rb +0 -130
  49. data/lib/apipie/client/main.rb +0 -101
  50. data/lib/apipie/client/rest_client_oauth.rb +0 -19
  51. data/lib/apipie/client/template/Gemfile.tt +0 -3
  52. data/lib/apipie/client/template/README.tt +0 -3
  53. data/lib/apipie/client/template/Rakefile.tt +0 -2
  54. data/lib/apipie/client/template/a_name.gemspec.tt +0 -23
  55. data/lib/apipie/client/template/bin/bin.rb.tt +0 -30
  56. data/lib/apipie/client/template/lib/a_name.rb.tt +0 -27
  57. data/lib/apipie/client/template/lib/a_name/commands/cli.rb.tt +0 -22
  58. data/lib/apipie/client/template/lib/a_name/config.yml +0 -6
  59. data/lib/apipie/client/template/lib/a_name/resources/resource.rb.tt +0 -22
  60. data/lib/apipie/client/template/lib/a_name/version.rb.tt +0 -3
  61. data/lib/apipie/client/thor.rb +0 -21
  62. data/rubygem-apipie-rails.spec +0 -122
  63. data/spec/lib/parameter_description_spec.rb +0 -41
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Api::V1::ArchitecturesController do
4
+ describe "resource description" do
5
+ subject { Apipie.get_resource_description(Api::V1::ArchitecturesController, "1.0") }
6
+
7
+ it "should be version 1.0" do
8
+ subject._version.should eq('1.0')
9
+
10
+ Apipie.resource_descriptions['1.0'].size.should == 2
11
+ Apipie.resource_descriptions['1.0'].keys.should
12
+ include('architectures', 'base')
13
+ end
14
+
15
+ context "there is another version" do
16
+ let(:v2) { archv2 = Apipie.get_resource_description(Api::V2::ArchitecturesController, "2.0") }
17
+
18
+ it "should have unique doc url" do
19
+ subject.doc_url.should_not eq(v2.doc_url)
20
+ end
21
+
22
+ it "should have unique methods" do
23
+ subject._methods.keys.should include(:index)
24
+ v2._methods.keys.should include(:index)
25
+ subject._methods[:index].should_not eq(v2._methods[:index])
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Api::V2::ArchitecturesController do
4
+ describe "resource description" do
5
+ subject { Apipie.get_resource_description(Api::V2::ArchitecturesController, "2.0") }
6
+
7
+ it "should be version 2.0" do
8
+ subject._version.should eq('2.0')
9
+ end
10
+
11
+ end
12
+ end
@@ -101,30 +101,36 @@ describe Apipie::ApipiesController do
101
101
 
102
102
  before do
103
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" }
104
+ FileUtils.mkdir_p(File.join(cache_dir, "apidoc", "v1", "resource"))
105
+ File.open(File.join(cache_dir, "apidoc", "v1.html"), "w") { |f| f << "apidoc.html cache v1" }
106
+ File.open(File.join(cache_dir, "apidoc", "v2.html"), "w") { |f| f << "apidoc.html cache v2" }
107
+ File.open(File.join(cache_dir, "apidoc", "v1.json"), "w") { |f| f << "apidoc.json cache" }
108
+ File.open(File.join(cache_dir, "apidoc", "v1", "resource.html"), "w") { |f| f << "resource.html cache" }
109
+ File.open(File.join(cache_dir, "apidoc", "v1", "resource", "method.html"), "w") { |f| f << "method.html cache" }
109
110
 
110
111
  Apipie.configuration.use_cache = true
111
112
  Apipie.configuration.cache_dir = cache_dir
113
+ Apipie.configuration.default_version = 'v1'
112
114
  end
113
115
 
114
116
  after do
115
- FileUtils.rm_r(cache_dir) if File.exists?(cache_dir)
117
+ # FileUtils.rm_r(cache_dir) if File.exists?(cache_dir)
116
118
  end
117
119
 
118
120
  it "uses the file in cache dir instead of generating the content on runtime" do
119
121
  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"
122
+ response.body.should == "apidoc.html cache v1"
123
+ get :index, :version => 'v1'
124
+ response.body.should == "apidoc.html cache v1"
125
+ get :index, :version => 'v2'
126
+ response.body.should == "apidoc.html cache v2"
127
+ get :index, :version => 'v1', :format => "html"
128
+ response.body.should == "apidoc.html cache v1"
129
+ get :index, :version => 'v1', :format => "json"
124
130
  response.body.should == "apidoc.json cache"
125
- get :index, :format => "html", :resource => "resource"
131
+ get :index, :version => 'v1', :format => "html", :resource => "resource"
126
132
  response.body.should == "resource.html cache"
127
- get :index, :format => "html", :resource => "resource", :method => "method"
133
+ get :index, :version => 'v1', :format => "html", :resource => "resource", :method => "method"
128
134
  response.body.should == "method.html cache"
129
135
  end
130
136
 
@@ -21,23 +21,25 @@ end
21
21
  describe UsersController do
22
22
 
23
23
  describe "resource description" do
24
- subject { a = Apipie.get_resource_description(UsersController) }
24
+ subject do
25
+ Apipie.get_resource_description(UsersController, Apipie.configuration.default_version)
26
+ end
25
27
 
26
28
  it "should contain all resource methods" do
27
29
  methods = subject._methods
28
30
  methods.count.should == 5
29
- methods.should include("users#show")
30
- methods.should include("users#create")
31
- methods.should include("users#index")
32
- methods.should include("users#two_urls")
31
+ methods.keys.should include(:show)
32
+ methods.keys.should include(:index)
33
+ methods.keys.should include(:create)
34
+ methods.keys.should include(:two_urls)
33
35
  end
34
36
 
35
37
  it "should contain info about resource" do
36
38
  subject._short_description.should eq('Site members')
37
39
  subject._id.should eq('users')
38
40
  subject._path.should eq('/users')
39
- subject._version.should eq('1.0 - 3.4.2012')
40
- subject._name.should eq('Members')
41
+ subject._version.should eq('development')
42
+ subject._name.should eq('Users')
41
43
  subject._formats.should eq(['json'])
42
44
  end
43
45
 
@@ -210,30 +212,30 @@ describe UsersController do
210
212
  a.formats.should eq(['json'])
211
213
  api = a.apis.first
212
214
  api.short_description.should eq("Create user")
213
- api.api_url.should eq("/api/users")
215
+ api.path.should eq("/users")
214
216
  api.http_method.should eq("POST")
215
217
 
216
218
  b = Apipie.get_method_description(UsersController, :show)
217
219
  b.should eq(Apipie[UsersController, :show])
218
- b.method.should eq(:show)
220
+ b.method.should eq('show')
219
221
  b.resource._id.should eq('users')
220
222
 
221
223
  b.apis.count.should == 1
222
224
  b.formats.should eq(['json', 'jsonp'])
223
225
  api = b.apis.first
224
226
  api.short_description.should eq("Show user profile")
225
- api.api_url.should eq("#{Apipie.configuration.api_base_url}/users/:id")
227
+ api.path.should eq("/users/:id")
226
228
  api.http_method.should eq("GET")
227
229
  b.full_description.length.should be > 400
228
230
  end
229
231
 
230
232
  context "contain :see option" do
231
233
 
232
- context "the key is valid" do
234
+ context "the key is valid" do
233
235
  it "should contain reference to another method" do
234
236
  api = Apipie[UsersController, :see_another]
235
- api.see.should eq('users#create')
236
- Apipie['users#see_another'].should eq(Apipie[UsersController, :see_another])
237
+ api.see.should eq('development#users#create')
238
+ Apipie['development#users#see_another'].should eq(Apipie[UsersController, :see_another])
237
239
  api.see_url.should eq(Apipie[UsersController, :create].doc_url)
238
240
  end
239
241
  end
@@ -274,11 +276,11 @@ describe UsersController do
274
276
  a1, a2 = method_description.apis
275
277
 
276
278
  a1.short_description.should eq('Get company users')
277
- a1.api_url.should eq('/api/company_users')
279
+ a1.path.should eq('/company_users')
278
280
  a1.http_method.should eq('GET')
279
281
 
280
282
  a2.short_description.should eq('Get users working in given company')
281
- a2.api_url.should eq('/api/company/:id/users')
283
+ a2.path.should eq('/company/:id/users')
282
284
  a2.http_method.should eq('GET')
283
285
  end
284
286
 
@@ -288,7 +290,7 @@ describe UsersController do
288
290
  :errors => [{:code=>404, :description=>"Missing"},
289
291
  {:code=>500, :description=>"Server crashed for some <%= reason %>"}],
290
292
  :examples => [],
291
- :doc_url => "#{Apipie.configuration.doc_base_url}/users/two_urls",
293
+ :doc_url => "#{Apipie.configuration.doc_base_url}/development/users/two_urls",
292
294
  :formats=>["json"],
293
295
  :full_description => '',
294
296
  :params => [{:full_name=>"oauth",
@@ -326,16 +328,16 @@ describe UsersController do
326
328
  :name=>"id", :full_name=>"id",
327
329
  :expected_type=>"numeric"},
328
330
  ],
329
- :name => :two_urls,
331
+ :name => 'two_urls',
330
332
  :apis => [
331
333
  {
332
334
  :http_method => 'GET',
333
335
  :short_description => 'Get company users',
334
- :api_url => "#{Apipie.configuration.api_base_url}/company_users"
336
+ :api_url => "#{Apipie.api_base_url}/company_users"
335
337
  },{
336
338
  :http_method => 'GET',
337
339
  :short_description => 'Get users working in given company',
338
- :api_url =>"#{Apipie.configuration.api_base_url}/company/:id/users"
340
+ :api_url =>"#{Apipie.api_base_url}/company/:id/users"
339
341
  }
340
342
  ]
341
343
  }
@@ -401,4 +403,39 @@ EOS2
401
403
  end
402
404
 
403
405
  end
406
+
407
+ describe "ignored option" do
408
+ class IgnoredController < ApplicationController; end
409
+
410
+ after do
411
+ Apipie.configuration.ignored = %w[]
412
+ end
413
+
414
+ describe "ignored action" do
415
+ before do
416
+ Apipie.configuration.ignored = %w[UsersController#ignore]
417
+ end
418
+
419
+ it "skips the listed actions from the documentation" do
420
+ Apipie.define_method_description(UsersController, :ignore)
421
+ Apipie.get_method_description(UsersController, :ignore).should be_nil
422
+
423
+ Apipie.define_method_description(UsersController, :dont_ignore)
424
+ Apipie.get_method_description(UsersController, :dont_ignore).should_not be_nil
425
+ end
426
+ end
427
+
428
+ describe "ignored controller" do
429
+ before do
430
+ Apipie.configuration.ignored = %w[IgnoredController]
431
+ end
432
+
433
+ it "skips the listed controller from the documentation" do
434
+ Apipie.define_method_description(IgnoredController, :ignore)
435
+ Apipie.get_method_description(IgnoredController, :ignore).should be_nil
436
+ Apipie.define_method_description(IgnoredController, :ignore)
437
+ Apipie.get_method_description(IgnoredController, :ignore).should be_nil
438
+ end
439
+ end
440
+ end
404
441
  end
@@ -0,0 +1,4 @@
1
+ module Api
2
+ class BaseController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,32 @@
1
+ module Api
2
+ module V1
3
+ class ArchitecturesController < V1::BaseController
4
+ resource_description { name 'Architectures' }
5
+ api :GET, "/architectures/", "List all architectures."
6
+ def index
7
+ end
8
+
9
+ api :GET, "/architectures/:id/", "Show an architecture."
10
+ def show
11
+ end
12
+
13
+ api :POST, "/architectures/", "Create an architecture."
14
+ param :architecture, Hash, :required => true do
15
+ param :name, String, :required => true
16
+ end
17
+ def create
18
+ end
19
+
20
+ api :PUT, "/architectures/:id/", "Update an architecture."
21
+ param :architecture, Hash, :required => true do
22
+ param :name, String
23
+ end
24
+ def update
25
+ end
26
+
27
+ api :DELETE, "/architecturess/:id/", "Delete an architecture."
28
+ def destroy
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ module Api
2
+ module V1
3
+ class BaseController < Api::BaseController
4
+ resource_description do
5
+ api_version '1.0'
6
+ app_info 'Version 1.0 description'
7
+ api_base_url '/api/v1'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,32 @@
1
+ module Api
2
+ module V2
3
+ class ArchitecturesController < V2::BaseController
4
+ resource_description { name 'Architectures' }
5
+ api :GET, "/architectures/", "List all architectures."
6
+ def index
7
+ end
8
+
9
+ api :GET, "/architectures/:id/", "Show an architecture."
10
+ def show
11
+ end
12
+
13
+ api :POST, "/architectures/", "Create an architecture."
14
+ param :architecture, Hash, :required => true do
15
+ param :name, String, :required => true
16
+ end
17
+ def create
18
+ end
19
+
20
+ api :PUT, "/architectures/:id/", "Update an architecture."
21
+ param :architecture, Hash, :required => true do
22
+ param :name, String
23
+ end
24
+ def update
25
+ end
26
+
27
+ api :DELETE, "/architecturess/:id/", "Delete an architecture."
28
+ def destroy
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ module Api
2
+ module V2
3
+ class BaseController < Api::BaseController
4
+ resource_description do
5
+ api_version '2.0'
6
+ app_info 'Version 2.0 description'
7
+ api_base_url '/api/v2'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,7 +1,7 @@
1
1
  class TwitterExampleController < ApplicationController
2
2
 
3
3
  resource_description do
4
- name 'Users'
4
+ name 'TwitterUsers'
5
5
  short_description 'Users are at the center of everything Twitter: they follow, they favorite, and tweet & retweet.'
6
6
  path '/twitter_example/'
7
7
  description "Long description of this resource."
@@ -1,16 +1,15 @@
1
1
  class UsersController < ApplicationController
2
2
 
3
3
  resource_description do
4
- name 'Members'
5
4
  short 'Site members'
6
5
  path '/users'
7
- version '1.0 - 3.4.2012'
8
6
  formats ['json']
9
7
  param :id, Fixnum, :desc => "User ID", :required => false
10
8
  param :resource_param, Hash, :desc => 'Param description for all methods' do
11
9
  param :ausername, String, :desc => "Username for login", :required => true
12
10
  param :apassword, String, :desc => "Password for login", :required => true
13
11
  end
12
+ api_version "development"
14
13
  error 404, "Missing"
15
14
  error 500, "Server crashed for some <%= reason %>"
16
15
  description <<-EOS
@@ -220,7 +219,7 @@ class UsersController < ApplicationController
220
219
  end
221
220
 
222
221
  api :GET, '/users/see_another', 'Boring method'
223
- see 'users#create'
222
+ see 'development#users#create'
224
223
  desc 'This method is boring, look at users#create'
225
224
  def see_another
226
225
  render :text => 'This is very similar to create action'
@@ -1,14 +1,26 @@
1
1
  Apipie.configure do |config|
2
2
  config.app_name = "Test app"
3
3
  config.copyright = "&copy; 2012 Pavel Pokorny"
4
+
5
+ # set default API version
6
+ # can be overriden in resource_description
7
+ # by default is it 1.0 if not specified anywhere
8
+ # this must be defined before api_base_url and app_info
9
+ config.default_version = "development"
10
+
4
11
  config.doc_base_url = "/apidoc"
12
+
13
+ # default version base url
14
+ # to define base url for specifid version use
15
+ # config.api_base_url[version] = url
16
+ # or define it in your base controller
5
17
  config.api_base_url = "/api"
6
18
 
7
19
  # set to true to turn on/off the cache. To generate the cache use:
8
20
  #
9
21
  # rake apipie:cache
10
22
  #
11
- # config.use_cache = Rails.env.production?
23
+ config.use_cache = Rails.env.production?
12
24
  # config.cache_dir = File.join(Rails.root, "public", "apipie-cache") # optional
13
25
 
14
26
  # set to enable/disable reloading controllers (and the documentation with it),
@@ -18,16 +30,27 @@ Apipie.configure do |config|
18
30
  # for reloading to work properly you need to specify where your api controllers are (like in Dir.glob):
19
31
  config.api_controllers_matcher = File.join(Rails.root, "app", "controllers", "**","*.rb")
20
32
 
21
- # config.api_base_url = "/api"
22
33
  # config.markup = choose one of:
23
34
  # Apipie::Markup::RDoc.new [default]
24
35
  # Apipie::Markup::Markdown.new
25
36
  # Apipie::Markup::Textile.new
26
37
  # or provide another class with to_html(text) instance method
38
+ # to disable markup, use
39
+ # config.markup = nil
40
+
27
41
  # config.validate = false
28
42
 
29
- path = File.expand_path(File.dirname(__FILE__)+'/../../../../README.rdoc')
30
- config.app_info = File.read(path)
43
+ # set all parameters as required by default
44
+ # if enabled, use param :name, val, :required => false for optional params
45
+ config.required_by_default = false
46
+
47
+ # set default version info, to describe specific version use
48
+ # config.app_info[version] = description
49
+ # or put this in your base or application controller
50
+ config.app_info = "Dummy app for testing"
51
+
52
+ # show debug informations
53
+ config.debug = false
31
54
 
32
55
  # set all parameters as required by default
33
56
  # if enabled, use param :name, val, :required => false for optional params
@@ -59,7 +82,7 @@ class Apipie::Validator::IntegerValidator < Apipie::Validator::BaseValidator
59
82
 
60
83
  def self.build(param_description, argument, options, block)
61
84
  if argument == Integer || argument == Fixnum
62
- self.new(param_description, argument)
85
+ self.new(param_description, argument)
63
86
  end
64
87
  end
65
88
 
@@ -72,7 +95,7 @@ class Apipie::Validator::IntegerValidator < Apipie::Validator::BaseValidator
72
95
  def description
73
96
  "Parameter has to be #{@type}."
74
97
  end
75
-
98
+
76
99
  def expected_type
77
100
  'numeric'
78
101
  end