class Dredd::Rack::RakeTask

A clonable Rake task powered by a Dredd::Rack::Runner

Examples:

require 'dredd/rack'
Dredd::Rack::RakeTask.new # run it with `rake dredd`

# Customize the name or description of the Rake task:
namespace :blueprint do
  desc 'Verify an API complies with its blueprint'
  Dredd::Rack::RakeTask.new(:verify)
end
# run it with `rake blueprint:verify`

Attributes

description[R]

Return the task's description

name[R]

Return the task's name

runner[R]

Return the Dredd::Rack::Runner instance

Public Class Methods

new(*args, &task_block) click to toggle source

Define a task with a custom name, arguments and description

# File lib/dredd/rack/rake_task.rb, line 50
def initialize(*args, &task_block)
  @name = args.shift || :dredd
  @description = 'Run Dredd::Rack API blueprint verification'
  @runner = Dredd::Rack::Runner.new(ENV['API_HOST'])

  desc description unless ::Rake.application.last_description
  rake_task = task name, *args do |task_args|
    task_block.call(*[self, task_args].slice(0, task_block.arity)) if task_block
    run_task(runner)
  end

  integrate_with_rails!(rake_task)
end

Private Instance Methods

command_message(runner) click to toggle source
# File lib/dredd/rack/rake_task.rb, line 74
        def command_message(runner)
          <<-eos.gsub /^( |\t)+/, ""
            #{runner.command}

          eos
        end
connection_error_message(runner) click to toggle source
# File lib/dredd/rack/rake_task.rb, line 81
        def connection_error_message(runner)
          <<-eos.gsub /^( |\t)+/, ""

            #{Rainbow("Something went wrong.").red}
            Maybe your API is not being served at #{runner.api_endpoint}?

            Note that specifying a different host is easy:
            #{Rainbow('`rake blueprint:verify API_HOST=http://localhost:4567`').yellow}

          eos
        end
dredd_available?() click to toggle source
# File lib/dredd/rack/rake_task.rb, line 66
def dredd_available?
  system ['which', Dredd::Rack.dredd_command].join(' ')
end
dredd_connection_error?(exit_status) click to toggle source
# File lib/dredd/rack/rake_task.rb, line 70
def dredd_connection_error?(exit_status)
  exit_status == 8
end
dredd_not_available_message() click to toggle source
# File lib/dredd/rack/rake_task.rb, line 93
        def dredd_not_available_message
          <<-eos.gsub /^( |\t)+/, ""

            The #{Rainbow('dredd').red} blueprint testing tool is not available.
            You may want to install it in order to validate the API blueprints.

            Try #{Rainbow('`npm install dredd --global`').yellow} (use `sudo` if necessary)
            or see https://github.com/apiaryio/dredd for instructions.

          eos
        end
integrate_with_rails!(rake_task) click to toggle source
# File lib/dredd/rack/rake_task.rb, line 127
def integrate_with_rails!(rake_task)
  rake_task.enhance([:environment]) if Rake::Task.task_defined? :environment
end
run_task(runner) click to toggle source
# File lib/dredd/rack/rake_task.rb, line 105
def run_task(runner)
  abort dredd_not_available_message unless dredd_available?

  puts starting_message

  puts command_message(runner)

  success = runner.run
  exit_status = $?.exitstatus

  puts connection_error_message(runner) unless success if dredd_connection_error?(exit_status)

  abort unless exit_status == 0
end
starting_message() click to toggle source
# File lib/dredd/rack/rake_task.rb, line 120
        def starting_message
          <<-eos.gsub /^( |\t)+/, ""

            #{Rainbow('Verify the API conformance against its blueprint.').blue}
          eos
        end