Skip to content

Commit

Permalink
Extract find_extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksei committed Apr 11, 2024
1 parent 52052d6 commit 2527d79
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 34 deletions.
1 change: 1 addition & 0 deletions lib/rspec/openapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require 'rspec/openapi/schema_cleaner'
require 'rspec/openapi/schema_sorter'
require 'rspec/openapi/key_transformer'
require 'rspec/openapi/shared_hooks'
require 'rspec/openapi/extractors'
require 'rspec/openapi/extractors/rack'

Expand Down
18 changes: 1 addition & 17 deletions lib/rspec/openapi/minitest_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,11 @@ def run(*args)
human_name = name.sub(/^test_/, '').gsub('_', ' ')
example = Example.new(self, human_name, {}, file_path)
path = RSpec::OpenAPI.path.then { |p| p.is_a?(Proc) ? p.call(example) : p }
record = RSpec::OpenAPI::RecordBuilder.build(self, example: example, extractor: find_extractor)
record = RSpec::OpenAPI::RecordBuilder.build(self, example: example, extractor: SharedHooks.find_extractor)
RSpec::OpenAPI.path_records[path] << record if record
end
result
end

def find_extractor
names = Bundler.load.specs.map(&:name)

if names.include?('rails') && defined?(Rails) &&
Rails.respond_to?(:application) && Rails.application
RSpec::OpenAPI::Extractors::Rails
elsif names.include?('hanami') && defined?(Hanami) &&
Hanami.respond_to?(:app) && Hanami.app?
RSpec::OpenAPI::Extractors::Hanami
# elsif defined?(Roda)
# some Roda extractor
else
RSpec::OpenAPI::Extractors::Rack
end
end
end

module ActivateOpenApiClassMethods
Expand Down
18 changes: 1 addition & 17 deletions lib/rspec/openapi/rspec_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
RSpec.configuration.after(:each) do |example|
if RSpec::OpenAPI.example_types.include?(example.metadata[:type]) && example.metadata[:openapi] != false
path = RSpec::OpenAPI.path.then { |p| p.is_a?(Proc) ? p.call(example) : p }
record = RSpec::OpenAPI::RecordBuilder.build(self, example: example, extractor: find_extractor)
record = RSpec::OpenAPI::RecordBuilder.build(self, example: example, extractor: SharedHooks.find_extractor)
RSpec::OpenAPI.path_records[path] << record if record
end
end
Expand All @@ -19,19 +19,3 @@
RSpec.configuration.reporter.message colorizer.wrap(error_message, :failure)
end
end

def find_extractor
names = Bundler.load.specs.map(&:name)

if names.include?('rails') && defined?(Rails) &&
Rails.respond_to?(:application) && Rails.application
RSpec::OpenAPI::Extractors::Rails
elsif names.include?('hanami') && defined?(Hanami) &&
Hanami.respond_to?(:app) && Hanami.app?
RSpec::OpenAPI::Extractors::Hanami
# elsif defined?(Roda)
# some Roda extractor
else
RSpec::OpenAPI::Extractors::Rack
end
end
17 changes: 17 additions & 0 deletions lib/rspec/openapi/shared_hooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module SharedHooks
def self.find_extractor
names = Bundler.load.specs.map(&:name)

if names.include?('rails') && defined?(Rails) &&
Rails.respond_to?(:application) && Rails.application
RSpec::OpenAPI::Extractors::Rails
elsif names.include?('hanami') && defined?(Hanami) &&
Hanami.respond_to?(:app) && Hanami.app?
RSpec::OpenAPI::Extractors::Hanami
# elsif defined?(Roda)
# some Roda extractor
else
RSpec::OpenAPI::Extractors::Rack
end
end
end

0 comments on commit 2527d79

Please sign in to comment.