Sha256: 9ec9e160efcc2ee5fba5bf09d4e92a171910412185157779e119db07f92098df

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

module Angelo

  class Responder
    include Celluloid::Logger

    class << self

      def symhash
        Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
      end

    end

    attr_writer :connection
    attr_reader :request

    def initialize &block
      @response_handler = Base.compile! :request_handler, &block
    end

    def base= base
      @base = base
      @base.responder = self
    end

    def request= request
      @params = nil
      @request = request
      handle_request
      respond
    end

    def handle_request
      begin
        if @response_handler
          @base.before
          @body = @response_handler.bind(@base).call || ''
          @base.after
        else
          raise NotImplementedError
        end
      rescue => e
        error_message = case
                        when respond_with?(:json)
                          { error: e.message }.to_json
                        else
                          e.message
                        end
        @connection.respond :internal_server_error, headers, error_message
        @connection.close
        error "#{e.class} - #{e.message}"
        ::STDERR.puts e.backtrace
      end
    end

    def headers hs = nil
      @headers ||= {}
      @headers.merge! hs if hs
      @headers
    end

    def content_type= type
      case type
      when :json
        headers CONTENT_TYPE_HEADER_KEY => JSON_TYPE
      else
        raise ArgumentError.new "invalid content_type: #{type}"
      end
    end

    def respond_with? type
      case headers[CONTENT_TYPE_HEADER_KEY]
      when JSON_TYPE
        type == :json
      else
        type == :html
      end
    end

    def respond
      @body = @body.to_json if respond_with? :json
      @connection.respond :ok, DEFAULT_RESPONSE_HEADERS.merge(headers), @body
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
angelo-0.0.4 lib/angelo/responder.rb