Class: RackRscript
- Inherits:
-
Object
- Object
- RackRscript
- Includes:
- AppRoutes, RXFHelperModule
- Defined in:
- lib/rack-rscript.rb
Instance Attribute Summary collapse
-
#req ⇒ Object
readonly
Returns the value of attribute req.
-
#rsc ⇒ Object
readonly
Returns the value of attribute rsc.
Instance Method Summary collapse
- #call(env, testmode: false) ⇒ Object
- #clear_cache ⇒ Object
- #haml(name, options = {}) ⇒ Object
-
#initialize(log: nil, pkg_src: '', cache: 5, rsc_host: nil, rsc: {host: rsc_host, port: '61000'}, pxlinks: nil, debug: false, root: '', static: {}) ⇒ RackRscript
constructor
A new instance of RackRscript.
- #redirect(url) ⇒ Object
- #run_job(url, job, params = {}, type = :get, *qargs) ⇒ Object
- #slim(name, options = {}) ⇒ Object
-
#transform(xsl, xml) ⇒ Object
jr 140616 not yet used and still needs to be tested.
Constructor Details
#initialize(log: nil, pkg_src: '', cache: 5, rsc_host: nil, rsc: {host: rsc_host, port: '61000'}, pxlinks: nil, debug: false, root: '', static: {}) ⇒ RackRscript
Returns a new instance of RackRscript.
41 42 43 44 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 92 93 |
# File 'lib/rack-rscript.rb', line 41 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 #=begin puts '@app_root: ' + @app_root.inspect if @debug puts 'root: ' + root.inspect if @debug @params = {} @templates = {} @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, _ = RXFHelper.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 end |
Instance Attribute Details
#req ⇒ Object (readonly)
Returns the value of attribute req.
38 39 40 |
# File 'lib/rack-rscript.rb', line 38 def req @req end |
#rsc ⇒ Object (readonly)
Returns the value of attribute rsc.
38 39 40 |
# File 'lib/rack-rscript.rb', line 38 def rsc @rsc end |
Instance Method Details
#call(env, testmode: false) ⇒ Object
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 123 124 125 126 |
# File 'lib/rack-rscript.rb', line 95 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 ⇒ Object
128 129 130 |
# File 'lib/rack-rscript.rb', line 128 def clear_cache() @rscript.reset end |
#haml(name, options = {}) ⇒ Object
220 221 222 |
# File 'lib/rack-rscript.rb', line 220 def haml(name,={}) @render.haml name, end |
#redirect(url) ⇒ Object
211 212 213 |
# File 'lib/rack-rscript.rb', line 211 def redirect(url) Redirect.new url end |
#run_job(url, job, params = {}, type = :get, *qargs) ⇒ Object
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/rack-rscript.rb', line 132 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..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
224 225 226 |
# File 'lib/rack-rscript.rb', line 224 def slim(name,={}) @render.slim name, end |
#transform(xsl, xml) ⇒ Object
jr 140616 not yet used and still needs to be tested
216 217 218 |
# File 'lib/rack-rscript.rb', line 216 def transform(xsl, xml) Rexslt.new(xsl, xml).to_s end |