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,191 @@
1
+ require 'set'
2
+ module Apipie
3
+
4
+ class MethodDescription
5
+
6
+ class Api
7
+
8
+ attr_accessor :short_description, :path, :http_method
9
+
10
+ def initialize(method, path, desc)
11
+ @http_method = method.to_s
12
+ @path = path
13
+ @short_description = desc
14
+ end
15
+
16
+ end
17
+
18
+ attr_reader :full_description, :method, :resource, :apis, :examples, :see, :formats
19
+
20
+ def initialize(method, resource, dsl_data)
21
+ @method = method.to_s
22
+ @resource = resource
23
+ @from_concern = dsl_data[:from_concern]
24
+
25
+ @apis = dsl_data[:api_args].map do |method, path, desc|
26
+ MethodDescription::Api.new(method, concern_subst(path), concern_subst(desc))
27
+ end
28
+
29
+ desc = dsl_data[:description] || ''
30
+ @full_description = Apipie.markup_to_html(desc)
31
+
32
+ @errors = dsl_data[:errors].map do |args|
33
+ Apipie::ErrorDescription.new(args)
34
+ end
35
+
36
+ @see = dsl_data[:see].map do |args|
37
+ Apipie::SeeDescription.new(args)
38
+ end
39
+
40
+ @formats = dsl_data[:formats]
41
+ @examples = dsl_data[:examples]
42
+ @examples += load_recorded_examples
43
+
44
+ @params_ordered = dsl_data[:params].map do |args|
45
+ Apipie::ParamDescription.from_dsl_data(self, args)
46
+ end
47
+ @params_ordered = ParamDescription.unify(@params_ordered)
48
+ end
49
+
50
+ def id
51
+ "#{resource._id}##{method}"
52
+ end
53
+
54
+ def params
55
+ params_ordered.reduce(ActiveSupport::OrderedHash.new) { |h,p| h[p.name] = p; h }
56
+ end
57
+
58
+ def params_ordered
59
+ all_params = []
60
+ parent = Apipie.get_resource_description(@resource.controller.superclass)
61
+
62
+ # get params from parent resource description
63
+ [parent, @resource].compact.each do |resource|
64
+ resource_params = resource._params_args.map do |args|
65
+ Apipie::ParamDescription.from_dsl_data(self, args)
66
+ end
67
+ merge_params(all_params, resource_params)
68
+ end
69
+
70
+ merge_params(all_params, @params_ordered)
71
+ all_params.find_all(&:validator)
72
+ end
73
+
74
+ def errors
75
+ return @merged_errors if @merged_errors
76
+ @merged_errors = []
77
+ if @resource
78
+ resource_errors = @resource._errors_args.map do |args|
79
+ Apipie::ErrorDescription.new(args)
80
+ end
81
+
82
+ # exclude overwritten parent errors
83
+ @merged_errors = resource_errors.find_all do |err|
84
+ !@errors.any? { |e| e.code == err.code }
85
+ end
86
+ end
87
+ @merged_errors.concat(@errors)
88
+ return @merged_errors
89
+ end
90
+
91
+ def version
92
+ resource._version
93
+ end
94
+
95
+ def doc_url
96
+ crumbs = []
97
+ crumbs << @resource._version if Apipie.configuration.version_in_url
98
+ crumbs << @resource._id
99
+ crumbs << @method
100
+ Apipie.full_url crumbs.join('/')
101
+ end
102
+
103
+ def create_api_url(api)
104
+ path = "#{Apipie.api_base_url(@resource._version)}#{api.path}"
105
+ path = path[0..-2] if path[-1..-1] == '/'
106
+ return path
107
+ end
108
+
109
+ def method_apis_to_json
110
+ @apis.each.collect do |api|
111
+ {
112
+ :api_url => create_api_url(api),
113
+ :http_method => api.http_method.to_s,
114
+ :short_description => api.short_description
115
+ }
116
+ end
117
+ end
118
+
119
+ def see
120
+ @see
121
+ end
122
+
123
+ def formats
124
+ @formats || @resource._formats
125
+ end
126
+
127
+ def to_json
128
+ {
129
+ :doc_url => doc_url,
130
+ :name => @method,
131
+ :apis => method_apis_to_json,
132
+ :formats => formats,
133
+ :full_description => @full_description,
134
+ :errors => errors.map(&:to_json),
135
+ :params => params_ordered.map(&:to_json).flatten,
136
+ :examples => @examples,
137
+ :see => see.map(&:to_json)
138
+ }
139
+ end
140
+
141
+ # was the description defines in a module instead of directly in controller?
142
+ def from_concern?
143
+ @from_concern
144
+ end
145
+
146
+ private
147
+
148
+ def merge_params(params, new_params)
149
+ new_param_names = Set.new(new_params.map(&:name))
150
+ params.delete_if { |p| new_param_names.include?(p.name) }
151
+ params.concat(new_params)
152
+ end
153
+
154
+ def load_recorded_examples
155
+ (Apipie.recorded_examples[id] || []).
156
+ find_all { |ex| ex["show_in_doc"].to_i > 0 }.
157
+ find_all { |ex| ex["versions"].nil? || ex["versions"].include?(self.version) }.
158
+ sort_by { |ex| ex["show_in_doc"] }.
159
+ map { |ex| format_example(ex.symbolize_keys) }
160
+ end
161
+
162
+ def format_example_data(data)
163
+ case data
164
+ when Array, Hash
165
+ JSON.pretty_generate(data).gsub(/: \[\s*\]/,": []").gsub(/\{\s*\}/,"{}")
166
+ else
167
+ data
168
+ end
169
+ end
170
+
171
+ def format_example(ex)
172
+ example = "#{ex[:verb]} #{ex[:path]}"
173
+ example << "?#{ex[:query]}" unless ex[:query].blank?
174
+ example << "\n" << format_example_data(ex[:request_data]).to_s if ex[:request_data]
175
+ example << "\n" << ex[:code].to_s
176
+ example << "\n" << format_example_data(ex[:response_data]).to_s if ex[:response_data]
177
+ example
178
+ end
179
+
180
+ def concern_subst(string)
181
+ return if string.nil?
182
+ if from_concern?
183
+ resource.controller._apipie_perform_concern_subst(string)
184
+ else
185
+ string
186
+ end
187
+ end
188
+
189
+ end
190
+
191
+ end
@@ -0,0 +1,204 @@
1
+ module Apipie
2
+
3
+ # method parameter description
4
+ #
5
+ # name - method name (show)
6
+ # desc - description
7
+ # required - boolean if required
8
+ # validator - Validator::BaseValidator subclass
9
+ class ParamDescription
10
+
11
+ attr_reader :method_description, :name, :desc, :allow_nil, :validator, :options, :validations
12
+
13
+ attr_accessor :parent, :required
14
+
15
+ def self.from_dsl_data(method_description, args)
16
+ param_name, validator, desc_or_options, options, block = args
17
+ Apipie::ParamDescription.new(method_description,
18
+ param_name,
19
+ validator,
20
+ desc_or_options,
21
+ options,
22
+ &block)
23
+ end
24
+
25
+ def initialize(method_description, name, validator, desc_or_options = nil, options = {}, &block)
26
+
27
+ if desc_or_options.is_a?(Hash)
28
+ options = options.merge(desc_or_options)
29
+ elsif desc_or_options.is_a?(String)
30
+ options[:desc] = desc_or_options
31
+ elsif !desc_or_options.nil?
32
+ raise ArgumentError.new("param description: expected description or options as 3rd parameter")
33
+ end
34
+
35
+ options.symbolize_keys!
36
+
37
+ # we save options to know what was passed in DSL
38
+ @options = options
39
+
40
+ @method_description = method_description
41
+ @name = concern_subst(name)
42
+ @desc = concern_subst(Apipie.markup_to_html(@options[:desc] || ''))
43
+ @parent = @options[:parent]
44
+ @required = if @options.has_key? :required
45
+ @options[:required]
46
+ else
47
+ Apipie.configuration.required_by_default?
48
+ end
49
+
50
+ @allow_nil = @options[:allow_nil] || false
51
+
52
+ action_awareness
53
+
54
+ if validator
55
+ @validator = Validator::BaseValidator.find(self, validator, @options, block)
56
+ raise "Validator for #{validator} not found." unless @validator
57
+ end
58
+
59
+ @validations = Array(options[:validations]).map {|v| concern_subst(Apipie.markup_to_html(v)) }
60
+ end
61
+
62
+ def validate(value)
63
+ return true if @allow_nil && value.nil?
64
+ unless @validator.valid?(value)
65
+ error = @validator.error
66
+ error = ParamError.new(error) unless error.is_a? Exception
67
+ raise error
68
+ end
69
+ end
70
+
71
+ def full_name
72
+ name_parts = parents_and_self.map(&:name)
73
+ return ([name_parts.first] + name_parts[1..-1].map { |n| "[#{n}]" }).join("")
74
+ end
75
+
76
+ # returns an array of all the parents: starting with the root parent
77
+ # ending with itself
78
+ def parents_and_self
79
+ ret = []
80
+ if self.parent
81
+ ret.concat(self.parent.parents_and_self)
82
+ end
83
+ ret << self
84
+ ret
85
+ end
86
+
87
+ def to_json
88
+ if validator.is_a? Apipie::Validator::HashValidator
89
+ {
90
+ :name => name.to_s,
91
+ :anchor => anchor,
92
+ :full_name => full_name,
93
+ :description => desc,
94
+ :required => required,
95
+ :allow_nil => allow_nil,
96
+ :validator => validator.to_s,
97
+ :expected_type => validator.expected_type,
98
+ :params => validator.hash_params_ordered.map(&:to_json),
99
+ :validations => validations
100
+ }
101
+ else
102
+ {
103
+ :name => name.to_s,
104
+ :anchor => anchor,
105
+ :full_name => full_name,
106
+ :description => desc,
107
+ :required => required,
108
+ :allow_nil => allow_nil,
109
+ :validator => validator.to_s,
110
+ :expected_type => validator.expected_type,
111
+ :validations => validations
112
+ }
113
+ end
114
+ end
115
+
116
+ def merge_with(other_param_desc)
117
+ if self.validator && other_param_desc.validator
118
+ self.validator.merge_with(other_param_desc.validator)
119
+ else
120
+ self.validator ||= other_param_desc.validator
121
+ end
122
+ self
123
+ end
124
+
125
+ # merge param descripsiont. Allows defining hash params on more places
126
+ # (e.g. in param_groups). For example:
127
+ #
128
+ # def_param_group :user do
129
+ # param :user, Hash do
130
+ # param :name, String
131
+ # end
132
+ # end
133
+ #
134
+ # param_group :user
135
+ # param :user, Hash do
136
+ # param :password, String
137
+ # end
138
+ def self.unify(params)
139
+ ordering = params.map(&:name)
140
+ params.group_by(&:name).map do |name, param_descs|
141
+ param_descs.reduce(&:merge_with)
142
+ end.sort_by { |param| ordering.index(param.name) }
143
+ end
144
+
145
+ # action awareness is being inherited from ancestors (in terms of
146
+ # nested params)
147
+ def action_aware?
148
+ if @options.has_key?(:action_aware)
149
+ return @options[:action_aware]
150
+ elsif @parent
151
+ @parent.action_aware?
152
+ else
153
+ false
154
+ end
155
+ end
156
+
157
+ def as_action
158
+ if @options[:param_group] && @options[:param_group][:options] &&
159
+ @options[:param_group][:options][:as]
160
+ @options[:param_group][:options][:as].to_s
161
+ elsif @parent
162
+ @parent.as_action
163
+ else
164
+ @method_description.method
165
+ end
166
+ end
167
+
168
+ # makes modification that are based on the action that the param
169
+ # is defined for. Typical for required and allow_nil variations in
170
+ # crate/update actions.
171
+ def action_awareness
172
+ if action_aware?
173
+ if !@options.has_key?(:allow_nil)
174
+ if @required
175
+ @allow_nil = false
176
+ else
177
+ @allow_nil = true
178
+ end
179
+ end
180
+ if as_action != "create"
181
+ @required = false
182
+ end
183
+ end
184
+ end
185
+
186
+ def concern_subst(string)
187
+ return string if string.nil? or !method_description.from_concern?
188
+
189
+ original = string
190
+ string = ":#{original}" if original.is_a? Symbol
191
+
192
+ replaced = method_description.resource.controller._apipie_perform_concern_subst(string)
193
+
194
+ return original if replaced == string
195
+ return replaced.to_sym if original.is_a? Symbol
196
+ return replaced
197
+ end
198
+
199
+ def anchor
200
+ (full_name.to_s || "").gsub(/\W/, '_').gsub(/_$/, '')
201
+ end
202
+ end
203
+
204
+ end
@@ -0,0 +1,9 @@
1
+ module Apipie
2
+ class Railtie < Rails::Railtie
3
+ initializer 'apipie.controller_additions' do
4
+ ActiveSupport.on_load :action_controller do
5
+ extend Apipie::DSL::Controller
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,102 @@
1
+ module Apipie
2
+
3
+ # Resource description
4
+ #
5
+ # version - api version (1)
6
+ # description
7
+ # path - relative path (/api/articles)
8
+ # methods - array of keys to Apipie.method_descriptions (array of Apipie::MethodDescription)
9
+ # name - human readable alias of resource (Articles)
10
+ # id - resouce name
11
+ # formats - acceptable request/response format types
12
+ class ResourceDescription
13
+
14
+ attr_reader :controller, :_short_description, :_full_description, :_methods, :_id,
15
+ :_path, :_name, :_params_args, :_errors_args, :_formats, :_parent
16
+
17
+ def initialize(controller, resource_name, dsl_data = nil, version = nil, &block)
18
+
19
+ @_methods = ActiveSupport::OrderedHash.new
20
+ @_params_args = []
21
+ @_errors_args = []
22
+
23
+ @controller = controller
24
+ @_id = resource_name
25
+ @_version = version || Apipie.configuration.default_version
26
+ @_name = @_id.humanize
27
+ @_parent = Apipie.get_resource_description(controller.superclass, version)
28
+
29
+ update_from_dsl_data(dsl_data) if dsl_data
30
+ end
31
+
32
+ def update_from_dsl_data(dsl_data)
33
+ @_name = dsl_data[:resource_name] if dsl_data[:resource_name]
34
+ @_full_description = Apipie.markup_to_html(dsl_data[:description])
35
+ @_short_description = dsl_data[:short_description]
36
+ @_path = dsl_data[:path] || ""
37
+ @_formats = dsl_data[:formats]
38
+ @_errors_args = dsl_data[:errors]
39
+ @_params_args = dsl_data[:params]
40
+
41
+ if dsl_data[:app_info]
42
+ Apipie.configuration.app_info[_version] = dsl_data[:app_info]
43
+ end
44
+ if dsl_data[:api_base_url]
45
+ Apipie.configuration.api_base_url[_version] = dsl_data[:api_base_url]
46
+ end
47
+ end
48
+
49
+ def _version
50
+ @_version || @_parent.try(:_version) || Apipie.configuration.default_version
51
+ end
52
+
53
+ def add_method_description(method_description)
54
+ Apipie.debug "@resource_descriptions[#{self._version}][#{self._name}]._methods[#{method_description.method}] = #{method_description}"
55
+ @_methods[method_description.method.to_sym] = method_description
56
+ end
57
+
58
+ def method_description(method_name)
59
+ @_methods[method_name.to_sym]
60
+ end
61
+
62
+ def remove_method_description(method_name)
63
+ if @_methods.has_key?(method_name)
64
+ @_methods.delete(method_name)
65
+ end
66
+ end
67
+
68
+ def method_descriptions
69
+ @_methods.values
70
+ end
71
+
72
+ def doc_url
73
+ crumbs = []
74
+ crumbs << _version if Apipie.configuration.version_in_url
75
+ crumbs << @_id
76
+ Apipie.full_url crumbs.join('/')
77
+ end
78
+
79
+ def api_url; "#{Apipie.api_base_url(_version)}#{@_path}"; end
80
+
81
+ def to_json(method_name = nil)
82
+
83
+ methods = if method_name.blank?
84
+ @_methods.collect { |key, method_description| method_description.to_json}
85
+ else
86
+ [@_methods[method_name.to_sym].to_json]
87
+ end
88
+
89
+ {
90
+ :doc_url => doc_url,
91
+ :api_url => api_url,
92
+ :name => @_name,
93
+ :short_description => @_short_description,
94
+ :full_description => @_full_description,
95
+ :version => _version,
96
+ :formats => @_formats,
97
+ :methods => methods
98
+ }
99
+ end
100
+
101
+ end
102
+ end