Sha256: c5dea4501eab8aa6c46a5646a7a6c6e1b8b09ec2c1484a8dc16ed5c24b67a5e1

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

require 'sanford-protocol'
require 'sanford/logger'
require 'sanford/template_source'

module Sanford

  class Runner

    ResponseArgs = Struct.new(:status, :data)

    attr_reader :handler_class, :handler
    attr_reader :request, :params, :logger, :template_source

    def initialize(handler_class)
      @handler_class = handler_class
      @handler = @handler_class.new(self)
    end

    def run
      raise NotImplementedError
    end

    # It's best to keep what `halt` and `catch_halt` return in the same format.
    # Currently this is a `ResponseArgs` object. This is so no matter how the
    # block returns (either by throwing or running normally), you get the same
    # thing kind of object.

    def halt(status, options = nil)
      options ||= {}
      message = options[:message] || options['message']
      response_status = [ status, message ]
      response_data = options[:data] || options['data']
      throw :halt, ResponseArgs.new(response_status, response_data)
    end

    private

    def catch_halt(&block)
      catch(:halt){ ResponseArgs.new(*block.call) }
    end

    def build_response(&block)
      args = catch_halt(&block)
      Sanford::Protocol::Response.new(args.status, args.data)
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sanford-0.11.1 lib/sanford/runner.rb
sanford-0.11.0 lib/sanford/runner.rb