Sha256: aa7f82a8b86e5b365bfa03f55aafdf91bf9ca2f776f8c6f5f95adac85972c5ac

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require 'rack'
require 'json'
require 'mustermann'

module EsReadModel

  class Api

    def initialize(routes)
      @routes = routes
    end

    def call(env)
      @request = Rack::Request.new(env)
      path = @request.path_info
      @routes.each do |route, handler|
        args = Mustermann.new(route).params(path)
        return invoke_handler(handler, args, env) if args
      end
      return json_response(404, {error: 'path did not match any route'})
    end

    private

    def invoke_handler(handler, args, env)
      return json_response(503, {status: env['readmodel.status']}) unless env['readmodel.available'] == true
      begin
        result = handler.call(@request.env['readmodel.state'], @request.params.merge(args))
        return result ? json_response(200, result) : json_response(404, {error: 'not found in read model'})
      rescue Exception => ex
        return json_response(500, {
          error: "#{ex.class.name}: #{ex.message}",
          backtrace: ex.backtrace
        })
      end
    end

    def json_response(status_code, body)
      result = body.merge({
        _links: { self: @request.fullpath }
      })
      [
        status_code,
        {
          'Content-Type' => 'application/json'
        },
        [result.to_json]
      ]
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
es-readmodel-0.0.5 lib/es_readmodel/api.rb