Class: Dredd::Rack::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/dredd/rack/runner.rb

Overview

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

Constant Summary collapse

NEGATABLE_BOOLEAN_OPTIONS =
[:dry_run!, :sandbox!, :names!, :init!, :sorted!, :inline_errors!,
:details!, :color!, :timestamp!, :silent!]
META_OPTIONS =
[:help, :version]
BOOLEAN_OPTIONS =
NEGATABLE_BOOLEAN_OPTIONS + META_OPTIONS
SINGLE_ARGUMENT_OPTIONS =
[:hookfiles, :language, :server, :server_wait, :custom, :only,
:reporter, :output, :header, :user, :method, :level, :path,
:hooks_worker_timeout, :hooks_worker_connect_timeout,
:hooks_worker_connect_retry, :hooks_worker_after_connect_wait,
:hooks_worker_term_timeout, :hooks_worker_term_retry,
:hooks_worker_handler_host, :hooks_worker_handler_port,
:config]
OPTIONS =
BOOLEAN_OPTIONS + SINGLE_ARGUMENT_OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_endpoint = nil) {|_self| ... } ⇒ Runner

Initialize a runner instance

The API endpoint can be local or remote.

api_endpoint - the API URL as a String

Yields:

  • (_self)

Yield Parameters:



52
53
54
55
56
57
58
59
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)

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



143
144
145
146
147
148
149
150
151
# 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

Instance Attribute Details

#command_partsObject

Store the Dredd command line options



44
45
46
# File 'lib/dredd/rack/runner.rb', line 44

def command_parts
  @command_parts
end

Instance Method Details

#api_endpoint(api_endpoint = nil) ⇒ Object

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.



69
70
71
72
# 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

#commandObject

Return the Dredd command line



75
76
77
# File 'lib/dredd/rack/runner.rb', line 75

def command
  ([@dredd_command, @paths_to_blueprints, @api_endpoint] + @command_parts).join(' ')
end

#configure {|_self| ... } ⇒ Object

Configure the runner instance

Yields:

  • (_self)

Yield Parameters:



80
81
82
# File 'lib/dredd/rack/runner.rb', line 80

def configure
  yield self if block_given?
end

#paths_to_blueprints(*paths_to_blueprints) ⇒ Object

Define custom paths to blueprints

paths_to_blueprints - as many Strings as paths where blueprints are located

Returns self.

Raises:

  • (ArgumentError)


89
90
91
92
93
94
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


108
109
110
111
112
# 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

#runObject

Run Dredd

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



99
100
101
102
103
# 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