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,15 @@
1
+ module Apipie
2
+ module Routing
3
+ module MapperExtensions
4
+ def apipie
5
+ namespace "apipie", :path => Apipie.configuration.doc_base_url do
6
+ constraints(:version => /[^\/]+/) do
7
+ get("(:version)/(:resource)/(:method)" => "apipies#index", :as => :apipie)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ ActionDispatch::Routing::Mapper.send :include, Apipie::Routing::MapperExtensions
@@ -0,0 +1,39 @@
1
+ module Apipie
2
+
3
+ class SeeDescription
4
+
5
+ attr_reader :link, :description
6
+
7
+ def initialize(args)
8
+ if args.first.is_a? Hash
9
+ args = args.first
10
+ elsif args.count == 2
11
+ if args.last.is_a? Hash
12
+ args = {:link => args.first}.merge(args.last)
13
+ else
14
+ args = {:link => args.first, :description => args.second}
15
+ end
16
+ elsif args.count == 1 && args.first.is_a?(String)
17
+ args = {:link => args.first, :description => args.first}
18
+ else
19
+ raise ArgumentError "ApipieError: Bad use of see method."
20
+ end
21
+ @link = args[:link] || args['link']
22
+ @description = args[:desc] || args[:description] || args['desc'] || args['description']
23
+ end
24
+
25
+ def to_json
26
+ {:link => see_url, :description => description}
27
+ end
28
+
29
+ def see_url
30
+ method_description = Apipie[@link]
31
+ if method_description.nil?
32
+ raise ArgumentError.new("Method #{@link} referenced in 'see' does not exist.")
33
+ end
34
+ method_description.doc_url
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,59 @@
1
+ module Apipie
2
+
3
+ class FileHandler
4
+ def initialize(root)
5
+ @root = root.chomp('/')
6
+ @compiled_root = /^#{Regexp.escape(root)}/
7
+ @file_server = ::Rack::File.new(@root)
8
+ end
9
+
10
+ def match?(path)
11
+ path = path.dup
12
+
13
+ full_path = path.empty? ? @root : File.join(@root, ::Rack::Utils.unescape(path))
14
+ paths = "#{full_path}#{ext}"
15
+
16
+ matches = Dir[paths]
17
+ match = matches.detect { |m| File.file?(m) }
18
+ if match
19
+ match.sub!(@compiled_root, '')
20
+ match
21
+ end
22
+ end
23
+
24
+ def call(env)
25
+ @file_server.call(env)
26
+ end
27
+
28
+ def ext
29
+ @ext ||= begin
30
+ ext = ::ActionController::Base.page_cache_extension
31
+ "{,#{ext},/index#{ext}}"
32
+ end
33
+ end
34
+ end
35
+
36
+ class StaticDispatcher
37
+ # Dispatches the statis files. Simillar to ActionDispatch::Static, but
38
+ # it supports different baseurl configurations
39
+ def initialize(app, path, baseurl)
40
+ @app = app
41
+ @baseurl = baseurl
42
+ @file_handler = Apipie::FileHandler.new(path)
43
+ end
44
+
45
+ def call(env)
46
+ case env['REQUEST_METHOD']
47
+ when 'GET', 'HEAD'
48
+ path = env['PATH_INFO'].sub("#{@baseurl}/","/apipie/").chomp('/')
49
+
50
+ if match = @file_handler.match?(path)
51
+ env["PATH_INFO"] = match
52
+ return @file_handler.call(env)
53
+ end
54
+ end
55
+
56
+ @app.call(env)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,310 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Apipie
3
+
4
+ module Validator
5
+
6
+ # to create new validator, inherit from Apipie::Validator::Base
7
+ # and implement class method build and instance method validate
8
+ class BaseValidator
9
+
10
+ attr_accessor :param_description
11
+
12
+ def initialize(param_description)
13
+ @param_description = param_description
14
+ end
15
+
16
+ def self.inherited(subclass)
17
+ @validators ||= []
18
+ @validators.insert 0, subclass
19
+ end
20
+
21
+ # find the right validator for given options
22
+ def self.find(param_description, argument, options, block)
23
+ @validators.each do |validator_type|
24
+ validator = validator_type.build(param_description, argument, options, block)
25
+ return validator if validator
26
+ end
27
+ return nil
28
+ end
29
+
30
+ # check if value is valid
31
+ def valid?(value)
32
+ if self.validate(value)
33
+ @error_value = nil
34
+ true
35
+ else
36
+ @error_value = value
37
+ false
38
+ end
39
+ end
40
+
41
+ def param_name
42
+ @param_description.name
43
+ end
44
+
45
+ # validator description
46
+ def description
47
+ "TODO: validator description"
48
+ end
49
+
50
+ def error
51
+ ParamInvalid.new(param_name, @error_value, description)
52
+ end
53
+
54
+ def to_s
55
+ self.description
56
+ end
57
+
58
+ def to_json
59
+ self.description
60
+ end
61
+
62
+ # what type is expected, mostly string
63
+ # this information is used in cli client
64
+ # thor supported types :string, :hash, :array, :numeric, or :boolean
65
+ def expected_type
66
+ 'string'
67
+ end
68
+
69
+ def merge_with(other_validator)
70
+ raise NotImplementedError, "Dont know how to merge #{self.inspect} with #{other_validator.inspect}"
71
+ end
72
+
73
+ end
74
+
75
+ # validate arguments type
76
+ class TypeValidator < BaseValidator
77
+
78
+ def initialize(param_description, argument)
79
+ super(param_description)
80
+ @type = argument
81
+ end
82
+
83
+ def validate(value)
84
+ return false if value.nil?
85
+ value.is_a? @type
86
+ end
87
+
88
+ def self.build(param_description, argument, options, block)
89
+ if argument.is_a?(Class) && (argument != Hash || block.nil?)
90
+ self.new(param_description, argument)
91
+ end
92
+ end
93
+
94
+ def description
95
+ "Must be #{@type}"
96
+ end
97
+
98
+ def expected_type
99
+ if @type.ancestors.include? Hash
100
+ 'hash'
101
+ elsif @type.ancestors.include? Array
102
+ 'array'
103
+ elsif @type.ancestors.include? Numeric
104
+ 'numeric'
105
+ else
106
+ 'string'
107
+ end
108
+ end
109
+ end
110
+
111
+ # validate arguments value with regular expression
112
+ class RegexpValidator < BaseValidator
113
+
114
+ def initialize(param_description, argument)
115
+ super(param_description)
116
+ @regexp = argument
117
+ end
118
+
119
+ def validate(value)
120
+ value =~ @regexp
121
+ end
122
+
123
+ def self.build(param_description, argument, options, proc)
124
+ self.new(param_description, argument) if argument.is_a? Regexp
125
+ end
126
+
127
+ def description
128
+ "Must match regular expression /#{@regexp.source}/."
129
+ end
130
+ end
131
+
132
+ # arguments value must be one of given in array
133
+ class ArrayValidator < BaseValidator
134
+
135
+ def initialize(param_description, argument)
136
+ super(param_description)
137
+ @array = argument
138
+ end
139
+
140
+ def validate(value)
141
+ @array.include?(value)
142
+ end
143
+
144
+ def self.build(param_description, argument, options, proc)
145
+ self.new(param_description, argument) if argument.is_a?(Array)
146
+ end
147
+
148
+ def description
149
+ "Must be one of: #{@array.join(', ')}."
150
+ end
151
+ end
152
+
153
+ class ProcValidator < BaseValidator
154
+
155
+ def initialize(param_description, argument)
156
+ super(param_description)
157
+ @proc = argument
158
+ end
159
+
160
+ def validate(value)
161
+ (@help = @proc.call(value)) === true
162
+ end
163
+
164
+ def self.build(param_description, argument, options, proc)
165
+ self.new(param_description, argument) if argument.is_a?(Proc) && argument.arity == 1
166
+ end
167
+
168
+ def error
169
+ ParamInvalid.new(param_name, @error_value, @help)
170
+ end
171
+
172
+ def description
173
+ ""
174
+ end
175
+ end
176
+
177
+ class HashValidator < BaseValidator
178
+ include Apipie::DSL::Base
179
+ include Apipie::DSL::Param
180
+
181
+ def self.build(param_description, argument, options, block)
182
+ self.new(param_description, block, options[:param_group]) if block.is_a?(Proc) && block.arity <= 0 && argument == Hash
183
+ end
184
+
185
+ def initialize(param_description, argument, param_group)
186
+ super(param_description)
187
+ @proc = argument
188
+ @param_group = param_group
189
+ self.instance_exec(&@proc)
190
+ # specifying action_aware on Hash influences the child params,
191
+ # not the hash param itself: assuming it's required when
192
+ # updating as well
193
+ if param_description.options[:action_aware] && param_description.options[:required]
194
+ param_description.required = true
195
+ end
196
+ prepare_hash_params
197
+ end
198
+
199
+ def hash_params_ordered
200
+ @hash_params_ordered ||= _apipie_dsl_data[:params].map do |args|
201
+ options = args.find { |arg| arg.is_a? Hash }
202
+ options[:parent] = self.param_description
203
+ Apipie::ParamDescription.from_dsl_data(param_description.method_description, args)
204
+ end
205
+ end
206
+
207
+ def validate(value)
208
+ if @hash_params
209
+ @hash_params.each do |k, p|
210
+ if Apipie.configuration.validate_presence?
211
+ raise ParamMissing.new(k) if p.required && !value.has_key?(k)
212
+ end
213
+ if Apipie.configuration.validate_value?
214
+ p.validate(value[k]) if value.has_key?(k)
215
+ end
216
+ end
217
+ end
218
+ return true
219
+ end
220
+
221
+ def description
222
+ "Must be a Hash"
223
+ end
224
+
225
+ def expected_type
226
+ 'hash'
227
+ end
228
+
229
+ # where the group definition should be looked up when no scope
230
+ # given. This is expected to return a controller.
231
+ def _default_param_group_scope
232
+ @param_group && @param_group[:scope]
233
+ end
234
+
235
+ def merge_with(other_validator)
236
+ if other_validator.is_a? HashValidator
237
+ @hash_params_ordered = ParamDescription.unify(self.hash_params_ordered + other_validator.hash_params_ordered)
238
+ prepare_hash_params
239
+ else
240
+ super
241
+ end
242
+ end
243
+
244
+ def prepare_hash_params
245
+ @hash_params = hash_params_ordered.reduce({}) do |h, param|
246
+ h.update(param.name.to_sym => param)
247
+ end
248
+ end
249
+ end
250
+
251
+
252
+ # special type of validator: we say that it's not specified
253
+ class UndefValidator < Apipie::Validator::BaseValidator
254
+
255
+ def validate(value)
256
+ true
257
+ end
258
+
259
+ def self.build(param_description, argument, options, block)
260
+ if argument == :undef
261
+ self.new(param_description)
262
+ end
263
+ end
264
+
265
+ def description
266
+ nil
267
+ end
268
+ end
269
+
270
+ class NumberValidator < Apipie::Validator::BaseValidator
271
+
272
+ def validate(value)
273
+ self.class.validate(value)
274
+ end
275
+
276
+ def self.build(param_description, argument, options, block)
277
+ if argument == :number
278
+ self.new(param_description)
279
+ end
280
+ end
281
+
282
+ def description
283
+ "Must be a number."
284
+ end
285
+
286
+ def self.validate(value)
287
+ value.to_s =~ /\A(0|[1-9]\d*)\Z$/
288
+ end
289
+ end
290
+
291
+ class BooleanValidator < Apipie::Validator::BaseValidator
292
+
293
+ def validate(value)
294
+ %w[true false].include?(value.to_s)
295
+ end
296
+
297
+ def self.build(param_description, argument, options, block)
298
+ if argument == :bool
299
+ self.new(param_description)
300
+ end
301
+ end
302
+
303
+ def description
304
+ "Must be 'true' or 'false'"
305
+ end
306
+ end
307
+
308
+ end
309
+ end
310
+
@@ -0,0 +1,3 @@
1
+ module Apipie
2
+ VERSION = '0.0.23'
3
+ end
@@ -0,0 +1,6 @@
1
+ Description:
2
+ Sets up Apipie-rails in your project.
3
+ Examples:
4
+ `rails generate apipie:install`
5
+
6
+ `rails generate apipie:install --help`
@@ -0,0 +1,25 @@
1
+ module Apipie
2
+ class InstallGenerator < ::Rails::Generators::Base
3
+ source_root File.expand_path("../templates", __FILE__)
4
+
5
+ class_option(:route,
6
+ :aliases => "-r",
7
+ :type => :string,
8
+ :desc => "What path should be the doc available on",
9
+ :default => "/apipie")
10
+
11
+ class_option(:api_path,
12
+ :alias => "-a",
13
+ :type => :string,
14
+ :desc => "What path are API requests on",
15
+ :default => "/api")
16
+
17
+ def create_initializer
18
+ template 'initializer.rb.erb', 'config/initializers/apipie.rb'
19
+ end
20
+
21
+ def add_route
22
+ route("apipie")
23
+ end
24
+ end
25
+ end