Sha256: 97625d347b8bfd09d14b43dc4b1e3806d9033635b39ae1f77c81d2d86f6910a3

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

require 'sanford-protocol'

module Sanford

  module ServiceHandler

    def self.constantize(class_name)
      names = class_name.to_s.split('::').reject{|name| name.empty? }
      klass = names.inject(Object) do |constant, name|
        constant.const_get(name)
      end
      klass == Object ? false : klass
    rescue NameError
      false
    end

    def initialize(runner)
      @sanford_runner = runner
    end

    def init
      self.run_callback 'before_init'
      self.init!
      self.run_callback 'after_init'
    end

    def init!
    end

    def run
      self.run_callback 'before_run'
      data = self.run!
      self.run_callback 'after_run'
      [ 200, data ]
    end

    def run!
      raise NotImplementedError
    end

    def inspect
      reference = '0x0%x' % (self.object_id << 1)
      "#<#{self.class}:#{reference} @request=#{self.request.inspect}>"
    end

    protected

    def before_init
    end

    def after_init
    end

    def before_run
    end

    def after_run
    end

    # Helpers

    def halt(*args)
      @sanford_runner.halt(*args)
    end

    def request
      @sanford_runner.request
    end

    def params
      self.request.params
    end

    def logger
      @sanford_runner.logger
    end

    def run_callback(callback)
      self.send(callback.to_s)
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sanford-0.4.0 lib/sanford/service_handler.rb
sanford-0.3.0 lib/sanford/service_handler.rb
sanford-0.2.0 lib/sanford/service_handler.rb