Class: RackRscript

Inherits:
Object
  • Object
show all
Includes:
AppRoutes
Defined in:
lib/rack-rscript.rb

Instance Method Summary collapse

Constructor Details

#initialize(raw_opts = {}) ⇒ RackRscript

Returns a new instance of RackRscript.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rack-rscript.rb', line 24

def initialize(raw_opts={})
  @params = {}
  @templates = {}
  
  @rrscript = RScript.new
  
  opts = {logfile: '', logrotate: 'daily', pkg_src: ''}.merge(raw_opts)
  @url_base = opts[:pkg_src] # web server serving the RSF files
  @url_base += '/' unless @url_base[-1] == '/'
  
  @log = false

  if opts[:logfile].length > 0 then
    @log = true
    @logger = Logger.new(opts[:logfile], opts[:logrotate])    
  end

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

Instance Method Details

#call(env) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rack-rscript.rb', line 45

def call(env)
  @env = env
  request = env['REQUEST_URI'][/https?:\/\/[^\/]+(.*)/,1]

  log "_: " + env.keys.inspect
  log Time.now.to_s + "_: " + env.inspect

  default_routes(env,@params)
  content, content_type, status_code = run_route(request)        

  if content.is_a? Redirect then
    redirectx = content
    res = Rack::Response.new
    res.redirect(redirectx.to_url)
    res.finish      
  else
    
    if content.nil? then
      e = $!
      log(e) if e
      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(options) {|x| content}.render, '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,
      'application/xml' => passthru_proc
    }
    content_type ||= 'text/html'
    status_code ||= 200                  
    content, content_type = ct_list[content_type].call(content, content_type)      
    
    [status_code, {"Content-Type" => content_type}, [content]]
  end
end

#clear_cacheObject



93
94
95
# File 'lib/rack-rscript.rb', line 93

def clear_cache()
  @rrscript.reset
end

#haml(name, options = {}) ⇒ Object



140
141
142
# File 'lib/rack-rscript.rb', line 140

def haml(name,options={})    
  render name, :haml, options
end

#log(msg) ⇒ Object



130
131
132
133
134
# File 'lib/rack-rscript.rb', line 130

def log(msg)
  if @log == true then
    @logger.debug msg
  end
end

#redirect(url) ⇒ Object



136
137
138
# File 'lib/rack-rscript.rb', line 136

def redirect(url)
  Redirect.new url
end

#run_job(url, jobs, params = {}, *qargs) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rack-rscript.rb', line 97

def run_job(url, jobs, params={}, *qargs)

  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
  
  result, args = @rrscript.read([url, jobs.split(/\s/), \
    qargs].flatten)

  rws = self
  
  begin
    r = eval result
    return r

  rescue Exception => e  
    @params = {}
    err_label = e.message.to_s + " :: \n" + e.backtrace.join("\n")      
    log(err_label)
  end
  
end

#slim(name, options = {}) ⇒ Object



144
145
146
# File 'lib/rack-rscript.rb', line 144

def slim(name,options={})
  render name, :slim, options
end