Sha256: 48391c62d96008f41b600f3bfe165dc5eeae50bc21ca2b34f7084e01a52fe6c8
Contents?: true
Size: 1.86 KB
Versions: 1
Compression:
Stored size: 1.86 KB
Contents
require 'busker/version' require 'webrick' require 'cgi' require 'erb' module Busker class Busker def initialize(opts={}, &block) @_ = {:routes => {}} instance_eval(&block) if block_given? opts[:Port] ||= opts.delete(:port) || 8080 opts[:DocumentRoot] ||= opts.delete(:document_root) || File.expand_path('./') @_[:server] = WEBrick::HTTPServer.new(opts) @_[:server].mount_proc '' do |rq, rs| #request, response begin rs.status, rs.content_type, method = nil, 'text/html', rq.request_method.tr('-', '_').upcase route, handler = @_[:routes].find{|k,v| k.first.include?(method) && k.last.match(rq.path_info)} params = Hash[ CGI::parse(rq.query_string||'').map{|k,v| [k.to_sym,v[0]]} + #url params rq.query.map{|k,v| [k.to_sym, v]} + #query params ($~ ? $~.names.map(&:to_sym).zip($~.captures) : []) ] #dynamic route params rs.status, rs.body = route ? [rs.status || 200, handler[:block].call(params, rq, rs)] : [404, 'not found'] rescue => e @_[:server].logger.error "#{e.message}\n#{e.backtrace.map{|line| "\t#{line}"}.join("\n")}" rs.status, rs.body = 500, "#{e}" end end end def route(path, methods = ['GET'], opts={}, &block) methods = (methods.is_a?(Array) ? methods : [methods]).map{|e| e.to_s.tr('-', '_').upcase} matcher = Regexp.new("\\A#{path.gsub(/(:\w+)/){|m| "(?<#{$1[1..-1]}>\\w+)"}}\\Z") @_[:routes][[methods, path, matcher]] = {:opts => opts, :block => block} end def render(name) @_[:templates] ||= (Hash[DATA.read.split(/^@@\s*(.*\S)\s*$/)[1..-1].map(&:strip).each_slice(2).to_a] rescue {}) ERB.new(@_[:templates][name.to_s] || File.read(name)).result(binding) end def start @_[:server].start ensure @_[:server].shutdown end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
busker-0.4.1 | lib/busker.rb |