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
- #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_name, 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_name(klass) ⇒ Object
-
#init_env ⇒ Object
initialize variables for gathering dsl data.
-
#initialize ⇒ Application
constructor
A new instance of Application.
- #recorded_examples ⇒ Object
- #reload_documentation ⇒ Object
- #reload_examples ⇒ Object
- #remove_method_description(resource, versions, method_name) ⇒ Object
- #set_controller_versions(controller, versions) ⇒ Object
- #set_resource_id(controller, resource_id) ⇒ Object
- #to_json(version, resource_name, method_name) ⇒ Object
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)
286 287 288 |
# File 'lib/apipie/application.rb', line 286 def active_dsl? Apipie.configuration.validate? || ! Apipie.configuration.use_cache? || Apipie.configuration.force_dsl? end |
#add_param_group(controller, name, &block) ⇒ Object
98 99 100 101 |
# File 'lib/apipie/application.rb', line 98 def add_param_group(controller, name, &block) key = "#{controller.name}##{name}" @param_groups[key] = block end |
#api_controllers_paths ⇒ Object
257 258 259 |
# File 'lib/apipie/application.rb', line 257 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
279 280 281 |
# File 'lib/apipie/application.rb', line 279 def checksum @checksum ||= compute_checksum end |
#compute_checksum ⇒ Object
272 273 274 275 276 277 |
# File 'lib/apipie/application.rb', line 272 def compute_checksum all_docs = Apipie.available_versions.inject({}) do |all, version| all.update(version => Apipie.to_json(version)) end Digest::MD5.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.
84 85 86 87 88 89 90 91 92 |
# File 'lib/apipie/application.rb', line 84 def controller_versions(controller) ret = @controller_versions[controller] return ret unless ret.empty? if controller == ActionController::Base || controller.nil? return [Apipie.configuration.default_version] else return controller_versions(controller.superclass) end end |
#define_method_description(controller, method_name, dsl_data) ⇒ Object
create new method api description
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/apipie/application.rb', line 33 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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/apipie/application.rb', line 62 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
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/apipie/application.rb', line 126 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
183 184 185 186 187 |
# File 'lib/apipie/application.rb', line 183 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
103 104 105 106 107 108 109 110 |
# File 'lib/apipie/application.rb', line 103 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
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/apipie/application.rb', line 149 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 == resource return resource_description end end end |
#get_resource_descriptions(resource) ⇒ Object
get all versions of resource description
176 177 178 179 180 |
# File 'lib/apipie/application.rb', line 176 def get_resource_descriptions(resource) available_versions.map do |version| get_resource_description(resource, version) end.compact end |
#get_resource_name(klass) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/apipie/application.rb', line 290 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_env ⇒ Object
initialize variables for gathering dsl data
199 200 201 202 203 204 205 206 |
# File 'lib/apipie/application.rb', line 199 def init_env @resource_descriptions = 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] = [] } end |
#recorded_examples ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/apipie/application.rb', line 208 def recorded_examples return @recorded_examples if @recorded_examples tape_file = File.join(Rails.root,"doc","apipie_examples.yml") if File.exists?(tape_file) #if SafeYAML gem is enabled, it will load examples as an array of Hash, instead of hash if YAML.respond_to?(:safe_load_file) && defined?(SafeYAML) @recorded_examples = SafeYAML.load_file(tape_file, :safe=>false) else @recorded_examples = YAML.load_file(tape_file) end else @recorded_examples = {} end @recorded_examples end |
#reload_documentation ⇒ Object
261 262 263 264 265 266 267 268 269 270 |
# File 'lib/apipie/application.rb', line 261 def reload_documentation rails_mark_classes_for_reload init_env reload_examples api_controllers_paths.each do |f| load_controller_from_file f end @checksum = compute_checksum if Apipie.configuration.update_checksum end |
#reload_examples ⇒ Object
224 225 226 |
# File 'lib/apipie/application.rb', line 224 def reload_examples @recorded_examples = nil end |
#remove_method_description(resource, versions, method_name) ⇒ Object
189 190 191 192 193 194 195 196 |
# File 'lib/apipie/application.rb', line 189 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 |
#set_controller_versions(controller, versions) ⇒ Object
94 95 96 |
# File 'lib/apipie/application.rb', line 94 def set_controller_versions(controller, versions) @controller_versions[controller] = 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) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/apipie/application.rb', line 228 def to_json(version, resource_name, method_name) return unless resource_descriptions.has_key?(version) _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 unless v._methods.blank? result end else [@resource_descriptions[version][resource_name].to_json(method_name)] end url_args = Apipie.configuration.version_in_url ? version : '' { :docs => { :name => Apipie.configuration.app_name, :info => Apipie.app_info(version), :copyright => Apipie.configuration.copyright, :doc_url => Apipie.full_url(url_args), :api_url => Apipie.api_base_url(version), :resources => _resources } } end |