class Dredd::Rack::Runner

A Ruby wrapper around the Dredd API blueprint validation tool

Usage:

# run `dredd doc/*.apib doc/*.apib.md http://localhost:XXXX --level warning --dry-run`
# You don't need to start any server, Dredd::Rack does it for you.
dredd = Dredd::Rack::Runner.new
dredd.level(:warning).dry_run!.run

# You can specify an API endpoint to perform a remote validation.
# Do not forget to serve the API at the given URL!
#
# runs `dredd blueprints/*.md doc/*.md https://api.example.com --no-color`
anderson = Anderson::Rack::Runner.new 'https://api.example.com' do |options|
  options.paths_to_blueprints 'blueprints/*.md', 'doc/*.md'
  options.no_color!
end
anderson.run

Constants

BOOLEAN_OPTIONS
META_OPTIONS
NEGATABLE_BOOLEAN_OPTIONS
OPTIONS
SINGLE_ARGUMENT_OPTIONS

Attributes

command_parts[RW]

Store the Dredd command line options

Public Class Methods

new(api_endpoint=nil) { |self| ... } click to toggle source

Initialize a runner instance

The API endpoint can be local or remote.

api_endpoint - the API URL as a String

# File lib/dredd/rack/runner.rb, line 52
def initialize(api_endpoint=nil)
  @dredd_command = Dredd::Rack.dredd_command
  @paths_to_blueprints = 'doc/*.apib doc/*.apib.md'
  @api_endpoint = api_endpoint || ''
  @command_parts = []

  yield self if block_given?
end

Public Instance Methods

api_endpoint(api_endpoint=nil) click to toggle source

Set or return the runner API endpoint

Use with no arguments to read the runner API endpoint, provide an API endpoint to set it.

api_endpoint - String URL of the API endpoint to validate

Returns the String URL of the runner API endpoint.

# File lib/dredd/rack/runner.rb, line 69
def api_endpoint(api_endpoint=nil)
  @api_endpoint = api_endpoint unless api_endpoint.nil?
  @api_endpoint
end
command() click to toggle source

Return the Dredd command line

# File lib/dredd/rack/runner.rb, line 75
def command
  ([@dredd_command, @paths_to_blueprints, @api_endpoint] + @command_parts).join(' ')
end
configure() { |self| ... } click to toggle source

Configure the runner instance

# File lib/dredd/rack/runner.rb, line 80
def configure
  yield self if block_given?
end
paths_to_blueprints(*paths_to_blueprints) click to toggle source

Define custom paths to blueprints

paths_to_blueprints - as many Strings as paths where blueprints are located

Returns self.

# File lib/dredd/rack/runner.rb, line 89
def paths_to_blueprints(*paths_to_blueprints)
  raise ArgumentError, 'invalid path to blueprints' if paths_to_blueprints == [''] || paths_to_blueprints.empty?

  @paths_to_blueprints = paths_to_blueprints.join(' ')
  self
end
respond_to_missing?(method, include_private=false) click to toggle source

Ensure that the runner does respond_to? its option methods

See ruby-doc.org/core-2.2.0/Object.html#method-i-respond_to_missing-3F

Calls superclass method
# File lib/dredd/rack/runner.rb, line 108
def respond_to_missing?(method, include_private=false)
  OPTIONS.include?(method.to_sym ) ||
  NEGATABLE_BOOLEAN_OPTIONS.include?(method.to_s.gsub(/\Ano_/, '').to_sym) ||
  super
end
run() click to toggle source

Run Dredd

Returns true if the Dredd exit status is zero, false instead.

# File lib/dredd/rack/runner.rb, line 99
def run
  raise InvalidCommandError.new(command) unless command_valid?
  start_server! unless api_remote?
  Kernel.system(command)
end

Private Instance Methods

api_remote?() click to toggle source
# File lib/dredd/rack/runner.rb, line 116
def api_remote?
  !@api_endpoint.empty?
end
command_valid?() click to toggle source
# File lib/dredd/rack/runner.rb, line 120
def command_valid?
  if api_remote?
    command.has_at_least_two_arguments?
  else
    command.has_at_least_one_argument?
  end
end
method_missing(name, *args) click to toggle source

Private: Define as many setter methods as there are Dredd options

The behaviour of Object#method_missing is not modified unless the called method name matches one of the Dredd options.

name - Symbol for the method called args - arguments of the called method

See also: ruby-doc.org/core-2.2.0/BasicObject.html#method-i-method_missing

Calls superclass method
# File lib/dredd/rack/runner.rb, line 143
def method_missing(name, *args)
  super unless OPTIONS.include?(name.to_sym ) ||
               NEGATABLE_BOOLEAN_OPTIONS.include?(name.to_s.gsub(/\Ano_/, '').to_sym)

  option_flag = name.to_s.gsub('_', '-').gsub('!', '').prepend('--')
  command_parts = self.command_parts.push option_flag
  command_parts = self.command_parts.push args.slice(0).to_s.quote! if SINGLE_ARGUMENT_OPTIONS.include? name
  self
end
start_server!() click to toggle source
# File lib/dredd/rack/runner.rb, line 128
def start_server!
  server = Capybara::Server.new(Dredd::Rack.app)
  server.boot
  @api_endpoint = "http://#{server.host}:#{server.port.to_s}"
end