Class: Apipie::Application
- Inherits:
-
Object
- Object
- Apipie::Application
- Defined in:
- lib/apipie/application.rb
Defined Under Namespace
Classes: Engine
Instance Attribute Summary collapse
-
#resource_descriptions ⇒ Object
readonly
Returns the value of attribute resource_descriptions.
Instance Method Summary collapse
-
#active_dsl? ⇒ Boolean
Is there a reason to interpret the DSL for this run? with specific setting for some environment there is no reason the dsl should be interpreted (e.g. no validations and doc from cache).
- #add_param_group(controller, name, &block) ⇒ Object
- #api_controllers_paths ⇒ Object
- #available_versions ⇒ Object
- #checksum ⇒ Object
- #clear_cached_routes! ⇒ Object
- #compute_checksum ⇒ Object
-
#controller_versions(controller) ⇒ Object
recursively searches what versions has the controller specified in resource_description? It’s used to derivate the default value of versions for methods.
-
#define_method_description(controller, method_name, dsl_data) ⇒ Object
create new method api description.
-
#define_resource_description(controller, version, dsl_data = nil) ⇒ Object
create new resource api description.
-
#get_method_description(resource_id, method_name = nil) ⇒ Object
(also: #[])
get api for given method.
-
#get_method_descriptions(resource, method) ⇒ Object
get all versions of method description.
- #get_param_group(controller, name) ⇒ Object
-
#get_resource_description(resource, version = nil) ⇒ Object
options: => “users” => “v2#users” => V2::UsersController.
-
#get_resource_descriptions(resource) ⇒ Object
get all versions of resource description.
- #get_resource_id(klass) ⇒ Object
-
#get_resource_name(klass) ⇒ Object
deprecated
Deprecated.
Use #get_resource_id instead
-
#init_env ⇒ Object
initialize variables for gathering dsl data.
-
#initialize ⇒ Application
constructor
A new instance of Application.
- #json_schema_for_method_response(version, controller_name, method_name, return_code, allow_nulls) ⇒ Object
- #json_schema_for_self_describing_class(cls, allow_nulls) ⇒ Object
- #load_documentation ⇒ Object
- #locale ⇒ Object
- #locale=(locale) ⇒ Object
- #rails_routes(route_set = nil, base_url = "") ⇒ Object
- #rails_routes_by_controller_and_action ⇒ Object
- #recorded_examples ⇒ Object
- #reload_documentation ⇒ Object
- #reload_examples ⇒ Object
- #remove_method_description(resource, versions, method_name) ⇒ Object
- #routes_for_action(controller, method, args) ⇒ Object
- #set_controller_versions(controller, versions) ⇒ Object
- #set_resource_id(controller, resource_id) ⇒ Object
- #to_json(version, resource_id, method_name, lang) ⇒ Object
- #to_swagger_json(version, resource_id, method_name, language, clear_warnings = false) ⇒ Object
- #translate(str, locale) ⇒ Object
-
#value_from_parents(controller, *args, default: nil, &block) ⇒ Object
Recursively walks up the controller hierarchy looking for a value from the block.
Constructor Details
#initialize ⇒ Application
Returns a new instance of Application.
19 20 21 22 |
# File 'lib/apipie/application.rb', line 19 def initialize super init_env end |
Instance Attribute Details
#resource_descriptions ⇒ Object (readonly)
Returns the value of attribute resource_descriptions.
17 18 19 |
# File 'lib/apipie/application.rb', line 17 def resource_descriptions @resource_descriptions end |
Instance Method Details
#active_dsl? ⇒ Boolean
Is there a reason to interpret the DSL for this run? with specific setting for some environment there is no reason the dsl should be interpreted (e.g. no validations and doc from cache)
377 378 379 |
# File 'lib/apipie/application.rb', line 377 def active_dsl? Apipie.configuration.validate? || ! Apipie.configuration.use_cache? || Apipie.configuration.force_dsl? end |
#add_param_group(controller, name, &block) ⇒ Object
149 150 151 152 |
# File 'lib/apipie/application.rb', line 149 def add_param_group(controller, name, &block) key = "#{controller.name}##{name}" @param_groups[key] = block end |
#api_controllers_paths ⇒ Object
328 329 330 |
# File 'lib/apipie/application.rb', line 328 def api_controllers_paths Dir.glob(Apipie.configuration.api_controllers_matcher) end |
#available_versions ⇒ Object
24 25 26 |
# File 'lib/apipie/application.rb', line 24 def available_versions @resource_descriptions.keys.sort end |
#checksum ⇒ Object
370 371 372 |
# File 'lib/apipie/application.rb', line 370 def checksum @checksum ||= compute_checksum end |
#clear_cached_routes! ⇒ Object
61 62 63 64 |
# File 'lib/apipie/application.rb', line 61 def clear_cached_routes! @_rails_routes = nil @_rails_routes_by_controller_and_action = nil end |
#compute_checksum ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/apipie/application.rb', line 354 def compute_checksum if Apipie.configuration.use_cache? file_base = File.join(Apipie.configuration.cache_dir, Apipie.configuration.doc_base_url) all_docs = {} Dir.glob(file_base + '/*.json').sort.each do |f| all_docs[File.basename(f, '.json')] = JSON.parse(File.read(f)) end else load_documentation if available_versions == [] all_docs = Apipie.available_versions.inject({}) do |all, version| all.update(version => Apipie.to_json(version)) end end Digest::SHA1.hexdigest(JSON.dump(all_docs)) end |
#controller_versions(controller) ⇒ Object
recursively searches what versions has the controller specified in resource_description? It’s used to derivate the default value of versions for methods.
124 125 126 127 128 129 |
# File 'lib/apipie/application.rb', line 124 def controller_versions(controller) value_from_parents(controller, default: [Apipie.configuration.default_version]) do |c| ret = @controller_versions[c.to_s] ret unless ret.empty? end end |
#define_method_description(controller, method_name, dsl_data) ⇒ Object
create new method api description
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/apipie/application.rb', line 73 def define_method_description(controller, method_name, dsl_data) return if ignored?(controller, method_name) ret_method_description = nil versions = dsl_data[:api_versions] || [] versions = controller_versions(controller) if versions.empty? versions.each do |version| resource_id_with_version = "#{version}##{get_resource_id(controller)}" resource_description = get_resource_description(resource_id_with_version) if resource_description.nil? resource_description = define_resource_description(controller, version) end method_description = Apipie::MethodDescription.new(method_name, resource_description, dsl_data) # we create separate method description for each version in # case the method belongs to more versions. We return just one # becuase the version doesn't matter for the purpose it's used # (to wrap the original version with validators) ret_method_description ||= method_description resource_description.add_method_description(method_description) end ret_method_description end |
#define_resource_description(controller, version, dsl_data = nil) ⇒ Object
create new resource api description
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/apipie/application.rb', line 102 def define_resource_description(controller, version, dsl_data = nil) return if ignored?(controller) resource_id = get_resource_id(controller) resource_description = @resource_descriptions[version][resource_id] if resource_description # we already defined the description somewhere (probably in # some method. Updating just meta data from dsl resource_description.update_from_dsl_data(dsl_data) if dsl_data else resource_description = Apipie::ResourceDescription.new(controller, resource_id, dsl_data, version) Apipie.debug("@resource_descriptions[#{version}][#{resource_id}] = #{resource_description}") @resource_descriptions[version][resource_id] ||= resource_description end return resource_description end |
#get_method_description(resource_id, method_name = nil) ⇒ Object Also known as: []
get api for given method
There are two ways how this method can be used: 1) Specify both parameters
resource_id:
controller class - UsersController
string with resource name (plural) and version - "v1#users"
method_name: name of the method (string or symbol)
2) Specify only first parameter:
resource_id: string containing both resource and method name joined
with '#' symbol.
- "users#create" get default version
- "v2#users#create" get specific version
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/apipie/application.rb', line 177 def get_method_description(resource_id, method_name = nil) if resource_id.is_a?(String) crumbs = resource_id.split('#') if method_name.nil? method_name = crumbs.pop end resource_id = crumbs.join("#") resource_description = get_resource_description(resource_id) elsif resource_id.respond_to? :apipie_resource_descriptions resource_description = get_resource_description(resource_id) else raise ArgumentError.new("Resource #{resource_id} does not exists.") end resource_description&.method_description(method_name.to_sym) end |
#get_method_descriptions(resource, method) ⇒ Object
get all versions of method description
232 233 234 235 236 |
# File 'lib/apipie/application.rb', line 232 def get_method_descriptions(resource, method) get_resource_descriptions(resource).map do |resource_description| resource_description.method_description(method.to_sym) end.compact end |
#get_param_group(controller, name) ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'lib/apipie/application.rb', line 154 def get_param_group(controller, name) key = "#{controller.name}##{name}" if @param_groups.key?(key) return @param_groups[key] else raise "param group #{key} not defined" end end |
#get_resource_description(resource, version = nil) ⇒ Object
options:
> “users”
> “v2#users”
> V2::UsersController
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/apipie/application.rb', line 198 def get_resource_description(resource, version = nil) if resource.is_a?(String) crumbs = resource.split('#') if crumbs.size == 2 version = crumbs.first end version ||= Apipie.configuration.default_version if @resource_descriptions.key?(version) return @resource_descriptions[version][crumbs.last] end else resource_id = get_resource_id(resource) if version resource_id = "#{version}##{resource_id}" end if resource_id.nil? return nil end resource_description = get_resource_description(resource_id) if resource_description && resource_description.controller.to_s == resource.to_s return resource_description end end end |
#get_resource_descriptions(resource) ⇒ Object
get all versions of resource description
225 226 227 228 229 |
# File 'lib/apipie/application.rb', line 225 def get_resource_descriptions(resource) available_versions.map do |version| get_resource_description(resource, version) end.compact end |
#get_resource_id(klass) ⇒ Object
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/apipie/application.rb', line 397 def get_resource_id(klass) if klass.class == String klass elsif @controller_to_resource_id.key?(klass) @controller_to_resource_id[klass] elsif Apipie.configuration.namespaced_resources? && klass.respond_to?(:controller_path) return nil if klass == ActionController::Base version_prefix = version_prefix(klass) path = klass.controller_path path = if version_prefix == '/' path else path.gsub(version_prefix, '') end path.gsub('/', '-') elsif klass.respond_to?(:controller_name) return nil if klass == ActionController::Base klass.controller_name else raise "Apipie: Can not resolve resource #{klass} name." end end |
#get_resource_name(klass) ⇒ Object
Use #get_resource_id instead
382 383 384 385 386 387 388 389 390 391 |
# File 'lib/apipie/application.rb', line 382 def get_resource_name(klass) ActiveSupport::Deprecation.warn( <<~HEREDOC Apipie::Application.get_resource_name is deprecated. Use `Apipie::Application.get_resource_id instead. HEREDOC ) get_resource_id(klass) end |
#init_env ⇒ Object
initialize variables for gathering dsl data
248 249 250 251 252 253 254 255 |
# File 'lib/apipie/application.rb', line 248 def init_env @resource_descriptions = ActiveSupport::HashWithIndifferentAccess.new { |h, version| h[version] = {} } @controller_to_resource_id = {} @param_groups = {} # what versions does the controller belong in (specified by resource_description)? @controller_versions = Hash.new { |h, controller| h[controller.to_s] = [] } end |
#json_schema_for_method_response(version, controller_name, method_name, return_code, allow_nulls) ⇒ Object
266 267 268 269 270 271 272 |
# File 'lib/apipie/application.rb', line 266 def json_schema_for_method_response(version, controller_name, method_name, return_code, allow_nulls) method = @resource_descriptions[version][controller_name].method_description(method_name) raise NoDocumentedMethod.new(controller_name, method_name) if method.nil? Apipie::SwaggerGenerator .json_schema_for_method_response(method, return_code, allow_nulls) end |
#json_schema_for_self_describing_class(cls, allow_nulls) ⇒ Object
274 275 276 277 |
# File 'lib/apipie/application.rb', line 274 def json_schema_for_self_describing_class(cls, allow_nulls) Apipie::SwaggerGenerator .json_schema_for_self_describing_class(cls, allow_nulls) end |
#load_documentation ⇒ Object
347 348 349 350 351 352 |
# File 'lib/apipie/application.rb', line 347 def load_documentation if !@documentation_loaded || Apipie.configuration.reload_controllers? Apipie.reload_documentation @documentation_loaded = true end end |
#locale ⇒ Object
424 425 426 |
# File 'lib/apipie/application.rb', line 424 def locale Apipie.configuration.locale&.call(nil) end |
#locale=(locale) ⇒ Object
428 429 430 |
# File 'lib/apipie/application.rb', line 428 def locale=(locale) Apipie.configuration.locale&.call(locale) end |
#rails_routes(route_set = nil, base_url = "") ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/apipie/application.rb', line 28 def rails_routes(route_set = nil, base_url = "") return @_rails_routes if route_set.nil? && @_rails_routes route_set ||= Rails.application.routes # ensure routes are loaded Rails.application.reload_routes! unless Rails.application.routes.routes.any? flattened_routes = [] route_set.routes.each do |route| # route is_a ActionDispatch::Journey::Route # route.app is_a ActionDispatch::Routing::Mapper::Constraints # route.app.app is_a TestEngine::Engine route_app = route.app.app if route_app.respond_to?(:routes) && route_app.routes.is_a?(ActionDispatch::Routing::RouteSet) # recursively go though the mounted engines flattened_routes.concat(rails_routes(route_app.routes, File.join(base_url, route.path.spec.to_s))) else route.base_url = base_url flattened_routes << route end end @_rails_routes = flattened_routes end |
#rails_routes_by_controller_and_action ⇒ Object
54 55 56 57 58 59 |
# File 'lib/apipie/application.rb', line 54 def rails_routes_by_controller_and_action @_rails_routes_by_controller_and_action = rails_routes.group_by do |route| requirements = route.requirements [requirements[:controller], requirements[:action]] end end |
#recorded_examples ⇒ Object
257 258 259 260 |
# File 'lib/apipie/application.rb', line 257 def recorded_examples return @recorded_examples if @recorded_examples @recorded_examples = Apipie::Extractor::Writer.load_recorded_examples end |
#reload_documentation ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/apipie/application.rb', line 332 def reload_documentation # don't load translated strings, we'll translate them later old_locale = locale locale = Apipie.configuration.default_locale rails_mark_classes_for_reload api_controllers_paths.each do |f| load_controller_from_file f end @checksum = nil if Apipie.configuration.update_checksum locale = old_locale end |
#reload_examples ⇒ Object
262 263 264 |
# File 'lib/apipie/application.rb', line 262 def reload_examples @recorded_examples = nil end |
#remove_method_description(resource, versions, method_name) ⇒ Object
238 239 240 241 242 243 244 245 |
# File 'lib/apipie/application.rb', line 238 def remove_method_description(resource, versions, method_name) versions.each do |version| resource = get_resource_id(resource) if resource_description = get_resource_description("#{version}##{resource}") resource_description.remove_method_description(method_name) end end end |
#routes_for_action(controller, method, args) ⇒ Object
66 67 68 69 70 |
# File 'lib/apipie/application.rb', line 66 def routes_for_action(controller, method, args) routes = rails_routes_by_controller_and_action[[controller.name.underscore.chomp('_controller'), method.to_s]] || [] Apipie.configuration.routes_formatter.format_routes(routes, args) end |
#set_controller_versions(controller, versions) ⇒ Object
145 146 147 |
# File 'lib/apipie/application.rb', line 145 def set_controller_versions(controller, versions) @controller_versions[controller.to_s] = versions end |
#set_resource_id(controller, resource_id) ⇒ Object
393 394 395 |
# File 'lib/apipie/application.rb', line 393 def set_resource_id(controller, resource_id) @controller_to_resource_id[controller] = resource_id end |
#to_json(version, resource_id, method_name, lang) ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/apipie/application.rb', line 299 def to_json(version, resource_id, method_name, lang) return unless valid_search_args?(version, resource_id, method_name) _resources = if resource_id.blank? # take just resources which have some methods because # we dont want to show eg ApplicationController as resource resource_descriptions[version].inject({}) do |result, (k,v)| result[k] = v.to_json(nil, lang) unless v._methods.blank? result end else [@resource_descriptions[version][resource_id].to_json(method_name, lang)] end url_args = Apipie.configuration.version_in_url ? version : '' { :docs => { :name => Apipie.configuration.app_name, :info => Apipie.app_info(version, lang), :copyright => Apipie.configuration.copyright, :doc_url => Apipie.full_url(url_args), :api_url => Apipie.api_base_url(version), :resources => _resources } } end |
#to_swagger_json(version, resource_id, method_name, language, clear_warnings = false) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/apipie/application.rb', line 279 def to_swagger_json(version, resource_id, method_name, language, clear_warnings = false) return unless valid_search_args?(version, resource_id, method_name) resources = Apipie::Generator::Swagger::ResourceDescriptionsCollection .new(resource_descriptions) .filter( resource_id: resource_id, method_name: method_name, version: version ) Apipie::SwaggerGenerator.generate_from_resources( resources, version: version, language: language, clear_warnings: clear_warnings ) end |
#translate(str, locale) ⇒ Object
432 433 434 435 436 437 438 |
# File 'lib/apipie/application.rb', line 432 def translate(str, locale) if Apipie.configuration.translate Apipie.configuration.translate.call(str, locale) else str end end |
#value_from_parents(controller, *args, default: nil, &block) ⇒ Object
Recursively walks up the controller hierarchy looking for a value from the block. Stops at ActionController::Base.
138 139 140 141 142 143 |
# File 'lib/apipie/application.rb', line 138 def value_from_parents(controller, *args, default: nil, &block) return default if controller == ActionController::Base || controller == AbstractController::Base || controller.nil? thing = yield(controller, *args) thing || value_from_parents(controller.superclass, *args, default: default, &block) end |