class Stargate::Server::Engine::Sinatra

Public: Sinatra transport. Very simple implementation of a transport that speaks non-REST interface. Sorta RPC transport over HTTP implemented with Sinatra.

Public Class Methods

new(registry) click to toggle source
# File lib/stargate/server/engine/sinatra.rb, line 11
def initialize(registry)
  require 'sinatra/base'

  logger = log

  @app = ::Sinatra.new do
    helpers Helpers

    registry.each do |version_number, actual_registry|
      caller = Caller.new(actual_registry)

      get "/v#{version_number}/definitions" do
        log.debug("Serving registry definitions")
        content_type(response_codec.content_type)
        response_codec.encode(actual_registry)
      end

      post "/v#{version_number}/:klass_name.:method" do |klass_name, method|
        args = parse_args
        log.debug("Executing call", class: klass_name, method: method, args: args)
        result = caller.call(klass_name, method, *args)
        content_type(response_codec.content_type)
        response_codec.encode(result)
      end
    end

    error Stargate::Server::NotFoundError do
      error_response(404)
    end

    error Stargate::Codec::Error do
      error_response(400)
    end

    error NotImplementedError do
      error_response(501)
    end

    error 500 do
      error_response(500)
    end
  end

  @app.set(:default_mime_type, ::Stargate::Codec::JSON.content_type)
  @app.set(:show_exceptions, false)
  @app.set(:logger, log)
end

Public Instance Methods

call(env) click to toggle source
# File lib/stargate/server/engine/sinatra.rb, line 59
def call(env)
  @app.call(env)
end