apipie-rails 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +3 -0
- data/.gitignore +6 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/.travis.yml +5 -0
- data/APACHE-LICENSE-2.0 +202 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +115 -0
- data/MIT-LICENSE +20 -0
- data/NOTICE +4 -0
- data/README.rdoc +365 -0
- data/Rakefile +13 -0
- data/apipie-rails.gemspec +27 -0
- data/app/controllers/apipie/apipies_controller.rb +60 -0
- data/app/public/apipie/javascripts/apipie.js +6 -0
- data/app/public/apipie/javascripts/bundled/bootstrap-collapse.js +138 -0
- data/app/public/apipie/javascripts/bundled/bootstrap.js +1726 -0
- data/app/public/apipie/javascripts/bundled/jquery-1.7.2.js +9404 -0
- data/app/public/apipie/javascripts/bundled/prettify.js +28 -0
- data/app/public/apipie/stylesheets/application.css +7 -0
- data/app/public/apipie/stylesheets/bundled/bootstrap-responsive.min.css +12 -0
- data/app/public/apipie/stylesheets/bundled/bootstrap.min.css +689 -0
- data/app/public/apipie/stylesheets/bundled/prettify.css +30 -0
- data/app/views/apipie/apipies/_params.html.erb +22 -0
- data/app/views/apipie/apipies/_params_plain.html.erb +16 -0
- data/app/views/apipie/apipies/index.html.erb +36 -0
- data/app/views/apipie/apipies/method.html.erb +63 -0
- data/app/views/apipie/apipies/plain.html.erb +70 -0
- data/app/views/apipie/apipies/resource.html.erb +82 -0
- data/app/views/apipie/apipies/static.html.erb +101 -0
- data/app/views/layouts/apipie/apipie.html.erb +37 -0
- data/lib/apipie-rails.rb +12 -0
- data/lib/apipie/apipie_module.rb +105 -0
- data/lib/apipie/application.rb +225 -0
- data/lib/apipie/client/generator.rb +105 -0
- data/lib/apipie/client/template/Gemfile.tt +5 -0
- data/lib/apipie/client/template/README.tt +3 -0
- data/lib/apipie/client/template/base.rb.tt +33 -0
- data/lib/apipie/client/template/bin.rb.tt +110 -0
- data/lib/apipie/client/template/cli.rb.tt +25 -0
- data/lib/apipie/client/template/cli_command.rb.tt +129 -0
- data/lib/apipie/client/template/client.rb.tt +10 -0
- data/lib/apipie/client/template/resource.rb.tt +17 -0
- data/lib/apipie/dsl_definition.rb +139 -0
- data/lib/apipie/error_description.rb +21 -0
- data/lib/apipie/extractor.rb +143 -0
- data/lib/apipie/extractor/collector.rb +113 -0
- data/lib/apipie/extractor/recorder.rb +122 -0
- data/lib/apipie/extractor/writer.rb +356 -0
- data/lib/apipie/helpers.rb +24 -0
- data/lib/apipie/markup.rb +45 -0
- data/lib/apipie/method_description.rb +150 -0
- data/lib/apipie/param_description.rb +87 -0
- data/lib/apipie/railtie.rb +9 -0
- data/lib/apipie/resource_description.rb +83 -0
- data/lib/apipie/routing.rb +13 -0
- data/lib/apipie/static_dispatcher.rb +60 -0
- data/lib/apipie/validator.rb +292 -0
- data/lib/apipie/version.rb +3 -0
- data/lib/tasks/apipie.rake +156 -0
- data/rel-eng/packages/.readme +3 -0
- data/rel-eng/tito.props +5 -0
- data/rubygem-apipie-rails.spec +72 -0
- data/spec/controllers/apipies_controller_spec.rb +132 -0
- data/spec/controllers/users_controller_spec.rb +390 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +6 -0
- data/spec/dummy/app/controllers/twitter_example_controller.rb +302 -0
- data/spec/dummy/app/controllers/users_controller.rb +223 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +45 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +21 -0
- data/spec/dummy/config/environment.rb +8 -0
- data/spec/dummy/config/environments/development.rb +25 -0
- data/spec/dummy/config/environments/production.rb +49 -0
- data/spec/dummy/config/environments/test.rb +35 -0
- data/spec/dummy/config/initializers/apipie.rb +64 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +21 -0
- data/spec/dummy/doc/apipie_examples.yml +28 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/javascripts/application.js +2 -0
- data/spec/dummy/public/javascripts/controls.js +965 -0
- data/spec/dummy/public/javascripts/dragdrop.js +974 -0
- data/spec/dummy/public/javascripts/effects.js +1123 -0
- data/spec/dummy/public/javascripts/prototype.js +6001 -0
- data/spec/dummy/public/javascripts/rails.js +202 -0
- data/spec/dummy/public/stylesheets/.gitkeep +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/spec_helper.rb +32 -0
- metadata +312 -0
data/rel-eng/tito.props
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
%define gem_name apipie-rails
|
2
|
+
|
3
|
+
Name: rubygem-%{gem_name}
|
4
|
+
Version: 0.0.6
|
5
|
+
Release: 1%{?dist}
|
6
|
+
Summary: Rails API documentation tool and client generator.
|
7
|
+
|
8
|
+
Group: Development/Libraries
|
9
|
+
License: MIT
|
10
|
+
URL: https://github.com/Pajk/apipie-rails
|
11
|
+
Source0: http://rubygems.org/downloads/%{gem_name}-%{version}.gem
|
12
|
+
|
13
|
+
%if 0%{?rhel} == 6 || 0%{?fedora} < 17
|
14
|
+
%define rubyabi 1.8
|
15
|
+
%else
|
16
|
+
%define rubyabi 1.9
|
17
|
+
%endif
|
18
|
+
Requires: ruby(abi) >= %{rubyabi}
|
19
|
+
Requires: ruby(rubygems)
|
20
|
+
Requires: ruby
|
21
|
+
BuildRequires: ruby(abi) >= %{rubyabi}
|
22
|
+
BuildRequires: rubygems-devel
|
23
|
+
BuildRequires: ruby
|
24
|
+
BuildArch: noarch
|
25
|
+
Provides: rubygem(%{gem_name}) = %{version}
|
26
|
+
|
27
|
+
%description
|
28
|
+
This gem adds new methods to Rails controllers that can be used to describe
|
29
|
+
resources exposed by API. Informations entered with provided DSL are used
|
30
|
+
to generate documentation, client or to validate incoming requests.
|
31
|
+
|
32
|
+
%prep
|
33
|
+
gem unpack %{SOURCE0}
|
34
|
+
%setup -q -D -T -n %{gem_name}-%{version}
|
35
|
+
gem spec %{SOURCE0} -l --ruby > %{gem_name}.gemspec
|
36
|
+
|
37
|
+
|
38
|
+
%build
|
39
|
+
mkdir -p .%{gem_dir}
|
40
|
+
|
41
|
+
# Create the gem as gem install only works on a gem file
|
42
|
+
gem build %{gem_name}.gemspec
|
43
|
+
|
44
|
+
# gem install compiles any C extensions and installs into a directory
|
45
|
+
# We set that to be a local directory so that we can move it into the
|
46
|
+
# buildroot in %%install
|
47
|
+
gem install -V \
|
48
|
+
--local \
|
49
|
+
--install-dir ./%{gem_dir} \
|
50
|
+
--bindir ./%{_bindir} \
|
51
|
+
--force \
|
52
|
+
--rdoc \
|
53
|
+
%{gem_name}-%{version}.gem
|
54
|
+
|
55
|
+
%install
|
56
|
+
mkdir -p %{buildroot}%{gem_dir}
|
57
|
+
cp -a ./%{gem_dir}/* %{buildroot}%{gem_dir}/
|
58
|
+
|
59
|
+
%files
|
60
|
+
%{gem_dir}/gems/%{gem_name}-%{version}/
|
61
|
+
|
62
|
+
|
63
|
+
%doc MIT-LICENSE README.rdoc
|
64
|
+
|
65
|
+
%{gem_dir}/cache/%{gem_name}-%{version}.gem
|
66
|
+
%{gem_dir}/specifications/%{gem_name}-%{version}.gemspec
|
67
|
+
%{gem_docdir}
|
68
|
+
|
69
|
+
%changelog
|
70
|
+
* Fri Jul 20 2012 Pavel Pokorný <pajkycz@gmail.com> 0.0.6-1
|
71
|
+
- new RPM package built with tito
|
72
|
+
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
describe Apipie::ApipiesController do
|
5
|
+
|
6
|
+
describe "GET index" do
|
7
|
+
|
8
|
+
it "test if route exists" do
|
9
|
+
get :index
|
10
|
+
|
11
|
+
assert_response :success
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "reload_controllers" do
|
17
|
+
|
18
|
+
RSpec::Matchers.define :reload_documentation do
|
19
|
+
match do
|
20
|
+
Apipie.should_receive(:reload_documentation)
|
21
|
+
get :index
|
22
|
+
begin
|
23
|
+
RSpec::Mocks.verify
|
24
|
+
rescue RSpec::Mocks::MockExpectationError
|
25
|
+
false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
failure_message_for_should { "the documentation expected to be reloaded but it was not" }
|
30
|
+
failure_message_for_should_not { "the documentation expected not to be reloaded but it was" }
|
31
|
+
end
|
32
|
+
|
33
|
+
before do
|
34
|
+
Apipie.configuration.api_controllers_matcher = File.join(Rails.root, "app", "controllers", "**","*.rb")
|
35
|
+
if Apipie.configuration.send :instance_variable_defined?, "@reload_controllers"
|
36
|
+
Apipie.configuration.send :remove_instance_variable, "@reload_controllers"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "it's not specified explicitly" do
|
41
|
+
context "and it's in development environment" do
|
42
|
+
before do
|
43
|
+
Rails.stub(:env => mock(:development? => true))
|
44
|
+
end
|
45
|
+
it { should reload_documentation }
|
46
|
+
end
|
47
|
+
|
48
|
+
context "and it's not development environment" do
|
49
|
+
it { should_not reload_documentation }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
context "it's explicitly enabled" do
|
55
|
+
before do
|
56
|
+
Apipie.configuration.reload_controllers = true
|
57
|
+
end
|
58
|
+
|
59
|
+
context "and it's in development environment" do
|
60
|
+
before do
|
61
|
+
Rails.stub(:env => mock(:development? => true))
|
62
|
+
end
|
63
|
+
it { should reload_documentation }
|
64
|
+
end
|
65
|
+
|
66
|
+
context "and it's not development environment" do
|
67
|
+
it { should reload_documentation }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "it's explicitly enabled" do
|
72
|
+
before do
|
73
|
+
Apipie.configuration.reload_controllers = false
|
74
|
+
end
|
75
|
+
|
76
|
+
context "and it's in development environment" do
|
77
|
+
before do
|
78
|
+
Rails.stub(:env => mock(:development? => true))
|
79
|
+
end
|
80
|
+
it { should_not reload_documentation }
|
81
|
+
end
|
82
|
+
|
83
|
+
context "and it's not development environment" do
|
84
|
+
it { should_not reload_documentation }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "api_controllers_matcher is specified" do
|
89
|
+
before do
|
90
|
+
Apipie.configuration.reload_controllers = true
|
91
|
+
Apipie.configuration.api_controllers_matcher = nil
|
92
|
+
end
|
93
|
+
|
94
|
+
it { should_not reload_documentation }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "documentation cache" do
|
99
|
+
|
100
|
+
let(:cache_dir) { File.join(Rails.root, "tmp", "apipie-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
|
+
Apipie.configuration.use_cache = true
|
111
|
+
Apipie.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
|
132
|
+
end
|
@@ -0,0 +1,390 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
def compare_hashes(h1, h2)
|
4
|
+
h1.each do |key, val|
|
5
|
+
if val.is_a? Hash
|
6
|
+
compare_hashes val, h2[key]
|
7
|
+
elsif val.is_a? Array
|
8
|
+
val.each_with_index do |v, i|
|
9
|
+
compare_hashes val[i], h2[key][i]
|
10
|
+
end
|
11
|
+
else
|
12
|
+
val.should eq(h2[key])
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe UsersController do
|
18
|
+
|
19
|
+
describe "resource description" do
|
20
|
+
subject { a = Apipie.get_resource_description(UsersController) }
|
21
|
+
|
22
|
+
it "should contain all resource methods" do
|
23
|
+
methods = subject._methods
|
24
|
+
methods.count.should == 5
|
25
|
+
methods.should include("users#show")
|
26
|
+
methods.should include("users#create")
|
27
|
+
methods.should include("users#index")
|
28
|
+
methods.should include("users#two_urls")
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should contain info about resource" do
|
32
|
+
subject._short_description.should eq('Site members')
|
33
|
+
subject._id.should eq('users')
|
34
|
+
subject._path.should eq('/users')
|
35
|
+
subject._version.should eq('1.0 - 3.4.2012')
|
36
|
+
subject._name.should eq('Members')
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should contain params defined on resource level" do
|
40
|
+
subject._params_ordered.count.should == 2
|
41
|
+
p = subject._params_ordered.first
|
42
|
+
p.should_not be(nil)
|
43
|
+
p.name.should eq(:id)
|
44
|
+
p.desc.should eq("\n<p>User ID</p>\n")
|
45
|
+
p.required.should eq(false)
|
46
|
+
p.validator.class.should eq(Apipie::Validator::IntegerValidator)
|
47
|
+
|
48
|
+
p = subject._params_ordered.second
|
49
|
+
p.should_not be(nil)
|
50
|
+
p.name.should eq(:resource_param)
|
51
|
+
p.desc.should eq("\n<p>Param description for all methods</p>\n")
|
52
|
+
p.required.should eq(false)
|
53
|
+
p.validator.class.should eq(Apipie::Validator::HashValidator)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "validators" do
|
58
|
+
|
59
|
+
context "validations are disabled" do
|
60
|
+
before { Apipie.configuration.validate = false }
|
61
|
+
|
62
|
+
it "should reply to valid request" do
|
63
|
+
get :show, :id => '5', :session => "secret_hash"
|
64
|
+
assert_response :success
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should pass if required parameter is missing" do
|
68
|
+
lambda { get :show, :id => 5 }.should_not raise_error
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
context "validations are enabled" do
|
75
|
+
before { Apipie.configuration.validate = true }
|
76
|
+
|
77
|
+
it "should reply to valid request" do
|
78
|
+
get :show, :id => '5', :session => "secret_hash"
|
79
|
+
assert_response :success
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should fail if required parameter is missing" do
|
83
|
+
lambda { get :show, :id => 5 }.should raise_error(ArgumentError, / session /)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should work with Type validator" do
|
87
|
+
lambda {
|
88
|
+
get :show,
|
89
|
+
:id => "not a number",
|
90
|
+
:session => "secret_hash"
|
91
|
+
}.should raise_error(ArgumentError, / id /)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should work with Regexp validator" do
|
95
|
+
get :show,
|
96
|
+
:id => 5,
|
97
|
+
:session => "secret_hash",
|
98
|
+
:regexp_param => "24 years"
|
99
|
+
assert_response :success
|
100
|
+
|
101
|
+
lambda {
|
102
|
+
get :show,
|
103
|
+
:id => 5,
|
104
|
+
:session => "secret_hash",
|
105
|
+
:regexp_param => "ten years"
|
106
|
+
}.should raise_error(ArgumentError, / regexp_param /)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should work with Array validator" do
|
110
|
+
get :show, :id => 5, :session => "secret_hash", :array_param => "one"
|
111
|
+
assert_response :success
|
112
|
+
get :show, :id => 5, :session => "secret_hash", :array_param => "two"
|
113
|
+
assert_response :success
|
114
|
+
get :show, :id => 5, :session => "secret_hash", :array_param => 1
|
115
|
+
assert_response :success
|
116
|
+
get :show, :id => 5, :session => "secret_hash", :boolean_param => false
|
117
|
+
assert_response :success
|
118
|
+
|
119
|
+
lambda {
|
120
|
+
get :show,
|
121
|
+
:id => 5,
|
122
|
+
:session => "secret_hash",
|
123
|
+
:array_param => "blabla"
|
124
|
+
}.should raise_error(ArgumentError, / array_param /)
|
125
|
+
|
126
|
+
lambda {
|
127
|
+
get :show,
|
128
|
+
:id => 5,
|
129
|
+
:session => "secret_hash",
|
130
|
+
:array_param => 3
|
131
|
+
}.should raise_error(ArgumentError, / array_param /)
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should work with Proc validator" do
|
135
|
+
lambda {
|
136
|
+
get :show,
|
137
|
+
:id => 5,
|
138
|
+
:session => "secret_hash",
|
139
|
+
:proc_param => "asdgsag"
|
140
|
+
}.should raise_error(ArgumentError, / proc_param /)
|
141
|
+
|
142
|
+
get :show,
|
143
|
+
:id => 5,
|
144
|
+
:session => "secret_hash",
|
145
|
+
:proc_param => "param value"
|
146
|
+
assert_response :success
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should work with Hash validator" do
|
150
|
+
post :create, :user => { :name => "root", :pass => "12345", :membership => "standard" }
|
151
|
+
assert_response :success
|
152
|
+
|
153
|
+
a = Apipie[UsersController, :create]
|
154
|
+
param = a.params_ordered.select {|p| p.name == :user }
|
155
|
+
param.count.should == 1
|
156
|
+
param.first.validator.class.should eq(Apipie::Validator::HashValidator)
|
157
|
+
hash_params = param.first.validator.hash_params_ordered
|
158
|
+
hash_params.count.should == 3
|
159
|
+
hash_params[0].name == :name
|
160
|
+
hash_params[1].name == :pass
|
161
|
+
hash_params[2].name == :membership
|
162
|
+
|
163
|
+
lambda {
|
164
|
+
post :create, :user => { :name => "root", :pass => "12345", :membership => "____" }
|
165
|
+
}.should raise_error(ArgumentError, / membership /)
|
166
|
+
|
167
|
+
lambda {
|
168
|
+
post :create, :user => { :name => "root" }
|
169
|
+
}.should raise_error(ArgumentError, / pass /)
|
170
|
+
|
171
|
+
post :create, :user => { :name => "root", :pass => "pwd" }
|
172
|
+
assert_response :success
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should support Hash validator without specifying keys" do
|
176
|
+
params = Apipie[UsersController, :create].to_json[:params]
|
177
|
+
params.should include(:name => "facts",
|
178
|
+
:full_name => "facts",
|
179
|
+
:validator => "Parameter has to be Hash.",
|
180
|
+
:description => "\n<p>Additional optional facts about the user</p>\n",
|
181
|
+
:required => false,
|
182
|
+
:allow_nil => true,
|
183
|
+
:expected_type => "hash")
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should allow nil when allow_nil is set to true" do
|
187
|
+
post :create,
|
188
|
+
:user => {
|
189
|
+
:name => "root",
|
190
|
+
:pass => "12345",
|
191
|
+
:membership => "standard",
|
192
|
+
},
|
193
|
+
:facts => nil
|
194
|
+
assert_response :success
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
describe "method description" do
|
201
|
+
|
202
|
+
it "should contain basic info about method" do
|
203
|
+
a = Apipie[UsersController, :create]
|
204
|
+
a.apis.count.should == 1
|
205
|
+
api = a.apis.first
|
206
|
+
api.short_description.should eq("Create user")
|
207
|
+
api.api_url.should eq("/api/users")
|
208
|
+
api.http_method.should eq("POST")
|
209
|
+
|
210
|
+
b = Apipie.get_method_description(UsersController, :show)
|
211
|
+
b.should eq(Apipie[UsersController, :show])
|
212
|
+
b.method.should eq(:show)
|
213
|
+
b.resource._id.should eq('users')
|
214
|
+
|
215
|
+
b.apis.count.should == 1
|
216
|
+
api = b.apis.first
|
217
|
+
api.short_description.should eq("Show user profile")
|
218
|
+
api.api_url.should eq("#{Apipie.configuration.api_base_url}/users/:id")
|
219
|
+
api.http_method.should eq("GET")
|
220
|
+
b.full_description.length.should be > 400
|
221
|
+
end
|
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 = Apipie[UsersController, :see_another]
|
228
|
+
api.see.should eq('users#create')
|
229
|
+
Apipie['users#see_another'].should eq(Apipie[UsersController, :see_another])
|
230
|
+
api.see_url.should eq(Apipie[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 = Apipie[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
|
+
|
246
|
+
it "should contain possible errors description" do
|
247
|
+
a = Apipie.get_method_description(UsersController, :show)
|
248
|
+
|
249
|
+
a.errors[0].code.should eq(401)
|
250
|
+
a.errors[0].description.should eq("Unauthorized")
|
251
|
+
a.errors[1].code.should eq(404)
|
252
|
+
a.errors[1].description.should eq("Not Found")
|
253
|
+
end
|
254
|
+
|
255
|
+
it "should contain all params description" do
|
256
|
+
a = Apipie.get_method_description(UsersController, :show)
|
257
|
+
a.params.count.should == 8
|
258
|
+
a.instance_variable_get('@params_ordered').count.should == 6
|
259
|
+
end
|
260
|
+
|
261
|
+
it "should contain all api method description" do
|
262
|
+
method_description = Apipie[UsersController, :two_urls]
|
263
|
+
method_description.class.should be(Apipie::MethodDescription)
|
264
|
+
method_description.apis.count.should == 2
|
265
|
+
a1, a2 = method_description.apis
|
266
|
+
|
267
|
+
a1.short_description.should eq('Get company users')
|
268
|
+
a1.api_url.should eq('/api/company_users')
|
269
|
+
a1.http_method.should eq('GET')
|
270
|
+
|
271
|
+
a2.short_description.should eq('Get users working in given company')
|
272
|
+
a2.api_url.should eq('/api/company/:id/users')
|
273
|
+
a2.http_method.should eq('GET')
|
274
|
+
end
|
275
|
+
|
276
|
+
it "should be described by valid json" do
|
277
|
+
json = Apipie[UsersController, :two_urls].to_json
|
278
|
+
expected_hash = {
|
279
|
+
:errors => [],
|
280
|
+
:examples => [],
|
281
|
+
:doc_url => "#{Apipie.configuration.doc_base_url}/users/two_urls",
|
282
|
+
:full_description => '',
|
283
|
+
:params => [{:full_name=>"oauth",
|
284
|
+
:required=>false,
|
285
|
+
:allow_nil => false,
|
286
|
+
:validator=>"Parameter has to be String.",
|
287
|
+
:description=>"\n<p>Authorization</p>\n",
|
288
|
+
:name=>"oauth",
|
289
|
+
:expected_type=>"string"},
|
290
|
+
{:validator=>"Has to be hash.",
|
291
|
+
:description=>"\n<p>Param description for all methods</p>\n",
|
292
|
+
:expected_type=>"hash",
|
293
|
+
:allow_nil=>false,
|
294
|
+
:name=>"resource_param",
|
295
|
+
:required=>false,
|
296
|
+
:full_name=>"resource_param",
|
297
|
+
:params=>
|
298
|
+
[{:required=>true,
|
299
|
+
:allow_nil => false,
|
300
|
+
:validator=>"Parameter has to be String.",
|
301
|
+
:description=>"\n<p>Username for login</p>\n",
|
302
|
+
:name=>"ausername", :full_name=>"resource_param[ausername]",
|
303
|
+
:expected_type=>"string"},
|
304
|
+
{:required=>true,
|
305
|
+
:allow_nil => false,
|
306
|
+
:validator=>"Parameter has to be String.",
|
307
|
+
:description=>"\n<p>Password for login</p>\n",
|
308
|
+
:name=>"apassword", :full_name=>"resource_param[apassword]",
|
309
|
+
:expected_type=>"string"}
|
310
|
+
]
|
311
|
+
},
|
312
|
+
{:required=>false, :validator=>"Parameter has to be Integer.",
|
313
|
+
:allow_nil => false,
|
314
|
+
:description=>"\n<p>Company ID</p>\n",
|
315
|
+
:name=>"id", :full_name=>"id",
|
316
|
+
:expected_type=>"numeric"},
|
317
|
+
],
|
318
|
+
:name => :two_urls,
|
319
|
+
:apis => [
|
320
|
+
{
|
321
|
+
:http_method => 'GET',
|
322
|
+
:short_description => 'Get company users',
|
323
|
+
:api_url => "#{Apipie.configuration.api_base_url}/company_users"
|
324
|
+
},{
|
325
|
+
:http_method => 'GET',
|
326
|
+
:short_description => 'Get users working in given company',
|
327
|
+
:api_url =>"#{Apipie.configuration.api_base_url}/company/:id/users"
|
328
|
+
}
|
329
|
+
]
|
330
|
+
}
|
331
|
+
|
332
|
+
compare_hashes json, expected_hash
|
333
|
+
end
|
334
|
+
|
335
|
+
end
|
336
|
+
|
337
|
+
describe "examples" do
|
338
|
+
|
339
|
+
it "should be able to load examples from yml file" do
|
340
|
+
Apipie.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
|
+
|
353
|
+
describe "param description" do
|
354
|
+
|
355
|
+
it "should contain all specified information" do
|
356
|
+
a = Apipie.get_method_description(UsersController, :show)
|
357
|
+
|
358
|
+
param = a.params[:session]
|
359
|
+
param.required.should eq(true)
|
360
|
+
param.desc.should eq("\n<p>user is logged in</p>\n")
|
361
|
+
param.validator.class.should be(Apipie::Validator::TypeValidator)
|
362
|
+
param.validator.instance_variable_get("@type").should eq(String)
|
363
|
+
|
364
|
+
param = a.params[:id]
|
365
|
+
param.required.should eq(true)
|
366
|
+
param.desc.should eq("\n<p>user id</p>\n")
|
367
|
+
param.validator.class.should be(Apipie::Validator::IntegerValidator)
|
368
|
+
param.validator.instance_variable_get("@type").should eq(Integer)
|
369
|
+
|
370
|
+
param = a.params[:regexp_param]
|
371
|
+
param.desc.should eq("\n<p>regexp param</p>\n")
|
372
|
+
param.required.should eq(false)
|
373
|
+
param.validator.class.should be(Apipie::Validator::RegexpValidator)
|
374
|
+
param.validator.instance_variable_get("@regexp").should
|
375
|
+
eq(/^[0-9]* years/)
|
376
|
+
|
377
|
+
param = a.params[:array_param]
|
378
|
+
param.desc.should eq("\n<p>array validator</p>\n")
|
379
|
+
param.validator.class.should be(Apipie::Validator::ArrayValidator)
|
380
|
+
param.validator.instance_variable_get("@array").should
|
381
|
+
eq([100, "one", "two", 1, 2])
|
382
|
+
|
383
|
+
param = a.params[:proc_param]
|
384
|
+
param.desc.should eq("\n<p>proc validator</p>\n")
|
385
|
+
param.validator.class.should be(Apipie::Validator::ProcValidator)
|
386
|
+
end
|
387
|
+
|
388
|
+
end
|
389
|
+
|
390
|
+
end
|