Skip to content

Commit

Permalink
Replace get_resource_name with get_resource_id (#864)
Browse files Browse the repository at this point in the history
* Replace `.get_resource_name` with `.get_resource_id`.

Although the function is named `get_resource_name` in reality it's generating
the ID that is used as an id of the resource description as well as in the
URLs of apipie.

It replaces `.get_resource_name` with `.get_resource_id` to avoid confusion
with ResourceDescription#name.

* Rename `resource_name` to `resource_id`

* Fix rubocop warnings
  • Loading branch information
PanosCodes authored Apr 13, 2023
1 parent 58cac79 commit 43f95a6
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 83 deletions.
2 changes: 1 addition & 1 deletion app/controllers/apipie/apipies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def authorized_doc
authorize_resource(resource)
end
else
@doc[:docs][:resources].select do |_resource_name, resource|
@doc[:docs][:resources].select do |_resource_id, resource|
authorize_resource(resource)
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/apipie/apipie_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ def self.app
@application ||= Apipie::Application.new
end

def self.to_json(version = nil, resource_name = nil, method_name = nil, lang = nil)
def self.to_json(version = nil, resource_id = nil, method_name = nil, lang = nil)
version ||= Apipie.configuration.default_version
app.to_json(version, resource_name, method_name, lang)
app.to_json(version, resource_id, method_name, lang)
end

def self.to_swagger_json(version = nil, resource_name = nil, method_name = nil, lang = nil, clear_warnings = true)
def self.to_swagger_json(version = nil, resource_id = nil, method_name = nil, lang = nil, clear_warnings = true)
version ||= Apipie.configuration.default_version
app.to_swagger_json(version, resource_name, method_name, lang, clear_warnings)
app.to_swagger_json(version, resource_id, method_name, lang, clear_warnings)
end

def self.json_schema_for_method_response(controller_name, method_name, return_code, allow_nulls)
Expand Down
92 changes: 52 additions & 40 deletions lib/apipie/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ def available_versions
@resource_descriptions.keys.sort
end

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

def rails_routes(route_set = nil, base_url = "")
if route_set.nil? && @rails_routes
return @rails_routes
Expand Down Expand Up @@ -83,8 +79,8 @@ def define_method_description(controller, method_name, dsl_data)
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)
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)
Expand All @@ -100,24 +96,24 @@ def define_method_description(controller, method_name, dsl_data)
resource_description.add_method_description(method_description)
end

return ret_method_description
ret_method_description
end

# create new resource api description
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]
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_name, dsl_data, version)
resource_description = Apipie::ResourceDescription.new(controller, resource_id, dsl_data, version)

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

return resource_description
Expand Down Expand Up @@ -158,28 +154,28 @@ def get_param_group(controller, name)
#
# There are two ways how this method can be used:
# 1) Specify both parameters
# resource_name:
# 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_name: string containing both resource and method name joined
# resource_id: string containing both resource and method name joined
# with '#' symbol.
# - "users#create" get default version
# - "v2#users#create" get specific version
def get_method_description(resource_name, method_name = nil)
if resource_name.is_a?(String)
crumbs = resource_name.split('#')
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_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)
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_name} does not exists.")
raise ArgumentError.new("Resource #{resource_id} does not exists.")
end
resource_description&.method_description(method_name.to_sym)
end
Expand All @@ -200,15 +196,15 @@ def get_resource_description(resource, version = nil)
return @resource_descriptions[version][crumbs.last]
end
else
resource_name = get_resource_name(resource)
resource_id = get_resource_id(resource)
if version
resource_name = "#{version}##{resource_name}"
resource_id = "#{version}##{resource_id}"
end

if resource_name.nil?
if resource_id.nil?
return nil
end
resource_description = get_resource_description(resource_name)
resource_description = get_resource_description(resource_id)
if resource_description && resource_description.controller.to_s == resource.to_s
return resource_description
end
Expand All @@ -231,7 +227,7 @@ def get_method_descriptions(resource, method)

def remove_method_description(resource, versions, method_name)
versions.each do |version|
resource = get_resource_name(resource)
resource = resource_id(resource)
if resource_description = get_resource_description("#{version}##{resource}")
resource_description.remove_method_description(method_name)
end
Expand Down Expand Up @@ -270,14 +266,14 @@ def json_schema_for_self_describing_class(cls, allow_nulls)
.json_schema_for_self_describing_class(cls, allow_nulls)
end

def to_swagger_json(version, resource_name, method_name, language, clear_warnings = false)
return unless valid_search_args?(version, resource_name, method_name)
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_name: resource_name,
resource_id: resource_id,
method_name: method_name,
version: version
)
Expand All @@ -290,19 +286,19 @@ def to_swagger_json(version, resource_name, method_name, language, clear_warning
)
end

def to_json(version, resource_name, method_name, lang)
def to_json(version, resource_id, method_name, lang)

return unless valid_search_args?(version, resource_name, method_name)
return unless valid_search_args?(version, resource_id, method_name)

_resources = if resource_name.blank?
_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_name].to_json(method_name, lang)]
[@resource_descriptions[version][resource_id].to_json(method_name, lang)]
end

url_args = Apipie.configuration.version_in_url ? version : ''
Expand Down Expand Up @@ -372,7 +368,23 @@ def active_dsl?
Apipie.configuration.validate? || ! Apipie.configuration.use_cache? || Apipie.configuration.force_dsl?
end

# @deprecated Use {#get_resource_id} instead
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

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

def get_resource_id(klass)
if klass.class == String
klass
elsif @controller_to_resource_id.key?(klass)
Expand Down Expand Up @@ -417,14 +429,14 @@ def translate(str, locale)

private

# Make sure that the version/resource_name/method_name are valid combination
# resource_name and method_name can be nil
def valid_search_args?(version, resource_name, method_name)
# Make sure that the version/resource_id/method_name are valid combination
# resource_id and method_name can be nil
def valid_search_args?(version, resource_id, method_name)
return false unless self.resource_descriptions.key?(version)
if resource_name
return false unless self.resource_descriptions[version].key?(resource_name)
if resource_id
return false unless self.resource_descriptions[version].key?(resource_id)
if method_name
resource_description = self.resource_descriptions[version][resource_name]
resource_description = self.resource_descriptions[version][resource_id]
return false unless resource_description.valid_method_name?(method_name)
end
end
Expand Down
9 changes: 4 additions & 5 deletions lib/apipie/dsl_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ def _apipie_dsl_data_init
end

module Resource
# by default, the resource id is derived from controller_name
# it can be overwritten with.
#
# resource_id "my_own_resource_id"
def resource_id(resource_id)
Apipie.set_resource_id(@controller, resource_id)
Expand Down Expand Up @@ -529,8 +526,10 @@ def apipie_update_methods(methods, *args, &block)
alias apipie_update_params apipie_update_methods

def _apipie_concern_subst
@_apipie_concern_subst ||= {:controller_path => self.controller_path,
:resource_id => Apipie.get_resource_name(self)}
@_apipie_concern_subst ||= {
controller_path: self.controller_path,
resource_id: Apipie.get_resource_id(self)
}
end

def _apipie_perform_concern_subst(string)
Expand Down
2 changes: 1 addition & 1 deletion lib/apipie/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def all_apis_from_docs
def update_api_descriptions
apis_from_docs = all_apis_from_docs
@apis_from_routes.each do |(controller, action), new_apis|
method_key = "#{Apipie.get_resource_name(controller.safe_constantize || next)}##{action}"
method_key = "#{Apipie.get_resource_id(controller.safe_constantize || next)}##{action}"
old_apis = apis_from_docs[method_key] || []
new_apis.each do |new_api|
new_api[:path]&.sub!(/\(\.:format\)$/,"")
Expand Down
4 changes: 2 additions & 2 deletions lib/apipie/extractor/collector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def controller_full_path(controller)
def ignore_call?(record)
return true unless record[:controller]
return true if @ignored.include?(record[:controller].name)
return true if @ignored.include?("#{Apipie.get_resource_name(record[:controller].name)}##{record[:action]}")
return true if @ignored.include?("#{Apipie.resource_id(record[:controller].name)}##{record[:action]}")
return true unless @api_controllers_paths.include?(controller_full_path(record[:controller]))
end

Expand All @@ -33,7 +33,7 @@ def handle_record(record)
end

def add_to_records(record)
key = "#{Apipie.get_resource_name(record[:controller])}##{record[:action]}"
key = "#{Apipie.get_resource_id(record[:controller])}##{record[:action]}"
@records[key] << record
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ def initialize(resource_descriptions)
end

# @return [Array<Apipie::ResourceDescription>]
def filter(version:, resource_name:, method_name: nil)
def filter(version:, resource_id:, method_name: nil)
resources = []

# If resource_name is blank, take just resources which have some methods because
# If resource_id 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
@resource_descriptions[version].each do |resource_description_name, resource_description|
if (resource_name.blank? && resource_description._methods.present?) || resource_description_name == resource_name
@resource_descriptions[version].each do |resource_description_id, resource_description|
if (resource_id.blank? && resource_description._methods.present?) || resource_description_id == resource_id
resources << resource_description
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/apipie/resource_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def name
alias _name name

def add_method_description(method_description)
Apipie.debug "@resource_descriptions[#{self._version}][#{self._name}]._methods[#{method_description.method}] = #{method_description}"
Apipie.debug "@resource_descriptions[#{self._version}][#{self._id}]._methods[#{method_description.method}] = #{method_description}"
@_methods[method_description.method.to_sym] = method_description
end

Expand Down
12 changes: 6 additions & 6 deletions lib/tasks/apipie.rake
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,11 @@ namespace :apipie do
end

def generate_resource_pages(version, file_base, doc, include_json = false, lang = nil)
doc[:docs][:resources].each do |resource_name, _|
resource_file_base = File.join(file_base, resource_name.to_s)
doc[:docs][:resources].each do |resource_id, _|
resource_file_base = File.join(file_base, resource_id.to_s)
FileUtils.mkdir_p(File.dirname(resource_file_base)) unless File.exist?(File.dirname(resource_file_base))

doc = Apipie.to_json(version, resource_name, nil, lang)
doc = Apipie.to_json(version, resource_id, nil, lang)
doc[:docs][:link_extension] = (lang ? ".#{lang}.html" : ".html")
render_page("#{resource_file_base}#{lang_ext(lang)}.html", "resource", {:doc => doc[:docs],
:resource => doc[:docs][:resources].first, :language => lang, :languages => Apipie.configuration.languages})
Expand All @@ -267,12 +267,12 @@ namespace :apipie do
end

def generate_method_pages(version, file_base, doc, include_json = false, lang = nil)
doc[:docs][:resources].each do |resource_name, resource_params|
doc[:docs][:resources].each do |resource_id, resource_params|
resource_params[:methods].each do |method|
method_file_base = File.join(file_base, resource_name.to_s, method[:name].to_s)
method_file_base = File.join(file_base, resource_id.to_s, method[:name].to_s)
FileUtils.mkdir_p(File.dirname(method_file_base)) unless File.exist?(File.dirname(method_file_base))

doc = Apipie.to_json(version, resource_name, method[:name], lang)
doc = Apipie.to_json(version, resource_id, method[:name], lang)
doc[:docs][:link_extension] = (lang ? ".#{lang}.html" : ".html")
render_page("#{method_file_base}#{lang_ext(lang)}.html", "method", {:doc => doc[:docs],
:resource => doc[:docs][:resources].first,
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/api/v2/nested/resources_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'spec_helper'

describe Api::V2::Nested::ResourcesController do
describe "resource id" do
subject { Apipie.get_resource_name(Api::V2::Nested::ResourcesController) }
describe '.get_resource_id' do
subject { Apipie.get_resource_id(Api::V2::Nested::ResourcesController) }

it "should have resource_id set" do
expect(subject).to eq("resource")
Expand Down
23 changes: 16 additions & 7 deletions spec/lib/apipie/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@

end

describe '.get_resource_name' do
subject(:get_resource_name) do
Apipie.get_resource_name(Api::V2::Nested::ArchitecturesController)
end

shared_examples 'resource id' do
let(:resource_class) { Api::V2::Nested::ArchitecturesController }
let(:base_url) { '/some-api' }

before { allow(Apipie.app).to receive(:get_base_url).and_return(base_url) }
before { allow(described_class).to receive(:get_base_url).and_return(base_url) }

context "with namespaced_resources enabled" do
before { Apipie.configuration.namespaced_resources = true }
Expand All @@ -37,7 +34,7 @@
let(:base_url) { nil }

it "should not raise an error" do
expect { get_resource_name }.not_to raise_error
expect { method_call }.not_to raise_error
end
end
end
Expand All @@ -50,4 +47,16 @@
end
end
end

describe '.get_resource_id' do
subject(:method_call) { Apipie.get_resource_id(resource_class) }

it_behaves_like 'resource id'
end

describe '.get_resource_name' do
subject(:method_call) { Apipie.get_resource_name(resource_class) }

it_behaves_like 'resource id'
end
end
Loading

0 comments on commit 43f95a6

Please sign in to comment.