Class: RackRscript

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

Instance Method Summary collapse

Constructor Details

#initialize(logfile: '', logrotate: 'daily', pkg_src: '', cache: 5, rsc_host: 'rse', rsc_package_src: nil) ⇒ RackRscript

Returns a new instance of RackRscript.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rack-rscript.rb', line 32

def initialize(logfile: '', logrotate: 'daily', pkg_src: '', cache: 5, 
               rsc_host: 'rse', rsc_package_src: nil)
  
  @params = {}
  @templates = {}
  
  @rrscript = RScript.new logfile: logfile, logrotate: logrotate, \
                          pkg_src: pkg_src, cache: cache
  
  @url_base = pkg_src # web server serving the RSF files
  @url_base += '/' unless @url_base[-1] == '/'
  
  @log = false

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

  super() # required for app-routes initialize method to exectue
  default_routes(@env, @params)    
  
  @rsc = nil
  @rsc = RSC.new rsc_host, rsc_package_src if rsc_package_src

end

Instance Method Details

#call(env) ⇒ Object



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
92
93
94
95
96
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
# File 'lib/rack-rscript.rb', line 59

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

    log "_: " + env.keys.inspect
    log Time.now.to_s + "_: " + env.inspect
    req = Rack::Request.new(env)

    @req_params = req.params
    default_routes(env,@params)
    content, content_type, status_code = run_route(request, env['REQUEST_METHOD'])

    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(e)      
      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,
'application/xml' => passthru_proc,
'application/json' => passthru_proc,
'image/png' => passthru_proc,
'image/jpeg' => 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)      
      
      [status_code, {"Content-Type" => content_type}, [content]]
    end
end

#clear_cacheObject



124
125
126
# File 'lib/rack-rscript.rb', line 124

def clear_cache()
  @rrscript.reset
end

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



179
180
181
# File 'lib/rack-rscript.rb', line 179

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

#log(msg) ⇒ Object



164
165
166
167
168
# File 'lib/rack-rscript.rb', line 164

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

#redirect(url) ⇒ Object



170
171
172
# File 'lib/rack-rscript.rb', line 170

def redirect(url)
  Redirect.new url
end

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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rack-rscript.rb', line 128

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

  rws = self
  rsc = @rsc if @rsc
  
  begin
    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(err_label)
  end
  
end

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



183
184
185
# File 'lib/rack-rscript.rb', line 183

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

#transform(xsl, xml) ⇒ Object

jr 140616 not yet used and still needs to be tested



175
176
177
# File 'lib/rack-rscript.rb', line 175

def transform(xsl, xml)
  Rexslt.new(xsl, xml).to_s
end