Class: Apipie::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/apipie/application.rb

Defined Under Namespace

Classes: Engine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

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_descriptionsObject (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)

Returns:

  • (Boolean)


364
365
366
# File 'lib/apipie/application.rb', line 364

def active_dsl?
  Apipie.configuration.validate? || ! Apipie.configuration.use_cache? || Apipie.configuration.force_dsl?
end

#add_param_group(controller, name, &block) ⇒ Object



138
139
140
141
# File 'lib/apipie/application.rb', line 138

def add_param_group(controller, name, &block)
  key = "#{controller.name}##{name}"
  @param_groups[key] = block
end

#api_controllers_pathsObject



315
316
317
# File 'lib/apipie/application.rb', line 315

def api_controllers_paths
  Dir.glob(Apipie.configuration.api_controllers_matcher)
end

#available_versionsObject



24
25
26
# File 'lib/apipie/application.rb', line 24

def available_versions
  @resource_descriptions.keys.sort
end

#checksumObject



357
358
359
# File 'lib/apipie/application.rb', line 357

def checksum
  @checksum ||= compute_checksum
end

#compute_checksumObject



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/apipie/application.rb', line 341

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
130
131
132
# File 'lib/apipie/application.rb', line 124

def controller_versions(controller)
  ret = @controller_versions[controller.to_s]
  return ret unless ret.empty?
  if controller == ActionController::Base || controller.nil?
    return [Apipie.configuration.default_version]
  else
    return controller_versions(controller.to_s.constantize.superclass)
  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_name_with_version = "#{version}##{get_resource_name(controller)}"
    resource_description = get_resource_description(resource_name_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

  return 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_name = get_resource_name(controller)
  resource_description = @resource_descriptions[version][resource_name]
  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_name, dsl_data, version)

    Apipie.debug("@resource_descriptions[#{version}][#{resource_name}] = #{resource_description}")
    @resource_descriptions[version][resource_name] ||= resource_description
  end

  return resource_description
end

#get_method_description(resource_name, 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_name:
    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_name: string containing both resource and method name joined
with '#' symbol.
- "users#create" get default version
- "v2#users#create" get specific version


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/apipie/application.rb', line 166

def get_method_description(resource_name, method_name = nil)
  if resource_name.is_a?(String)
    crumbs = resource_name.split('#')
    if method_name.nil?
      method_name = crumbs.pop
    end
    resource_name = crumbs.join("#")
    resource_description = get_resource_description(resource_name)
  elsif resource_name.respond_to? :apipie_resource_descriptions
    resource_description = get_resource_description(resource_name)
  else
    raise ArgumentError.new("Resource #{resource_name} does not exists.")
  end
  unless resource_description.nil?
    resource_description.method_description(method_name.to_sym)
  end
end

#get_method_descriptions(resource, method) ⇒ Object

get all versions of method description



223
224
225
226
227
# File 'lib/apipie/application.rb', line 223

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



143
144
145
146
147
148
149
150
# File 'lib/apipie/application.rb', line 143

def get_param_group(controller, name)
  key = "#{controller.name}##{name}"
  if @param_groups.has_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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/apipie/application.rb', line 189

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.has_key?(version)
      return @resource_descriptions[version][crumbs.last]
    end
  else
    resource_name = get_resource_name(resource)
    if version
      resource_name = "#{version}##{resource_name}"
    end

    if resource_name.nil?
      return nil
    end
    resource_description = get_resource_description(resource_name)
    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



216
217
218
219
220
# File 'lib/apipie/application.rb', line 216

def get_resource_descriptions(resource)
  available_versions.map do |version|
    get_resource_description(resource, version)
  end.compact
end

#get_resource_name(klass) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/apipie/application.rb', line 368

def get_resource_name(klass)
  if klass.class == String
    klass
  elsif @controller_to_resource_id.has_key?(klass)
    @controller_to_resource_id[klass]
  elsif Apipie.configuration.namespaced_resources? && klass.respond_to?(:controller_path)
    return nil if klass == ActionController::Base
    path = klass.controller_path
    path.gsub(version_prefix(klass), "").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

#init_envObject

initialize variables for gathering dsl data



239
240
241
242
243
244
245
246
247
# File 'lib/apipie/application.rb', line 239

def init_env
  @resource_descriptions ||= HashWithIndifferentAccess.new { |h, version| h[version] = {} }
  @controller_to_resource_id ||= {}
  @param_groups ||= {}
  @swagger_generator = Apipie::SwaggerGenerator.new(self)

  # 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

Raises:



258
259
260
261
262
# File 'lib/apipie/application.rb', line 258

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?
  @swagger_generator.json_schema_for_method_response(method, return_code, allow_nulls)
end

#json_schema_for_self_describing_class(cls, allow_nulls) ⇒ Object



264
265
266
# File 'lib/apipie/application.rb', line 264

def json_schema_for_self_describing_class(cls, allow_nulls)
  @swagger_generator.json_schema_for_self_describing_class(cls, allow_nulls)
end

#load_documentationObject



334
335
336
337
338
339
# File 'lib/apipie/application.rb', line 334

def load_documentation
  if !@documentation_loaded || Apipie.configuration.reload_controllers?
    Apipie.reload_documentation
    @documentation_loaded = true
  end
end

#localeObject



385
386
387
# File 'lib/apipie/application.rb', line 385

def locale
  Apipie.configuration.locale.call(nil) if Apipie.configuration.locale
end

#locale=(locale) ⇒ Object



389
390
391
# File 'lib/apipie/application.rb', line 389

def locale=(locale)
  Apipie.configuration.locale.call(locale) if Apipie.configuration.locale
end

#rails_routes(route_set = nil) ⇒ Object



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 32

def rails_routes(route_set = nil)
  if route_set.nil? && @rails_routes
    return @rails_routes
  end
  route_set ||= Rails.application.routes
  # ensure routes are loaded
  Rails.application.reload_routes! unless Rails.application.routes.routes.any?

  flatten_routes = []

  route_set.routes.each do |route|
    if route.app.respond_to?(:routes) && route.app.routes.is_a?(ActionDispatch::Routing::RouteSet)
      # recursively go though the moutned engines
      flatten_routes.concat(rails_routes(route.app.routes))
    else
      flatten_routes << route
    end
  end

  @rails_routes = flatten_routes
end

#recorded_examplesObject



249
250
251
252
# File 'lib/apipie/application.rb', line 249

def recorded_examples
  return @recorded_examples if @recorded_examples
  @recorded_examples = Apipie::Extractor::Writer.load_recorded_examples
end

#reload_documentationObject



319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/apipie/application.rb', line 319

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_examplesObject



254
255
256
# File 'lib/apipie/application.rb', line 254

def reload_examples
  @recorded_examples = nil
end

#remove_method_description(resource, versions, method_name) ⇒ Object



229
230
231
232
233
234
235
236
# File 'lib/apipie/application.rb', line 229

def remove_method_description(resource, versions, method_name)
  versions.each do |version|
    resource = get_resource_name(resource)
    if resource_description = get_resource_description("#{version}##{resource}")
      resource_description.remove_method_description(method_name)
    end
  end
end

#route_app_controller(app, route, visited_apps = []) ⇒ Object

the app might be nested when using contraints, namespaces etc. this method does in depth search for the route controller



56
57
58
59
60
61
# File 'lib/apipie/application.rb', line 56

def route_app_controller(app, route, visited_apps = [])
  if route.defaults[:controller]
    controller_name = "#{route.defaults[:controller]}_controller".camelize
    controller_name.safe_constantize
  end
end

#routes_for_action(controller, method, args) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/apipie/application.rb', line 63

def routes_for_action(controller, method, args)
  routes = rails_routes.select do |route|
    controller == route_app_controller(route.app, route) &&
        method.to_s == route.defaults[:action]
  end

  Apipie.configuration.routes_formatter.format_routes(routes, args)
end

#set_controller_versions(controller, versions) ⇒ Object



134
135
136
# File 'lib/apipie/application.rb', line 134

def set_controller_versions(controller, versions)
  @controller_versions[controller.to_s] = versions
end

#set_resource_id(controller, resource_id) ⇒ Object



28
29
30
# File 'lib/apipie/application.rb', line 28

def set_resource_id(controller, resource_id)
  @controller_to_resource_id[controller] = resource_id
end

#to_json(version, resource_name, method_name, lang) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/apipie/application.rb', line 286

def to_json(version, resource_name, method_name, lang)

  return unless valid_search_args?(version, resource_name, method_name)

  _resources = if resource_name.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_name].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_name, method_name, lang, clear_warnings = false) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/apipie/application.rb', line 268

def to_swagger_json(version, resource_name, method_name, lang, clear_warnings=false)
  return unless valid_search_args?(version, resource_name, method_name)

  # if resource_name is blank, take just resources which have some methods because
  # we dont want to show eg ApplicationController as resource
  # otherwise, take only the specified resource
  _resources = resource_descriptions[version].inject({}) do |result, (k,v)|
     if resource_name.blank?
       result[k] = v unless v._methods.blank?
     else
       result[k] = v if k == resource_name
     end
     result
   end

  @swagger_generator.generate_from_resources(version,_resources, method_name, lang, clear_warnings)
end

#translate(str, locale) ⇒ Object



393
394
395
396
397
398
399
# File 'lib/apipie/application.rb', line 393

def translate(str, locale)
  if Apipie.configuration.translate
    Apipie.configuration.translate.call(str, locale)
  else
    str
  end
end