class Ferret::Browser::Delegator

Public Class Methods

new(reader, path) click to toggle source
# File lib/ferret/browser.rb, line 6
def initialize(reader, path)
  @reader, @path = reader, path
end

Public Instance Methods

run(env) click to toggle source
# File lib/ferret/browser.rb, line 10
def run(env)
  controller, action, args = :home, :index, nil
  query_string = env['QUERY_STRING']||''
  params = parse_query_string(query_string)
  req_path = env['PATH_INFO'].gsub(/\/+/, '/')
  case req_path
  when '/'
    # nothing to do
  when /^\/?([-a-zA-Z]+)\/?$/
    controller = $1
  when /^\/?([-a-zA-Z]+)\/([-a-zA-Z]+)\/?(.*)?$/
    controller = $1
    action = $2
    args = $3
  else
    controller = :error
    args = req_path
  end
  controller_vars = {
    :params => params,
    :req_path => req_path,
    :query_string => query_string,
  }
  delegate(controller, action, args, controller_vars)
end

Private Instance Methods

delegate(controller, action, args, controller_vars) click to toggle source
# File lib/ferret/browser.rb, line 38
def delegate(controller, action, args, controller_vars)
  begin
    controller = to_const(controller, 'Controller').
      new(@reader, @path, controller_vars)
    controller.send(action, args)
  rescue Exception => e
    puts e.to_s
    controller_vars[:params][:error] = e
    ErrorController.new(@reader, @path, controller_vars).index
  end
end
parse_query_string(query_string, delim = '&;') click to toggle source
# File lib/ferret/browser.rb, line 60
def parse_query_string(query_string, delim = '&;')
  m = proc {|_,o,n| o.update(n, &m) rescue ([*o] << n)}
  (query_string||'').split(/[#{delim}] */n).
    inject({}) { |hash, param| key, val = unescape_uri(param).split('=',2)
      hash.update(key.split(/[\]\[]+/).reverse.
        inject(val) { |x,i| Hash[i,x] }, &m)
    } 
end
to_const(str, suffix='') click to toggle source
# File lib/ferret/browser.rb, line 50
def to_const(str, suffix='')
  Ferret::Browser.const_get(str.to_s.split('-').
                            map {|w| w.capitalize}.join('') + suffix)
end
unescape_uri(s) click to toggle source

from _why's camping

# File lib/ferret/browser.rb, line 56
def unescape_uri(s)
  s.tr('+', ' ').gsub(/%([\da-f]{2})/in){[$1].pack('H*')}
end