Class: Committee::Bin::CommitteeStub

Inherits:
Object
  • Object
show all
Defined in:
lib/committee/bin/committee_stub.rb

Overview

CommitteeStub internalizes the functionality of bin/committee-stub so that we can test code that would otherwise be difficult to get at in an executable.

Instance Method Summary collapse

Instance Method Details

#get_app(schema, options) ⇒ Object

Gets a Rack app suitable for use as a stub.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/committee/bin/committee_stub.rb', line 8

def get_app(schema, options)
  cache = {}

  Rack::Builder.new {
    unless options[:tolerant]
      use Committee::Middleware::RequestValidation, schema: schema
      use Committee::Middleware::ResponseValidation, schema: schema
    end

    use Committee::Middleware::Stub, cache: cache, schema: schema

    run lambda { |_|
      [404, {}, ["Not found"]]
    }
  }
end

#get_options_parserObject

Gets an option parser for command line arguments.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/committee/bin/committee_stub.rb', line 26

def get_options_parser
  options = {
    :driver   => nil,
    :help     => false,
    :port     => 9292,
    :tolerant => false,
  }

  parser = OptionParser.new do |opts|
    opts.banner = "Usage: rackup [options] [JSON Schema file]"

    opts.separator ""
    opts.separator "Options:"

    # required
    opts.on("-d", "--driver NAME", "name of driver [open_api_2|hyper_schema]") { |name|
      options[:driver] = name.to_sym
    }

    opts.on_tail("-h", "-?", "--help", "Show this message") {
      options[:help] = true
    }

    opts.on("-t", "--tolerant", "don't perform request/response validations") {
      options[:tolerant] = true
    }

    opts.on("-p", "--port PORT", "use PORT (default: 9292)") { |port|
      options[:port] = port
    }
  end
  [options, parser]
end