Class: RackRscript

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



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
85
86
87
88
89
90
91
92
# File 'lib/rack-rscript.rb', line 40

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

#reqObject (readonly)

Returns the value of attribute req.



37
38
39
# File 'lib/rack-rscript.rb', line 37

def req
  @req
end

#rscObject (readonly)

Returns the value of attribute rsc.



37
38
39
# File 'lib/rack-rscript.rb', line 37

def rsc
  @rsc
end

Instance Method Details

#call(env, testmode: false) ⇒ Object



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

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_cacheObject



127
128
129
# File 'lib/rack-rscript.rb', line 127

def clear_cache()
  @rscript.reset
end

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



219
220
221
# File 'lib/rack-rscript.rb', line 219

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

#redirect(url) ⇒ Object



210
211
212
# File 'lib/rack-rscript.rb', line 210

def redirect(url)
  Redirect.new url
end

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



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

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.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



223
224
225
# File 'lib/rack-rscript.rb', line 223

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

#transform(xsl, xml) ⇒ Object

jr 140616 not yet used and still needs to be tested



215
216
217
# File 'lib/rack-rscript.rb', line 215

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