Class: RackRscript

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

Instance Method Summary collapse

Constructor Details

#initialize(log: nil, 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
# File 'lib/rack-rscript.rb', line 32

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



  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



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
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
# File 'lib/rack-rscript.rb', line 55

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

    @log.info 'RackRscript/call: ' + env.inspect if @log
    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.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)      
      
      [status_code, {"Content-Type" => content_type}, [content]]
    end
end

#clear_cacheObject



122
123
124
# File 'lib/rack-rscript.rb', line 122

def clear_cache()
  @rrscript.reset
end

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



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

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

#redirect(url) ⇒ Object



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

def redirect(url)
  Redirect.new url
end

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



126
127
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 126

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.debug 'RackRscript/run_job/error: ' + err_label if @log
  end
  
end

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



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

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

#transform(xsl, xml) ⇒ Object

jr 140616 not yet used and still needs to be tested



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

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