apipie-rails 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,14 @@
2
2
  Changelog
3
3
  ===========
4
4
 
5
+ v0.1.2
6
+ ------
7
+
8
+ * reloading works correctly with to_prepare blocks in the app
9
+ [#228](https://github.com/Apipie/apipie-rails/pull/228) [@iNecas][]
10
+ * documentation bootstrapping now respects api_controllers_matcher
11
+ [#227](https://github.com/Apipie/apipie-rails/pull/227) [@clamoris][]
12
+
5
13
  v0.1.1
6
14
  ------
7
15
 
@@ -104,3 +112,4 @@ v0.0.15
104
112
  [@yonpols]: https://github.com/yonpols
105
113
  [@mkrajewski]: https://github.com/mkrajewski
106
114
  [@iNecas]: https://github.com/iNecas
115
+ [@clamoris]: https://github.com/clamoris
data/README.rst CHANGED
@@ -519,6 +519,9 @@ reload_controllers
519
519
  api_controllers_matcher
520
520
  For reloading to work properly you need to specify where your API controllers are. Can be an array if multiple paths are needed
521
521
 
522
+ api_routes
523
+ Set if your application uses custom API router, different from Rails default
524
+
522
525
  markup
523
526
  You can choose markup language for descriptions of your application,
524
527
  resources and methods. RDoc is the default but you can choose from
@@ -566,6 +569,7 @@ Example:
566
569
  config.markup = Apipie::Markup::Markdown.new
567
570
  config.reload_controllers = Rails.env.development?
568
571
  config.api_controllers_matcher = File.join(Rails.root, "app", "controllers", "**","*.rb")
572
+ config.api_routes = Rails.application.routes
569
573
  config.app_info = "
570
574
  This is where you can inform user about your application and API
571
575
  in general.
@@ -259,13 +259,7 @@ module Apipie
259
259
  end
260
260
 
261
261
  def reload_documentation
262
- if rails_mark_classes_for_reload
263
- # if we are not able to mark classes for reload, we
264
- # can't reinit the env, as we would loose the data that were
265
- # already loaded
266
- init_env
267
- reload_examples
268
- end
262
+ rails_mark_classes_for_reload
269
263
 
270
264
  api_controllers_paths.each do |f|
271
265
  load_controller_from_file f
@@ -359,9 +353,10 @@ module Apipie
359
353
  # as this would break loading of the controllers.
360
354
  def rails_mark_classes_for_reload
361
355
  unless Rails.application.config.cache_classes
362
- ActiveSupport::DescendantsTracker.clear
363
- ActiveSupport::Dependencies.clear
364
- return true
356
+ ActionDispatch::Reloader.cleanup!
357
+ init_env
358
+ reload_examples
359
+ ActionDispatch::Reloader.prepare!
365
360
  end
366
361
  end
367
362
 
@@ -22,6 +22,11 @@ module Apipie
22
22
  # properly.
23
23
  attr_writer :reload_controllers
24
24
 
25
+ # specify routes if used router differ from default e.g.
26
+ #
27
+ # Api::Engine.routes
28
+ attr_accessor :api_routes
29
+
25
30
  def reload_controllers?
26
31
  @reload_controllers = Rails.env.development? unless defined? @reload_controllers
27
32
  return @reload_controllers && @api_controllers_matcher
@@ -110,6 +115,10 @@ module Apipie
110
115
  @api_base_url[version] = url
111
116
  end
112
117
 
118
+ def api_routes
119
+ @api_routes || Rails.application.routes
120
+ end
121
+
113
122
  def initialize
114
123
  @markup = Apipie::Markup::RDoc.new
115
124
  @app_name = "Another API"
@@ -55,41 +55,54 @@ module Apipie
55
55
  Writer.new(@collector).write_examples if @collector
56
56
  end
57
57
 
58
- # TODO: this is a loooooooooong method :)
59
58
  def apis_from_routes
60
59
  return @apis_from_routes if @apis_from_routes
61
60
 
62
- api_prefix = Apipie.api_base_url.sub(/\/$/,"")
63
- all_routes = Rails.application.routes.routes.map do |r|
64
- {
65
- :verb => case r.verb
66
- when Regexp then r.verb.source[/\w+/]
67
- else r.verb.to_s
68
- end,
69
- :path => case
70
- when r.path.respond_to?(:spec) then r.path.spec.to_s
71
- else r.path.to_s
72
- end,
73
- :controller => r.requirements[:controller],
74
- :action => r.requirements[:action]
75
- }
61
+ @api_prefix = Apipie.api_base_url.sub(/\/$/,"")
62
+ populate_api_routes
63
+ update_api_descriptions
64
+
65
+ @apis_from_routes
66
+ end
67
+
68
+ def controller_path(name)
69
+ Apipie.api_controllers_paths.detect { |p| p.include?("#{name}_controller.rb") }
70
+ end
76
71
 
72
+ private
77
73
 
74
+ def all_api_routes
75
+ all_routes = Apipie.configuration.api_routes.routes.map do |r|
76
+ {
77
+ :verb => case r.verb
78
+ when Regexp then r.verb.source[/\w+/]
79
+ else r.verb.to_s
80
+ end,
81
+ :path => case
82
+ when r.path.respond_to?(:spec) then r.path.spec.to_s
83
+ else r.path.to_s
84
+ end,
85
+ :controller => r.requirements[:controller],
86
+ :action => r.requirements[:action]
87
+ }
78
88
  end
79
- api_routes = all_routes.find_all do |r|
89
+
90
+ return all_routes.find_all do |r|
80
91
  r[:path].starts_with?(Apipie.api_base_url)
81
92
  end
93
+ end
82
94
 
95
+ def populate_api_routes
83
96
  @apis_from_routes = Hash.new { |h, k| h[k] = [] }
84
97
 
85
- api_routes.each do |route|
98
+ all_api_routes.each do |route|
86
99
  controller_path, action = route[:controller], route[:action]
87
100
  next unless controller_path && action
88
101
 
89
102
  controller_path = controller_path.split('::').map(&:camelize).join('::')
90
103
  controller = "#{controller_path}Controller"
91
104
 
92
- path = if /^#{Regexp.escape(api_prefix)}(.*)$/ =~ route[:path]
105
+ path = if /^#{Regexp.escape(@api_prefix)}(.*)$/ =~ route[:path]
93
106
  $1.sub!(/\(\.:format\)$/,"")
94
107
  else
95
108
  nil
@@ -99,11 +112,13 @@ module Apipie
99
112
  @apis_from_routes[[controller, action]] << {:method => route[:verb], :path => path}
100
113
  end
101
114
  end
102
- @apis_from_routes
115
+ end
103
116
 
117
+ def all_apis_from_docs
104
118
  resource_descriptions = Apipie.resource_descriptions.values.map(&:values).flatten
105
119
  method_descriptions = resource_descriptions.map(&:method_descriptions).flatten
106
- apis_from_docs = method_descriptions.reduce({}) do |h, desc|
120
+
121
+ return method_descriptions.reduce({}) do |h, desc|
107
122
  apis = desc.method_apis_to_json.map do |api|
108
123
  { :method => api[:http_method],
109
124
  :path => api[:api_url],
@@ -111,21 +126,23 @@ module Apipie
111
126
  end
112
127
  h.update(desc.id => apis)
113
128
  end
129
+ end
114
130
 
131
+ def update_api_descriptions
132
+ apis_from_docs = all_apis_from_docs
115
133
  @apis_from_routes.each do |(controller, action), new_apis|
116
134
  method_key = "#{Apipie.get_resource_name(controller.constantize)}##{action}"
117
135
  old_apis = apis_from_docs[method_key] || []
118
136
  new_apis.each do |new_api|
119
137
  new_api[:path].sub!(/\(\.:format\)$/,"")
120
138
  old_api = old_apis.find do |api|
121
- api[:path] == "#{api_prefix}#{new_api[:path]}"
139
+ api[:path] == "#{@api_prefix}#{new_api[:path]}"
122
140
  end
123
141
  if old_api
124
142
  new_api[:desc] = old_api[:desc]
125
143
  end
126
144
  end
127
145
  end
128
- @apis_from_routes
129
146
  end
130
147
 
131
148
  end
@@ -13,7 +13,7 @@ module Apipie
13
13
  end
14
14
 
15
15
  def controller_full_path(controller)
16
- File.join(Rails.root, "app", "controllers", "#{controller.controller_path}_controller.rb")
16
+ Apipie::Extractor.controller_path controller.controller_path
17
17
  end
18
18
 
19
19
  def ignore_call?(record)
@@ -205,11 +205,11 @@ module Apipie
205
205
  end
206
206
 
207
207
  def controller_path
208
- @controller_path ||= File.join(Rails.root, "app", "controllers", "#{@controller.controller_path}_controller.rb")
208
+ @controller_path ||= Apipie::Extractor.controller_path(@controller.controller_path)
209
209
  end
210
210
 
211
211
  def controller_content
212
- raise ControllerNotFound.new unless File.exists? controller_path
212
+ raise ControllerNotFound.new unless controller_path && File.exists?(controller_path)
213
213
  @controller_content ||= File.read(controller_path)
214
214
  end
215
215
 
@@ -1,3 +1,3 @@
1
1
  module Apipie
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -30,6 +30,9 @@ Apipie.configure do |config|
30
30
  # for reloading to work properly you need to specify where your api controllers are (like in Dir.glob):
31
31
  config.api_controllers_matcher = File.join(Rails.root, "app", "controllers", "**","*.rb")
32
32
 
33
+ # specify routes if your application uses router differ from Rails default one
34
+ config.api_routes = Rails.application.routes
35
+
33
36
  # config.markup = choose one of:
34
37
  # Apipie::Markup::RDoc.new [default]
35
38
  # Apipie::Markup::Markdown.new
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.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-03-13 00:00:00.000000000 Z
13
+ date: 2014-03-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails