Class: Barrister::SinatraContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/barrister-sinatra.rb

Instance Method Summary collapse

Constructor Details

#initialize(json_path, mount_path, handlers, port = 4567, host = 'localhost') ⇒ SinatraContainer

Returns a new instance of SinatraContainer.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/barrister-sinatra.rb', line 9

def initialize(json_path, mount_path, handlers, port=4567, host='localhost')
  @my_app = ::Sinatra.new do
    set :bind, host
    set :port, port

    contract = Barrister::contract_from_file json_path
    server   = Barrister::Server.new(contract)

    # register each provided handler
    handlers.each do |handler_klass|
      server.add_handler handler_klass.to_s, handler_klass.new
    end

    post mount_path do
      request.body.rewind
      resp = server.handle_json(request.body.read)

      status 200
      headers 'Content-Type' => 'application/json'
      resp
    end
  end
end

Instance Method Details

#startObject



33
34
35
# File 'lib/barrister-sinatra.rb', line 33

def start
  @my_app.run!
end