Sha256: bb28e4bba60fc8f3c6693c918cba79808630e86fccd55a2cc3e45620b4044d34

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'orange-core/middleware/base'
module Orange::Middleware
  
  class Rerouter < Base
    def init(*args)
      orange.add_pulp Orange::Pulp::Packet_Reroute
    end
    
    def packet_call(packet)
      begin
        pass packet
      rescue Orange::Reroute
        packet.finish
      end
    end
  end
end

module Orange
  
  module Pulp::Packet_Reroute
    def reroute(url, type = :real, *args)
      packet['reroute.to'] = url
      packet['reroute.type'] = type
      packet['reroute.args'] = *args if args
      raise Reroute.new(self), 'Unhandled reroute'
    end
  end
  
  class Reroute < Exception
    def initialize(packet)
      @packet = packet
      @packet[:headers] = {"Content-Type" => 'text/html', "Location" => self.url}
      @packet[:status] = 302
    end
    
    def url
      case packet['reroute.type']
      when :real
        packet['reroute.to']
      # Parsing for orange urls or something
      when :orange
        "http://"+packet.env['HTTP_HOST']+packet.route_to(packet['reroute.to'], *packet['reroute.args', []])
      else
        packet['reroute.to']
      end
    end
    
    def packet
      @packet
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
orange-core-0.7.1 lib/orange-core/middleware/rerouter.rb