Class: RackRscript
Instance Attribute Summary collapse
-
#ws ⇒ Object
readonly
Returns the value of attribute ws.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #clear_cache ⇒ Object
- #haml(name, options = {}) ⇒ Object
-
#initialize(raw_opts = {}) ⇒ RackRscript
constructor
A new instance of RackRscript.
- #log(msg) ⇒ Object
- #redirect(url) ⇒ Object
- #run_job(url, jobs, params = {}, *qargs) ⇒ Object
- #slim(name, options = {}) ⇒ Object
Constructor Details
#initialize(raw_opts = {}) ⇒ RackRscript
Returns a new instance of RackRscript.
27 28 29 30 31 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rack-rscript.rb', line 27 def initialize(raw_opts={}) @params = {} @templates = {} @rrscript = RScript.new opts = {logfile: '', logrotate: 'daily', pkg_src: '', sps_port: 59000}\ .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) if opts[:sps_address] then c = WebSocket::EventMachine::Client Thread.new do EM.run do @ws = c.connect(uri: "ws://%s:%s" % [opts[:sps_address], opts[:sps_port]]) @ws.onopen do puts "Client connected" end @ws.onclose do puts "Client disconnected" end EventMachine.error_handler{ |e| puts "Error raised during event loop: #{e.}" } end end end end |
Instance Attribute Details
#ws ⇒ Object (readonly)
Returns the value of attribute ws.
25 26 27 |
# File 'lib/rack-rscript.rb', line 25 def ws @ws end |
Instance Method Details
#call(env) ⇒ Object
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 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rack-rscript.rb', line 77 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, = s [Tilt[type].new() {|x| content}.render(self, ), '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, '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_cache ⇒ Object
134 135 136 |
# File 'lib/rack-rscript.rb', line 134 def clear_cache() @rrscript.reset end |
#haml(name, options = {}) ⇒ Object
182 183 184 |
# File 'lib/rack-rscript.rb', line 182 def haml(name,={}) render name, :haml, end |
#log(msg) ⇒ Object
172 173 174 175 176 |
# File 'lib/rack-rscript.rb', line 172 def log(msg) if @log == true then @logger.debug msg end end |
#redirect(url) ⇒ Object
178 179 180 |
# File 'lib/rack-rscript.rb', line 178 def redirect(url) Redirect.new url end |
#run_job(url, jobs, params = {}, *qargs) ⇒ Object
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 |
# File 'lib/rack-rscript.rb', line 138 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 begin r = eval result return r rescue Exception => e @params = {} err_label = e..to_s + " :: \n" + e.backtrace.join("\n") log(err_label) end end |
#slim(name, options = {}) ⇒ Object
186 187 188 |
# File 'lib/rack-rscript.rb', line 186 def slim(name,={}) render name, :slim, end |