class GrapeSwagger::Rake::OapiTasks
Attributes
oapi[R]
Public Class Methods
new(api_class)
click to toggle source
Calls superclass method
# File lib/grape-swagger/rake/oapi_tasks.rb, line 14 def initialize(api_class) super() if api_class.is_a? String @api_class_name = api_class else @api_class = api_class end define_tasks end
Private Instance Methods
api_class()
click to toggle source
# File lib/grape-swagger/rake/oapi_tasks.rb, line 28 def api_class @api_class ||= @api_class_name.constantize end
app()
click to toggle source
# File lib/grape-swagger/rake/oapi_tasks.rb, line 130 def app api_class.new end
define_tasks()
click to toggle source
# File lib/grape-swagger/rake/oapi_tasks.rb, line 32 def define_tasks namespace :oapi do fetch validate end end
error?()
click to toggle source
# File lib/grape-swagger/rake/oapi_tasks.rb, line 114 def error? JSON.parse(@oapi).keys.first == 'error' end
fetch()
click to toggle source
tasks
get swagger/OpenAPI documentation
# File lib/grape-swagger/rake/oapi_tasks.rb, line 42 def fetch desc 'generates OpenApi documentation … params (usage: key=value): store – save as JSON file, default: false (optional) resource - if given only for that it would be generated (optional)' task fetch: :environment do # :nocov: urls_for(api_class).each do |url| make_request(url) save_to_file? ? File.write(file(url), @oapi) : $stdout.print(@oapi) end # :nocov: end end
file(url)
click to toggle source
# File lib/grape-swagger/rake/oapi_tasks.rb, line 118 def file(url) api_version = url.split('/').last name = if ENV.fetch('store', nil) == 'true' || ENV.fetch('store', nil).blank? "swagger_doc_#{api_version}.json" else ENV.fetch('store').sub('.json', "_#{api_version}.json") end File.join(Dir.getwd, name) end
format_path(path)
click to toggle source
# File lib/grape-swagger/rake/oapi_tasks.rb, line 104 def format_path(path) oapi_route = api_class.routes.select { |e| e.path == path }.first path = path.sub(/\(\.\w+\)$/, '').sub(/\(\.:\w+\)$/, '') path.sub(':version', oapi_route.version.to_s) end
make_request(url)
click to toggle source
helper methods
rubocop:disable Style/StringConcatenation
# File lib/grape-swagger/rake/oapi_tasks.rb, line 86 def make_request(url) get url @oapi = JSON.pretty_generate( JSON.parse(last_response.body, symolize_names: true) ) + "\n" end
save_to_file?()
click to toggle source
# File lib/grape-swagger/rake/oapi_tasks.rb, line 110 def save_to_file? ENV.fetch('store', nil).present? && !error? end
urls_for(api_class)
click to toggle source
rubocop:enable Style/StringConcatenation
# File lib/grape-swagger/rake/oapi_tasks.rb, line 95 def urls_for(api_class) api_class.routes .map(&:path) .grep(/#{GrapeSwagger::DocMethods.class_variable_get(:@@mount_path)}/) .reject { |e| e.include?(':name') } .map { |e| format_path(e) } .map { |e| [e, ENV.fetch('resource', nil)].join('/').chomp('/') } end
validate()
click to toggle source
validates swagger/OpenAPI documentation
# File lib/grape-swagger/rake/oapi_tasks.rb, line 60 def validate desc 'validates the generated OpenApi file … params (usage: key=value): resource - if given only for that it would be generated (optional)' task validate: :environment do # :nocov: ENV.store('store', 'true') ::Rake::Task['oapi:fetch'].invoke exit if error? urls_for(api_class).each do |url| @output = system "swagger-cli validate #{file(url)}" FileUtils.rm( file(url) ) end $stdout.puts 'install swagger-cli with `npm install swagger-cli -g`' if @output.nil? # :nocov: end end