Class: RackRscript

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

Instance Method Summary collapse

Constructor Details

#initialize(log: nil, pkg_src: '', cache: 5, rsc_host: nil, pxlinks: nil, debug: false, root: '', static: {}) ⇒ RackRscript

Returns a new instance of RackRscript.



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
76
77
78
79
80
81
82
83
84
# File 'lib/rack-rscript.rb', line 37

def initialize(log: nil, pkg_src: '', cache: 5, rsc_host: nil, 
               pxlinks: nil, debug: false, root: '', static: {})

  @log, @debug, @static = log, debug, static
  
  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
  
  @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

  @rsc = RSC.new rsc_host if rsc_host
  
  @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

end

Instance Method Details

#call(env) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rack-rscript.rb', line 86

def call(env)
   
   @env = env
   raw_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)
   
   request = if @pxlinks then
     found = @pxlinks.locate(raw_request)
     found ? found.join : raw_request
   else
     raw_request
   end
   
   run_request(request)
end

#clear_cacheObject



107
108
109
# File 'lib/rack-rscript.rb', line 107

def clear_cache()
  @rscript.reset
end

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



162
163
164
# File 'lib/rack-rscript.rb', line 162

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

#redirect(url) ⇒ Object



153
154
155
# File 'lib/rack-rscript.rb', line 153

def redirect(url)
  Redirect.new url
end

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
# File 'lib/rack-rscript.rb', line 111

def run_job(url, jobs, params={}, type=:get, *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
  @rscript.type = type
  result, args = @rscript.read([url, jobs.split(/\s/), \
    qargs].flatten)

  rws = self
  rsc = @rsc if @rsc    
  req = @req if @req
  
  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



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

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

#transform(xsl, xml) ⇒ Object

jr 140616 not yet used and still needs to be tested



158
159
160
# File 'lib/rack-rscript.rb', line 158

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