apipie-rails 0.5.1 → 0.5.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21e987c452e1e522365932db32bb06b9d4dabf51
4
- data.tar.gz: 92d447975ab9539f24b52c19e0fa22875b0fe039
3
+ metadata.gz: ff402148f82601329555c80c61abcf03b5aad5f8
4
+ data.tar.gz: 3e813a085a3f97ac4a22c0bb4868d2794aa92d66
5
5
  SHA512:
6
- metadata.gz: 097757c953763f1a70eb1005dda15fb0734e6e9670e235a6b534a0ef3a109ab087e7e7e01459edb822cc4f887009d57ad17a733c081c14eac72deae2d384bb6f
7
- data.tar.gz: 2ef85b2507dc949756c8816c31c340efdc4f59f259c92fe0342972f4f799240c1443f637ca2f4e91114fbfbd283ed3a30a3afdbd553a95307444e32531cd5fed
6
+ metadata.gz: 8ec2b8a330539a058e6c9a1b454635016ffcdc53374a661fb58eed1de6adb404ab0085f18d2e99f4ed7c99a091cb7ddf649bcc74fa5676dad4ed24f2a02e05ac
7
+ data.tar.gz: cd298cfd7e25d2ad6abcc1cc78c1d63d406f2a41a7f12505b0f2261302a319df3229878c1db0af2cd389b0e1c72b57b02cb2a1c980defff283ab8d324e992277
data/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  Changelog
2
2
  ===========
3
3
 
4
+ v0.5.2
5
+ ------
6
+
7
+ - A way to extend an exiting API via concern [\#554](https://github.com/Apipie/apipie-rails/pull/554) ([iNecas](https://github.com/iNecas))
8
+ - Fallback to apipie views when application override isn't present [\#552](https://github.com/Apipie/apipie-rails/pull/552) ([tstrachota](https://github.com/tstrachota))
9
+ - Updated setting default locale for api documentation [\#543](https://github.com/Apipie/apipie-rails/pull/543) ([DmitryKK](https://github.com/DmitryKK))
10
+
4
11
  v0.5.1
5
12
  ------
6
13
 
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ Gemfile.rails50
data/Gemfile.rails50 CHANGED
@@ -2,6 +2,7 @@ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
+ gem 'pry'
5
6
  gem 'rails', '~> 5.0'
6
7
  gem 'mime-types', '~> 2.99.3'
7
8
  gem 'rails-controller-testing'
data/README.rst CHANGED
@@ -504,6 +504,32 @@ Example
504
504
  end
505
505
 
506
506
 
507
+ Sometimes, it's needed to extend an existing controller method with additional
508
+ parameters (usually when extending exiting API from plugins/rails engines).
509
+ The concern can be also used for this purposed, using `update_api` method.
510
+ The params defined in this block are merged with the params of the original method
511
+ in the controller this concern is included to.
512
+
513
+ Example
514
+ ~~~~~~~
515
+
516
+ .. code:: ruby
517
+
518
+ module Concerns
519
+ module OauthConcern
520
+ extend Apipie::DSL::Concern
521
+
522
+ update_api(:create, :update) do
523
+ param :user, Hash do
524
+ param :oauth, String, :desc => 'oauth param'
525
+ end
526
+ end
527
+ end
528
+ end
529
+
530
+ The concern needs to be included to the controller after the methods are defined
531
+ (either at the end of the class, or by using
532
+ ``Controller.send(:include, Concerns::OauthConcern)``.
507
533
 
508
534
  =========================
509
535
  Configuration Reference
@@ -999,7 +1025,7 @@ the default version is used instead.
999
1025
  ========
1000
1026
 
1001
1027
  The default markup language is `RDoc
1002
- <http://rdoc.rubyforge.org/RDoc/Markup.html>`_. It can be changed in
1028
+ <https://rdoc.github.io/rdoc/RDoc/Markup.html>`_. It can be changed in
1003
1029
  the config file (``config.markup=``) to one of these:
1004
1030
 
1005
1031
  Markdown
@@ -1231,7 +1257,7 @@ one example per method) by adding a 'title' attribute.
1231
1257
  - recorded: true
1232
1258
 
1233
1259
  In RSpec you can add metadata to examples. We can use that feature
1234
- to mark selected examples the ones that perform the requests that we want to
1260
+ to mark selected examples - the ones that perform the requests that we want to
1235
1261
  show as examples in the documentation.
1236
1262
 
1237
1263
  For example, we can add ``show_in_doc`` to examples, like this:
@@ -79,7 +79,7 @@ module Apipie
79
79
  helper_method :heading
80
80
 
81
81
  def get_language
82
- lang = nil
82
+ lang = Apipie.configuration.default_locale
83
83
  [:resource, :method, :version].each do |par|
84
84
  if params[par]
85
85
  splitted = params[par].split('.')
@@ -8,6 +8,14 @@ module Apipie
8
8
  module Base
9
9
  attr_reader :apipie_resource_descriptions, :api_params
10
10
 
11
+ def _apipie_eval_dsl(*args, &block)
12
+ raise 'The Apipie DLS data need to be cleared before evaluating new block' if @_apipie_dsl_data
13
+ instance_exec(*args, &block)
14
+ return _apipie_dsl_data
15
+ ensure
16
+ _apipie_dsl_data_clear
17
+ end
18
+
11
19
  private
12
20
 
13
21
  def _apipie_dsl_data
@@ -381,6 +389,37 @@ module Apipie
381
389
  _apipie_concern_subst.merge!(subst_hash)
382
390
  end
383
391
 
392
+ # Allows to update existing params after definition was made (usually needed
393
+ # when extending the API form plugins).
394
+ #
395
+ # UsersController.apipie_update_params([:create, :update]) do
396
+ # param :user, Hash do
397
+ # param :oauth, String
398
+ # end
399
+ # end
400
+ #
401
+ # The block is evaluated in scope of the controller. Ohe can pass some additional
402
+ # objects via additional arguments like this:
403
+ #
404
+ # UsersController.apipie_update_params([:create, :update], [:name, :secret]) do |param_names|
405
+ # param :user, Hash do
406
+ # param_names.each { |p| param p, String }
407
+ # end
408
+ # end
409
+ def apipie_update_params(methods, *args, &block)
410
+ methods.each do |method|
411
+ method_description = Apipie.get_method_description(self, method)
412
+ unless method_description
413
+ raise "Could not find method description for #{self}##{method}. Was the method defined?"
414
+ end
415
+ dsl_data = _apipie_eval_dsl(*args, &block)
416
+ params_ordered = dsl_data[:params].map do |args|
417
+ Apipie::ParamDescription.from_dsl_data(method_description, args)
418
+ end
419
+ ParamDescription.unify(method_description.params_ordered_self + params_ordered)
420
+ end
421
+ end
422
+
384
423
  def _apipie_concern_subst
385
424
  @_apipie_concern_subst ||= {:controller_path => self.controller_path,
386
425
  :resource_id => Apipie.get_resource_name(self)}
@@ -428,12 +467,19 @@ module Apipie
428
467
  description = Apipie.define_method_description(controller, method_name, _apipie_dsl_data)
429
468
  controller._apipie_define_validators(description)
430
469
  end
470
+ _apipie_concern_update_api_blocks.each do |(methods, block)|
471
+ controller.apipie_update_params(methods, &block)
472
+ end
431
473
  end
432
474
 
433
475
  def _apipie_concern_data
434
476
  @_apipie_concern_data ||= []
435
477
  end
436
478
 
479
+ def _apipie_concern_update_api_blocks
480
+ @_apipie_concern_update_api_blocks ||= []
481
+ end
482
+
437
483
  def apipie_concern?
438
484
  true
439
485
  end
@@ -449,6 +495,10 @@ module Apipie
449
495
  _apipie_dsl_data_clear
450
496
  end
451
497
 
498
+ def update_api(*methods, &block)
499
+ _apipie_concern_update_api_blocks << [methods, block]
500
+ end
501
+
452
502
  end
453
503
 
454
504
  class ResourceDescriptionDsl
@@ -461,14 +511,9 @@ module Apipie
461
511
  @controller = controller
462
512
  end
463
513
 
464
- def _eval_dsl(&block)
465
- instance_eval(&block)
466
- return _apipie_dsl_data
467
- end
468
-
469
514
  # evaluates resource description DSL and returns results
470
515
  def self.eval_dsl(controller, &block)
471
- dsl_data = self.new(controller)._eval_dsl(&block)
516
+ dsl_data = self.new(controller)._apipie_eval_dsl(&block)
472
517
  if dsl_data[:api_versions].empty?
473
518
  dsl_data[:api_versions] = Apipie.controller_versions(controller)
474
519
  end
@@ -65,6 +65,10 @@ module Apipie
65
65
  params_ordered.reduce(ActiveSupport::OrderedHash.new) { |h,p| h[p.name] = p; h }
66
66
  end
67
67
 
68
+ def params_ordered_self
69
+ @params_ordered
70
+ end
71
+
68
72
  def params_ordered
69
73
  all_params = []
70
74
  parent = Apipie.get_resource_description(@resource.controller.superclass)
@@ -21,6 +21,19 @@ module Apipie
21
21
  &block)
22
22
  end
23
23
 
24
+ def to_s
25
+ "ParamDescription: #{method_description.id}##{name}"
26
+ end
27
+
28
+ def ==(other)
29
+ return false unless self.class == other.class
30
+ if method_description == other.method_description && @options == other.options
31
+ true
32
+ else
33
+ false
34
+ end
35
+ end
36
+
24
37
  def initialize(method_description, name, validator, desc_or_options = nil, options = {}, &block)
25
38
 
26
39
  if desc_or_options.is_a?(Hash)
@@ -146,7 +159,7 @@ module Apipie
146
159
  self
147
160
  end
148
161
 
149
- # merge param descripsiont. Allows defining hash params on more places
162
+ # merge param descriptions. Allows defining hash params on more places
150
163
  # (e.g. in param_groups). For example:
151
164
  #
152
165
  # def_param_group :user do
@@ -13,6 +13,16 @@ module Apipie
13
13
  @param_description = param_description
14
14
  end
15
15
 
16
+ def inspected_fields
17
+ [:param_description]
18
+ end
19
+
20
+ def inspect
21
+ string = "#<#{self.class.name}:#{self.object_id} "
22
+ fields = inspected_fields.map {|field| "#{field}: #{self.send(field)}"}
23
+ string << fields.join(", ") << ">"
24
+ end
25
+
16
26
  def self.inherited(subclass)
17
27
  @validators ||= []
18
28
  @validators.insert 0, subclass
@@ -67,13 +77,22 @@ module Apipie
67
77
  end
68
78
 
69
79
  def merge_with(other_validator)
70
- raise NotImplementedError, "Dont know how to merge #{self.inspect} with #{other_validator.inspect}"
80
+ return self if self == other_validator
81
+ raise NotImplementedError, "Don't know how to merge #{self.inspect} with #{other_validator.inspect}"
71
82
  end
72
83
 
73
84
  def params_ordered
74
85
  nil
75
86
  end
76
87
 
88
+ def ==(other)
89
+ return false unless self.class == other.class
90
+ if param_description == other.param_description
91
+ true
92
+ else
93
+ false
94
+ end
95
+ end
77
96
  end
78
97
 
79
98
  # validate arguments type
@@ -1,3 +1,3 @@
1
1
  module Apipie
2
- VERSION = '0.5.1'
2
+ VERSION = '0.5.2'
3
3
  end
@@ -100,17 +100,14 @@ namespace :apipie do
100
100
  # Attempt to use the Rails application views, otherwise default to built in views
101
101
  def renderer
102
102
  return @apipie_renderer if @apipie_renderer
103
- base_path = if File.directory?("#{Rails.root}/app/views/apipie/apipies")
104
- "#{Rails.root}/app/views/apipie/apipies"
105
- else
106
- File.expand_path("../../../app/views/apipie/apipies", __FILE__)
107
- end
108
- layouts_path = if File.directory?("#{Rails.root}/app/views/layouts/apipie")
109
- "#{Rails.root}/app/views/layouts"
110
- else
111
- File.expand_path("../../../app/views/layouts", __FILE__)
112
- end
113
- @apipie_renderer = ActionView::Base.new([base_path, layouts_path])
103
+
104
+ base_paths = [File.expand_path("../../../app/views/apipie/apipies", __FILE__)]
105
+ base_paths.unshift("#{Rails.root}/app/views/apipie/apipies") if File.directory?("#{Rails.root}/app/views/apipie/apipies")
106
+
107
+ layouts_paths = [File.expand_path("../../../app/views/layouts", __FILE__)]
108
+ layouts_paths.unshift("#{Rails.root}/app/views/layouts") if File.directory?("#{Rails.root}/app/views/layouts/apipie")
109
+
110
+ @apipie_renderer = ActionView::Base.new(base_paths + layouts_paths)
114
111
  @apipie_renderer.singleton_class.send(:include, ApipieHelper)
115
112
  return @apipie_renderer
116
113
  end
@@ -0,0 +1,10 @@
1
+ require "spec_helper"
2
+
3
+ describe ExtendedController do
4
+
5
+ it 'should include params from both original controller and extending concern' do
6
+ user_param = Apipie["extended#create"].params[:user]
7
+ expect(user_param.validator.params_ordered.map(&:name)).to eq [:name, :password, :from_concern]
8
+ end
9
+ end
10
+
@@ -0,0 +1,11 @@
1
+ module Concerns
2
+ module ExtendingConcern
3
+ extend Apipie::DSL::Concern
4
+
5
+ update_api(:create) do
6
+ param :user, Hash do
7
+ param :from_concern, String, :desc => 'param from concern', :allow_nil => false
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ class ExtendedController < ApplicationController
2
+
3
+ api :POST, '/extended'
4
+ param :user, Hash do
5
+ param :name, String
6
+ param :password, String
7
+ end
8
+ def create
9
+ end
10
+
11
+ include Concerns::ExtendingConcern
12
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apipie-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Pokorny
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-04-26 00:00:00.000000000 Z
12
+ date: 2017-08-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -225,6 +225,7 @@ files:
225
225
  - spec/controllers/api/v2/nested/resources_controller_spec.rb
226
226
  - spec/controllers/apipies_controller_spec.rb
227
227
  - spec/controllers/concerns_controller_spec.rb
228
+ - spec/controllers/extended_controller_spec.rb
228
229
  - spec/controllers/users_controller_spec.rb
229
230
  - spec/dummy/Rakefile
230
231
  - spec/dummy/app/controllers/api/base_controller.rb
@@ -235,8 +236,10 @@ files:
235
236
  - spec/dummy/app/controllers/api/v2/nested/architectures_controller.rb
236
237
  - spec/dummy/app/controllers/api/v2/nested/resources_controller.rb
237
238
  - spec/dummy/app/controllers/application_controller.rb
239
+ - spec/dummy/app/controllers/concerns/extending_concern.rb
238
240
  - spec/dummy/app/controllers/concerns/sample_controller.rb
239
241
  - spec/dummy/app/controllers/concerns_controller.rb
242
+ - spec/dummy/app/controllers/extended_controller.rb
240
243
  - spec/dummy/app/controllers/files_controller.rb
241
244
  - spec/dummy/app/controllers/overridden_concerns_controller.rb
242
245
  - spec/dummy/app/controllers/twitter_example_controller.rb
@@ -299,7 +302,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
299
302
  version: '0'
300
303
  requirements: []
301
304
  rubyforge_project:
302
- rubygems_version: 2.4.5
305
+ rubygems_version: 2.5.1
303
306
  signing_key:
304
307
  specification_version: 4
305
308
  summary: Rails REST API documentation tool
@@ -309,6 +312,7 @@ test_files:
309
312
  - spec/controllers/api/v2/nested/resources_controller_spec.rb
310
313
  - spec/controllers/apipies_controller_spec.rb
311
314
  - spec/controllers/concerns_controller_spec.rb
315
+ - spec/controllers/extended_controller_spec.rb
312
316
  - spec/controllers/users_controller_spec.rb
313
317
  - spec/dummy/Rakefile
314
318
  - spec/dummy/app/controllers/api/base_controller.rb
@@ -319,8 +323,10 @@ test_files:
319
323
  - spec/dummy/app/controllers/api/v2/nested/architectures_controller.rb
320
324
  - spec/dummy/app/controllers/api/v2/nested/resources_controller.rb
321
325
  - spec/dummy/app/controllers/application_controller.rb
326
+ - spec/dummy/app/controllers/concerns/extending_concern.rb
322
327
  - spec/dummy/app/controllers/concerns/sample_controller.rb
323
328
  - spec/dummy/app/controllers/concerns_controller.rb
329
+ - spec/dummy/app/controllers/extended_controller.rb
324
330
  - spec/dummy/app/controllers/files_controller.rb
325
331
  - spec/dummy/app/controllers/overridden_concerns_controller.rb
326
332
  - spec/dummy/app/controllers/twitter_example_controller.rb
data/Gemfile DELETED
@@ -1,7 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gemspec
4
-
5
- gem 'rails', '~> 5.0'
6
- gem 'mime-types', '~> 2.99.3'
7
- gem 'rails-controller-testing'