jmoses_apipie-rails 0.0.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. data/.gitignore +11 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +4 -0
  4. data/APACHE-LICENSE-2.0 +202 -0
  5. data/CHANGELOG +55 -0
  6. data/Gemfile +3 -0
  7. data/MIT-LICENSE +20 -0
  8. data/NOTICE +4 -0
  9. data/README.rst +938 -0
  10. data/Rakefile +13 -0
  11. data/apipie-rails.gemspec +26 -0
  12. data/app/controllers/apipie/apipies_controller.rb +105 -0
  13. data/app/public/apipie/javascripts/apipie.js +6 -0
  14. data/app/public/apipie/javascripts/bundled/bootstrap-collapse.js +138 -0
  15. data/app/public/apipie/javascripts/bundled/bootstrap.js +1726 -0
  16. data/app/public/apipie/javascripts/bundled/jquery-1.7.2.js +9404 -0
  17. data/app/public/apipie/javascripts/bundled/prettify.js +28 -0
  18. data/app/public/apipie/stylesheets/application.css +20 -0
  19. data/app/public/apipie/stylesheets/bundled/bootstrap-responsive.min.css +12 -0
  20. data/app/public/apipie/stylesheets/bundled/bootstrap.min.css +689 -0
  21. data/app/public/apipie/stylesheets/bundled/prettify.css +30 -0
  22. data/app/views/apipie/apipies/_disqus.html.erb +11 -0
  23. data/app/views/apipie/apipies/_params.html.erb +29 -0
  24. data/app/views/apipie/apipies/_params_plain.html.erb +16 -0
  25. data/app/views/apipie/apipies/apipie_404.html.erb +12 -0
  26. data/app/views/apipie/apipies/getting_started.html.erb +4 -0
  27. data/app/views/apipie/apipies/index.html.erb +43 -0
  28. data/app/views/apipie/apipies/method.html.erb +71 -0
  29. data/app/views/apipie/apipies/plain.html.erb +70 -0
  30. data/app/views/apipie/apipies/resource.html.erb +98 -0
  31. data/app/views/apipie/apipies/static.html.erb +101 -0
  32. data/app/views/layouts/apipie/apipie.html.erb +26 -0
  33. data/lib/apipie-rails.rb +15 -0
  34. data/lib/apipie/apipie_module.rb +62 -0
  35. data/lib/apipie/application.rb +334 -0
  36. data/lib/apipie/client/generator.rb +135 -0
  37. data/lib/apipie/configuration.rb +128 -0
  38. data/lib/apipie/dsl_definition.rb +379 -0
  39. data/lib/apipie/error_description.rb +25 -0
  40. data/lib/apipie/errors.rb +38 -0
  41. data/lib/apipie/extractor.rb +151 -0
  42. data/lib/apipie/extractor/collector.rb +113 -0
  43. data/lib/apipie/extractor/recorder.rb +124 -0
  44. data/lib/apipie/extractor/writer.rb +367 -0
  45. data/lib/apipie/helpers.rb +52 -0
  46. data/lib/apipie/markup.rb +48 -0
  47. data/lib/apipie/method_description.rb +191 -0
  48. data/lib/apipie/param_description.rb +204 -0
  49. data/lib/apipie/railtie.rb +9 -0
  50. data/lib/apipie/resource_description.rb +102 -0
  51. data/lib/apipie/routing.rb +15 -0
  52. data/lib/apipie/see_description.rb +39 -0
  53. data/lib/apipie/static_dispatcher.rb +59 -0
  54. data/lib/apipie/validator.rb +310 -0
  55. data/lib/apipie/version.rb +3 -0
  56. data/lib/generators/apipie/install/README +6 -0
  57. data/lib/generators/apipie/install/install_generator.rb +25 -0
  58. data/lib/generators/apipie/install/templates/initializer.rb.erb +7 -0
  59. data/lib/tasks/apipie.rake +166 -0
  60. data/rel-eng/packages/.readme +3 -0
  61. data/rel-eng/packages/rubygem-apipie-rails +1 -0
  62. data/rel-eng/tito.props +5 -0
  63. data/spec/controllers/api/v1/architectures_controller_spec.rb +30 -0
  64. data/spec/controllers/api/v2/architectures_controller_spec.rb +12 -0
  65. data/spec/controllers/api/v2/nested/resources_controller_spec.rb +11 -0
  66. data/spec/controllers/apipies_controller_spec.rb +141 -0
  67. data/spec/controllers/concerns_controller_spec.rb +42 -0
  68. data/spec/controllers/users_controller_spec.rb +473 -0
  69. data/spec/dummy/Rakefile +7 -0
  70. data/spec/dummy/app/controllers/api/base_controller.rb +4 -0
  71. data/spec/dummy/app/controllers/api/v1/architectures_controller.rb +42 -0
  72. data/spec/dummy/app/controllers/api/v1/base_controller.rb +11 -0
  73. data/spec/dummy/app/controllers/api/v2/architectures_controller.rb +30 -0
  74. data/spec/dummy/app/controllers/api/v2/base_controller.rb +11 -0
  75. data/spec/dummy/app/controllers/api/v2/nested/architectures_controller.rb +30 -0
  76. data/spec/dummy/app/controllers/api/v2/nested/resources_controller.rb +33 -0
  77. data/spec/dummy/app/controllers/application_controller.rb +6 -0
  78. data/spec/dummy/app/controllers/concerns/sample_controller.rb +39 -0
  79. data/spec/dummy/app/controllers/concerns_controller.rb +8 -0
  80. data/spec/dummy/app/controllers/twitter_example_controller.rb +302 -0
  81. data/spec/dummy/app/controllers/users_controller.rb +251 -0
  82. data/spec/dummy/app/views/layouts/application.html.erb +21 -0
  83. data/spec/dummy/config.ru +4 -0
  84. data/spec/dummy/config/application.rb +45 -0
  85. data/spec/dummy/config/boot.rb +10 -0
  86. data/spec/dummy/config/database.yml +21 -0
  87. data/spec/dummy/config/environment.rb +8 -0
  88. data/spec/dummy/config/environments/development.rb +25 -0
  89. data/spec/dummy/config/environments/production.rb +49 -0
  90. data/spec/dummy/config/environments/test.rb +35 -0
  91. data/spec/dummy/config/initializers/apipie.rb +102 -0
  92. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  93. data/spec/dummy/config/initializers/inflections.rb +10 -0
  94. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  95. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  96. data/spec/dummy/config/initializers/session_store.rb +8 -0
  97. data/spec/dummy/config/locales/en.yml +5 -0
  98. data/spec/dummy/config/routes.rb +22 -0
  99. data/spec/dummy/db/.gitkeep +0 -0
  100. data/spec/dummy/doc/apipie_examples.yml +28 -0
  101. data/spec/dummy/public/404.html +26 -0
  102. data/spec/dummy/public/422.html +26 -0
  103. data/spec/dummy/public/500.html +26 -0
  104. data/spec/dummy/public/favicon.ico +0 -0
  105. data/spec/dummy/public/javascripts/application.js +2 -0
  106. data/spec/dummy/public/javascripts/controls.js +965 -0
  107. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  108. data/spec/dummy/public/javascripts/effects.js +1123 -0
  109. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  110. data/spec/dummy/public/javascripts/rails.js +202 -0
  111. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  112. data/spec/dummy/script/rails +6 -0
  113. data/spec/lib/application_spec.rb +38 -0
  114. data/spec/lib/method_description_spec.rb +30 -0
  115. data/spec/lib/param_description_spec.rb +174 -0
  116. data/spec/lib/param_group_spec.rb +45 -0
  117. data/spec/lib/resource_description_spec.rb +30 -0
  118. data/spec/lib/validator_spec.rb +46 -0
  119. data/spec/spec_helper.rb +32 -0
  120. metadata +337 -0
@@ -0,0 +1,202 @@
1
+ (function() {
2
+ Ajax.Responders.register({
3
+ onCreate: function(request) {
4
+ var token = $$('meta[name=csrf-token]')[0];
5
+ if (token) {
6
+ if (!request.options.requestHeaders) request.options.requestHeaders = {};
7
+ request.options.requestHeaders['X-CSRF-Token'] = token.readAttribute('content');
8
+ }
9
+ }
10
+ });
11
+
12
+ // Technique from Juriy Zaytsev
13
+ // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
14
+ function isEventSupported(eventName) {
15
+ var el = document.createElement('div');
16
+ eventName = 'on' + eventName;
17
+ var isSupported = (eventName in el);
18
+ if (!isSupported) {
19
+ el.setAttribute(eventName, 'return;');
20
+ isSupported = typeof el[eventName] == 'function';
21
+ }
22
+ el = null;
23
+ return isSupported;
24
+ }
25
+
26
+ function isForm(element) {
27
+ return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM';
28
+ }
29
+
30
+ function isInput(element) {
31
+ if (Object.isElement(element)) {
32
+ var name = element.nodeName.toUpperCase();
33
+ return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA';
34
+ }
35
+ else return false;
36
+ }
37
+
38
+ var submitBubbles = isEventSupported('submit'),
39
+ changeBubbles = isEventSupported('change');
40
+
41
+ if (!submitBubbles || !changeBubbles) {
42
+ // augment the Event.Handler class to observe custom events when needed
43
+ Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
44
+ function(init, element, eventName, selector, callback) {
45
+ init(element, eventName, selector, callback);
46
+ // is the handler being attached to an element that doesn't support this event?
47
+ if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
48
+ (!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
49
+ // "submit" => "emulated:submit"
50
+ this.eventName = 'emulated:' + this.eventName;
51
+ }
52
+ }
53
+ );
54
+ }
55
+
56
+ if (!submitBubbles) {
57
+ // discover forms on the page by observing focus events which always bubble
58
+ document.on('focusin', 'form', function(focusEvent, form) {
59
+ // special handler for the real "submit" event (one-time operation)
60
+ if (!form.retrieve('emulated:submit')) {
61
+ form.on('submit', function(submitEvent) {
62
+ var emulated = form.fire('emulated:submit', submitEvent, true);
63
+ // if custom event received preventDefault, cancel the real one too
64
+ if (emulated.returnValue === false) submitEvent.preventDefault();
65
+ });
66
+ form.store('emulated:submit', true);
67
+ }
68
+ });
69
+ }
70
+
71
+ if (!changeBubbles) {
72
+ // discover form inputs on the page
73
+ document.on('focusin', 'input, select, textarea', function(focusEvent, input) {
74
+ // special handler for real "change" events
75
+ if (!input.retrieve('emulated:change')) {
76
+ input.on('change', function(changeEvent) {
77
+ input.fire('emulated:change', changeEvent, true);
78
+ });
79
+ input.store('emulated:change', true);
80
+ }
81
+ });
82
+ }
83
+
84
+ function handleRemote(element) {
85
+ var method, url, params;
86
+
87
+ var event = element.fire("ajax:before");
88
+ if (event.stopped) return false;
89
+
90
+ if (element.tagName.toLowerCase() === 'form') {
91
+ method = element.readAttribute('method') || 'post';
92
+ url = element.readAttribute('action');
93
+ // serialize the form with respect to the submit button that was pressed
94
+ params = element.serialize({ submit: element.retrieve('rails:submit-button') });
95
+ // clear the pressed submit button information
96
+ element.store('rails:submit-button', null);
97
+ } else {
98
+ method = element.readAttribute('data-method') || 'get';
99
+ url = element.readAttribute('href');
100
+ params = {};
101
+ }
102
+
103
+ new Ajax.Request(url, {
104
+ method: method,
105
+ parameters: params,
106
+ evalScripts: true,
107
+
108
+ onCreate: function(response) { element.fire("ajax:create", response); },
109
+ onComplete: function(response) { element.fire("ajax:complete", response); },
110
+ onSuccess: function(response) { element.fire("ajax:success", response); },
111
+ onFailure: function(response) { element.fire("ajax:failure", response); }
112
+ });
113
+
114
+ element.fire("ajax:after");
115
+ }
116
+
117
+ function insertHiddenField(form, name, value) {
118
+ form.insert(new Element('input', { type: 'hidden', name: name, value: value }));
119
+ }
120
+
121
+ function handleMethod(element) {
122
+ var method = element.readAttribute('data-method'),
123
+ url = element.readAttribute('href'),
124
+ csrf_param = $$('meta[name=csrf-param]')[0],
125
+ csrf_token = $$('meta[name=csrf-token]')[0];
126
+
127
+ var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
128
+ $(element.parentNode).insert(form);
129
+
130
+ if (method !== 'post') {
131
+ insertHiddenField(form, '_method', method);
132
+ }
133
+
134
+ if (csrf_param) {
135
+ insertHiddenField(form, csrf_param.readAttribute('content'), csrf_token.readAttribute('content'));
136
+ }
137
+
138
+ form.submit();
139
+ }
140
+
141
+ function disableFormElements(form) {
142
+ form.select('input[type=submit][data-disable-with]').each(function(input) {
143
+ input.store('rails:original-value', input.getValue());
144
+ input.setValue(input.readAttribute('data-disable-with')).disable();
145
+ });
146
+ }
147
+
148
+ function enableFormElements(form) {
149
+ form.select('input[type=submit][data-disable-with]').each(function(input) {
150
+ input.setValue(input.retrieve('rails:original-value')).enable();
151
+ });
152
+ }
153
+
154
+ function allowAction(element) {
155
+ var message = element.readAttribute('data-confirm');
156
+ return !message || confirm(message);
157
+ }
158
+
159
+ document.on('click', 'a[data-confirm], a[data-remote], a[data-method]', function(event, link) {
160
+ if (!allowAction(link)) {
161
+ event.stop();
162
+ return false;
163
+ }
164
+
165
+ if (link.readAttribute('data-remote')) {
166
+ handleRemote(link);
167
+ event.stop();
168
+ } else if (link.readAttribute('data-method')) {
169
+ handleMethod(link);
170
+ event.stop();
171
+ }
172
+ });
173
+
174
+ document.on("click", "form input[type=submit], form button[type=submit], form button:not([type])", function(event, button) {
175
+ // register the pressed submit button
176
+ event.findElement('form').store('rails:submit-button', button.name || false);
177
+ });
178
+
179
+ document.on("submit", function(event) {
180
+ var form = event.findElement();
181
+
182
+ if (!allowAction(form)) {
183
+ event.stop();
184
+ return false;
185
+ }
186
+
187
+ if (form.readAttribute('data-remote')) {
188
+ handleRemote(form);
189
+ event.stop();
190
+ } else {
191
+ disableFormElements(form);
192
+ }
193
+ });
194
+
195
+ document.on('ajax:create', 'form', function(event, form) {
196
+ if (form == event.findElement()) disableFormElements(form);
197
+ });
198
+
199
+ document.on('ajax:complete', 'form', function(event, form) {
200
+ if (form == event.findElement()) enableFormElements(form);
201
+ });
202
+ })();
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+
3
+ describe Apipie::Application do
4
+
5
+ describe "get_resource_name" do
6
+ subject {Apipie.get_resource_name(Api::V2::Nested::ArchitecturesController)}
7
+
8
+ context "with namespaced_resources enabled" do
9
+ before { Apipie.configuration.namespaced_resources = true }
10
+ context "with a defined base url" do
11
+
12
+ it "should not overwrite the parent resource" do
13
+ should_not eq(Apipie.get_resource_name(Api::V2::ArchitecturesController))
14
+ end
15
+
16
+ end
17
+
18
+ context "with an undefined base url" do
19
+ before {Apipie.app.stub(:get_base_url) { nil }}
20
+
21
+ it "should not raise an error" do
22
+ expect { Apipie.get_resource_name(Api::V2::ArchitecturesController) }.
23
+ not_to raise_error
24
+ end
25
+ end
26
+
27
+ after { Apipie.configuration.namespaced_resources = false }
28
+ end
29
+
30
+ context "with namespaced_resources enabled" do
31
+ before { Apipie.configuration.namespaced_resources = false }
32
+
33
+ it "should overwrite the the parent" do
34
+ should eq(Apipie.get_resource_name(Api::V2::ArchitecturesController))
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,30 @@
1
+ require "spec_helper"
2
+
3
+ describe Apipie::MethodDescription do
4
+
5
+ let(:dsl_data) { ActionController::Base.send(:_apipie_dsl_data_init) }
6
+
7
+ describe "params descriptions" do
8
+
9
+ before(:each) do
10
+ @resource = Apipie::ResourceDescription.new(ApplicationController, "dummy")
11
+ dsl_data[:params] = [[:a, String, nil, {}, nil],
12
+ [:b, String, nil, {}, nil],
13
+ [:c, String, nil, {}, nil]]
14
+ @method = Apipie::MethodDescription.new(:a, @resource, dsl_data)
15
+ @resource.add_method_description @method
16
+ end
17
+
18
+ it "should be ordered" do
19
+ @method.params.keys.should == [:a, :b, :c]
20
+ @method.to_json[:params].map{|h| h[:name]}.should == ['a', 'b', 'c']
21
+ end
22
+
23
+ it "should be still ordered" do
24
+ @method.params.keys.should == [:a, :b, :c]
25
+ @method.to_json[:params].map{|h| h[:name]}.should == ['a', 'b', 'c']
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,174 @@
1
+ require "spec_helper"
2
+
3
+ describe Apipie::ParamDescription do
4
+
5
+ let(:dsl_data) { ActionController::Base.send(:_apipie_dsl_data_init) }
6
+
7
+ let(:resource_desc) do
8
+ Apipie::ResourceDescription.new(UsersController, "users")
9
+ end
10
+
11
+ let(:method_desc) do
12
+ Apipie::MethodDescription.new(:show, resource_desc, dsl_data)
13
+ end
14
+
15
+ describe "validator selection" do
16
+
17
+ it "should allow nil validator" do
18
+ param = Apipie::ParamDescription.new(method_desc, :hidden_param, nil)
19
+ param.validator.should be_nil
20
+ end
21
+
22
+ it "should throw exception on unknown validator" do
23
+ proc { Apipie::ParamDescription.new(method_desc, :param, :unknown) }.should raise_error(RuntimeError, /Validator.*not found/)
24
+ end
25
+
26
+ it "should pick type validator" do
27
+ Apipie::Validator::BaseValidator.should receive(:find).and_return(:validator_instance)
28
+ param = Apipie::ParamDescription.new(method_desc, :param, String)
29
+ param.validator.should == :validator_instance
30
+ end
31
+
32
+ end
33
+
34
+ describe "concern substitution" do
35
+
36
+ let(:concern_dsl_data) { dsl_data.merge(:from_concern => true) }
37
+
38
+ let(:concern_resource_desc) do
39
+ Apipie::ResourceDescription.new(ConcernsController, "concerns")
40
+ end
41
+
42
+ let(:concern_method_desc) do
43
+ Apipie::MethodDescription.new(:show, concern_resource_desc, concern_dsl_data)
44
+ end
45
+
46
+ it "should replace string parameter name with colon prefix" do
47
+ param = Apipie::ParamDescription.new(concern_method_desc, ":string_subst", String)
48
+ param.name.should == "string"
49
+ end
50
+
51
+ it "should replace symbol parameter name" do
52
+ param = Apipie::ParamDescription.new(concern_method_desc, :concern, String)
53
+ param.name.should == :user
54
+ end
55
+
56
+ it "should keep original value for strings without colon prefixes" do
57
+ param = Apipie::ParamDescription.new(concern_method_desc, "string_subst", String)
58
+ param.name.should == "string_subst"
59
+ end
60
+
61
+ it "should keep the original value when a string can't be replaced" do
62
+ param = Apipie::ParamDescription.new(concern_method_desc, ":param", String)
63
+ param.name.should == ":param"
64
+ end
65
+
66
+ it "should keep the original value when a symbol can't be replaced" do
67
+ param = Apipie::ParamDescription.new(concern_method_desc, :param, String)
68
+ param.name.should == :param
69
+ end
70
+ end
71
+
72
+
73
+ describe "required_by_default config option" do
74
+ context "parameters required by default" do
75
+
76
+ before { Apipie.configuration.required_by_default = true }
77
+
78
+ it "should set param as required by default" do
79
+ param = Apipie::ParamDescription.new(method_desc, :required_by_default, String)
80
+ param.required.should be_true
81
+ end
82
+
83
+ it "should be possible to set param as optional" do
84
+ param = Apipie::ParamDescription.new(method_desc, :optional, String, :required => false)
85
+ param.required.should be_false
86
+ end
87
+
88
+ end
89
+
90
+ context "parameters optional by default" do
91
+
92
+ before { Apipie.configuration.required_by_default = false }
93
+
94
+ it "should set param as optional by default" do
95
+ param = Apipie::ParamDescription.new(method_desc, :optional_by_default, String)
96
+ param.required.should be_false
97
+ end
98
+
99
+ it "should be possible to set param as required" do
100
+ param = Apipie::ParamDescription.new(method_desc, :required, String, 'description','required' => true)
101
+ param.required.should be_true
102
+ end
103
+
104
+ end
105
+
106
+ end
107
+
108
+ describe "required params in action aware validator" do
109
+
110
+ subject { method_description.params[:user].validator.hash_params_ordered }
111
+
112
+ let(:required) do
113
+ subject.find_all(&:required).map(&:name)
114
+ end
115
+
116
+ let(:allowed_nil) do
117
+ subject.find_all(&:allow_nil).map(&:name)
118
+ end
119
+
120
+ context "with resource creation" do
121
+
122
+ let(:method_description) do
123
+ Apipie.get_method_description(UsersController, :create)
124
+ end
125
+
126
+ it "makes the param required" do
127
+ required.should include :name
128
+ required.should include :pass
129
+ end
130
+
131
+ it "doesn't allow nil" do
132
+ allowed_nil.should_not include :name
133
+ allowed_nil.should_not include :pass
134
+ end
135
+ end
136
+
137
+ context "with resource update" do
138
+
139
+ let(:method_description) do
140
+ Apipie.get_method_description(UsersController, :update)
141
+ end
142
+
143
+ it "doesn't make the param required" do
144
+ required.should_not include :name
145
+ required.should_not include :pass
146
+ end
147
+
148
+ it "doesn't allow nil" do
149
+ allowed_nil.should_not include :name
150
+ allowed_nil.should_not include :pass
151
+ end
152
+
153
+ it "doesn't touch params with explicitly set allow_nil" do
154
+ allowed_nil.should_not include :membership
155
+ end
156
+ end
157
+
158
+ context "with explicitly setting action type in param group" do
159
+ let(:method_description) do
160
+ Apipie.get_method_description(UsersController, :admin_create)
161
+ end
162
+
163
+ it "makes the param required" do
164
+ required.should include :name
165
+ required.should include :pass
166
+ end
167
+
168
+ it "doesn't allow nil" do
169
+ allowed_nil.should_not include :name
170
+ allowed_nil.should_not include :pass
171
+ end
172
+ end
173
+ end
174
+ end