Skip to content

Instantly share code, notes, and snippets.

@andriy-baran
Last active May 9, 2018 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andriy-baran/e16626099b3db7db9c1703fc43ccf1fb to your computer and use it in GitHub Desktop.
Save andriy-baran/e16626099b3db7db9c1703fc43ccf1fb to your computer and use it in GitHub Desktop.
module Rspec
class EndpointGenerator < Rails::Generators::NamedBase
desc 'Generate a spec/requests file for specified action of controller'
argument :controller, type: :string, required: true, banner: 'CONTOLLER'
source_root File.expand_path('../templates', __FILE__)
# https://www.rubytapas.com/2012/11/28/episode-029-redirecting-output/
def capture_output
fake_stdout = StringIO.new
old_stdout = $stdout
$stdout = fake_stdout
yield
ensure
$stdout = old_stdout
return fake_stdout.string
end
def copy_files
MyApp::Application.load_tasks
routes = capture_output { Rake::Task['routes'].invoke }
@route = routes.split("\n").grep(Regexp.new "#{controller}##{file_name}$").first
@http_verb = @route[/GET|POST|DELETE|PUT|PATCH/]
@path = @route[/(\/.*)\(/, 1]
@path_params = @path.split('/').select{|i| i[/:/]}
@path_params.each do |param|
@path.sub!(param, '#{' + param[1..-1] + '}')
end
empty_directory "spec/requests/#{controller}"
template 'action_request_spec.rb', "spec/requests/#{controller}/#{file_name}_spec.rb"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment