class RackRscript

Attributes

req[R]
rsc[R]

Public Class Methods

new(log: nil, pkg_src: '', cache: 5, rsc_host: nil, rsc: {host: rsc_host, port: '61000'}, pxlinks: nil, debug: false, root: '', static: {}) click to toggle source
Calls superclass method
# File lib/rack-rscript.rb, line 42
def initialize(log: nil, pkg_src: '', cache: 5, rsc_host: nil,
               rsc: {host: rsc_host, port: '61000'},
               pxlinks: nil, debug: false, root: '', static: {})

  @log, @debug, @static = log, debug, static

  puts 'root: ' + root.inspect if @debug
  puts 'pkg_Src: ' + pkg_src.inspect if @debug

  @params = {}

  @templates = {}

  @log.debug 'pkg_src: ' + pkg_src.inspect if @log
  @rscript = RScriptRW.new log: log, pkg_src: pkg_src, cache: cache, debug: true
  @render = NThrut.new(self)

  @url_base = pkg_src # web server serving the RSF files
  @url_base += '/' unless @url_base[-1] == '/'

  if pxlinks then

    src, _ = RXFReader.read(pxlinks)

    if src =~ /^<\?/ then

      @pxlinks = PolyrexLinks.new src

    else

      @pxlinks = PolyrexLinks.new
      @pxlinks.parse pxlinks

    end

  end

  super() # required for app-routes initialize method to exectue
  default_routes(@env, @params || {})

  @rsc = nil

  if rsc[:host] and rsc[:host].length > 0 then
    @rsc = RSC.new(rsc[:host], port: rsc[:port])
  end

  @filetype = {xml: 'application/xml', html: 'text/html', png: 'image/png',
           jpg: 'image/jpeg', txt: 'text/plain', css: 'text/css',
           xsl: 'application/xml', svg: 'image/svg+xml'}

  @root, @static = root, static
  @initialized = {}

end

Public Instance Methods

call(env, testmode: false) click to toggle source
# File lib/rack-rscript.rb, line 97
  def call(env, testmode: false)


    @env = env
    raw_request = env['REQUEST_URI'][/\/\/[^\/]+(.*)/,1]
    #raw_request = env['REQUEST_PATH']

    @log.info 'RackRscript/call: ' + env.inspect if @log

    if testmode == false then

      @req = Rack::Request.new(env)
      @req_params = @req.params

    end

    default_routes(env,@params)

    request = if @pxlinks then
      found = @pxlinks.locate(raw_request)
      found ? found.join : raw_request
    else
      raw_request
    end

    @log.info 'RackRscript/call/request: ' + request.inspect if @log
#=begin
    puts 'request: ' + request.inspect if @debug
    run_request(request)
#=end
#    [200, {"Content-Type" => "text/plain"}, [env.inspect]]
        end
clear_cache() click to toggle source
# File lib/rack-rscript.rb, line 130
def clear_cache()
  @rscript.reset
end
haml(name,options={}) click to toggle source
# File lib/rack-rscript.rb, line 221
def haml(name,options={})
  @render.haml name, options
end
redirect(url) click to toggle source
# File lib/rack-rscript.rb, line 212
def redirect(url)
  Redirect.new url
end
run_job(url, job, params={}, type=:get, *qargs) click to toggle source
# File lib/rack-rscript.rb, line 134
def run_job(url, job, params={}, type=:get, *qargs)

  puts 'inside run_job' if @debug
  @log.debug 'RackRscript/run_job/params: ' + params.inspect if @log
  @log.debug 'RackRscript/run_job/qargs: ' + qargs.inspect if @log

  if @params[:splat] then
    @params.each do  |k,v|
      @params.delete k unless k == :splat or k == :package \
          or k == :job or k == :captures
    end
  end

  if @params[:splat] and @params[:splat].length > 0 then
    h = @params[:splat].first[1..-1].split('&').inject({}) do |r,x|
      k, v = x.split('=')
      v ? r.merge(k[/\w+$/].to_sym => Rack::Utils.unescape(v)) : r
    end
    @params.merge! h
  end

  @params.merge! @req.params if @req
  @rscript.type = type

  if not @rscript.jobs(url).include?(job.to_sym) then

    if @rscript.jobs(url).include?(:job_missing) then
      job = 'job_missing'
    else
      [404, 'job not found']
    end

  end

  result, args = @rscript.read([url, '//job:' + job, \
    qargs].flatten)

  rws = self
  rsc = @rsc if @rsc
  req = @req if @req

  begin

    if @debug then
      puts @rscript.jobs(url).inspect
      puts 'job: ' + job.inspect
      puts 'url: ' + url.inspect
      puts '@initialized: ' + @initialized.inspect
      bflag = @rscript.jobs(url).include?(:initialize) and \
          !@initialized[url] and job != 'initialize'
      puts 'bflag: ' + bflag.inspect
    end

    if @rscript.jobs(url).include?(:initialize) and
        !@initialized[url]  and job != 'initialize' then
      puts 'before run initialize' if @debug
      r2 = @rscript.read([url, '//job:initialize'])
      puts 'r2: ' + r2.inspect if @debug
      eval r2.join
      @initialized[url] = true
    end

    r = eval result

    return r

  rescue Exception => e

    @params = {}
    err_label = e.message.to_s + " :: \n" + e.backtrace.join("\n")
    raise RackRscriptError, err_label

    @log.debug 'RackRscript/run_job/error: ' + err_label if @log
  end

end
slim(name,options={}) click to toggle source
# File lib/rack-rscript.rb, line 225
def slim(name,options={})
  @render.slim name, options
end
transform(xsl, xml) click to toggle source

jr 140616 not yet used and still needs to be tested

# File lib/rack-rscript.rb, line 217
def transform(xsl, xml)
  Rexslt.new(xsl, xml).to_s
end

Protected Instance Methods

default_routes(env, params) click to toggle source
# File lib/rack-rscript.rb, line 231
def default_routes(env, params)

  @log.info 'RackRscript/default_routes: ' + params.inspect if @log

  get '/do/:package/:job' do |package,job|
    @log.info 'RackRscript/route/do/package/job: ' + [package, job].inspect if @log
    run_job("%s%s.rsf" % [@url_base, package], job, params)
  end

  get '/:dir/do/:package/:job' do |dir, package, job|
    run_job(("%s%s/%s.rsf" % [@url_base, dir, package]), job, params)
  end

  get '/:dir/do/:package/:job/*' do |dir, package, job|
    raw_args = params[:splat]
    args = raw_args.first[/[^\s\?]+/].to_s.split('/')[1..-1]
    run_job(("%s%s/%s.rsf" % [@url_base, dir, package]), job, params, :get, args)
  end

  post '/do/:package/:job' do |package,job|
    run_job("%s%s.rsf" % [@url_base, package], job, params, :post)
  end

  post '/:dir/do/:package/:job' do |dir, package,job|
    run_job(("%s%s/%s.rsf" % [@url_base, dir, package]), job, params, :post)
  end

  get '/do/:package/:job/*' do |package, job|
    raw_args = params[:splat]
    args = raw_args.first[/[^\s\?]+/].to_s.split('/')[1..-1]
    run_job("%s%s.rsf" % [@url_base, package], job, params, :get, args)
  end

  post '/do/:package/:job/*' do |package, job|
    raw_args = params[:splat]
    args = raw_args.first[/[^\s\?]+/].to_s.split('/')[1..-1]
    run_job("%s%s.rsf" % [@url_base, package], job, params, :post, args)
  end

  post '/:dir/do/:package/:job/*' do |package, job|
    raw_args = params[:splat]
    args = raw_args.first[/[^\s\?]+/].to_s.split('/')[1..-1]
    run_job("%s%s/%s.rsf" % [@url_base, dir, package], job, params, :post, args)
  end

  get '/source/:package/:job' do |package,job|
    url = "%s%s.rsf" % [@url_base, package]
    [@rscript.read([url, job]).first, 'text/plain']
  end

  get '/source/:package' do |package,job|

    url = "%s%s.rsf" % [@url_base, package]

    begin

      [RXFReader.read(url).first,'text/plain']

    rescue

      ['url: ' + url + '; ' + ($!).inspect + \
        'couldn\'t find that package', 'text/plain']
    end

  end

  get '/ls' do

    FileX.exists? @url_base
    filepath = @url_base

    [DirX.glob(filepath + '/*.rsf').map{|x| x[/([^\/]+)\.rsf$/,1]}.to_json,\
                                                          'application/json']

  end

  if @static.any? then

    get /^(\/(?:#{@static.keys.join('|')}).*)/ do |raw_path|

      _, file, tailpath = raw_path.split('/',3)

      filepath = if @static[file].empty? then

        path = raw_path
        puts 'path: ' + path.inspect if @debug
        filepath = File.join(@root, path )

      else

        File.join(@static[file], tailpath)

      end

      @log.debug 'RackRscript/default_routes/filepath: ' + filepath.inspect if @log


      if @log then
        @log.info 'DandelionS1/default_routes: ' +
            "root: %s path: %s" % [@root, path]
      end

      if filepath.length < 1 or filepath[-1] == '/' then
        filepath += 'index.html'
        FileX.read filepath
      elsif FileX.directory? filepath then
        Redirect.new (filepath + '/')
      elsif FileX.exists? filepath then

        content_type = @filetype[filepath[/\w+$/].to_sym]
        [FileX.read(filepath), content_type || 'text/plain']
      else
        'oops, file ' + filepath + ' not found'
      end

    end

  end

  get /^\/$/ do

    if @root.length > 0 then
      file = File.join(@root, 'index.html')
      FileX.read file
    else
      Time.now.inspect
    end

  end

end
jumpto(request)
Alias for: run_request
run_request(request) click to toggle source
# File lib/rack-rscript.rb, line 363
def run_request(request)

  #@log.debug 'inside run_request: ' + request.inspect if @log
  #@log.debug 'inside run_request @env: ' + @env.inspect if @log
  method_type = @env ? @env['REQUEST_METHOD'] : 'GET'
  content, content_type, status_code = run_route(request, method_type)
  @log.info 'RackRscript/run_request/content: ' + content.inspect if @log
  #puts 'content: ' + content.inspect if @debug

  #@log.debug 'inside run_request' if @log
  if content.is_a? Redirect then

    redirectx = content
    res = Rack::Response.new
    res.redirect(redirectx.to_url)
    res.finish

  else

    e = $!

    if e then
      content, status_code = e, 500
      @log.debug 'RackRscript/call/error: ' + e if @log
    elsif content.nil? then
      content, status_code  = "404: page not found", 404
    end

    tilt_proc = lambda do |s, content_type|
      type = content_type[/[^\/]+$/]
      s = [s,{}] unless s.is_a? Array
      content, options = s
      [Tilt[type].new() {|x| content}.render(self, options), 'text/html']
    end

    passthru_proc = lambda{|c, ct| [c,ct]}

    ct_list = {
      'text/html' => passthru_proc,
      'text/haml' => tilt_proc,
      'text/slim' => tilt_proc,
      'text/plain' => passthru_proc,
      'text/xml' => passthru_proc,
      'text/css' => passthru_proc,
      'text/markdown' => passthru_proc,
      'application/xml' => passthru_proc,
      'application/xhtml' => passthru_proc,
      'application/json' => passthru_proc,
      'image/png' => passthru_proc,
      'image/jpeg' => passthru_proc,
      'image/svg+xml' => passthru_proc
    }

    content_type ||= 'text/html'
    status_code ||= 200
    proc = ct_list[content_type]
    proc ||= passthru_proc
    content, content_type = proc.call(content, content_type)

    if content_type.is_a? Integer then

      status_code = content_type.to_s
      httpcodefile = File.join(@root, status_code + '.html')
      if FileX.exists? httpcodefile then
        content = FileX.read(httpcodefile)
      else
        content_type = 'text/plain'
      end
    end

    [status_code, {"Content-Type" => content_type}, [content]]
  end

end
Also aliased as: jumpto

Private Instance Methods

template(name, type=nil, &blk) click to toggle source
# File lib/rack-rscript.rb, line 442
def template(name, type=nil, &blk)
  @render.template name, type, &blk
end