Class: HTTPController

Inherits:
Object
  • Object
show all
Defined in:
lib/appswarm/http/http_controller.rb

Direct Known Subclasses

TemplateController

Defined Under Namespace

Classes: DummyResult, UnknownAction, WebRequest

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, instanceName) ⇒ HTTPController

Returns a new instance of HTTPController.



62
63
64
65
66
67
68
69
70
# File 'lib/appswarm/http/http_controller.rb', line 62

def initialize(app,instanceName)
  @app=app
  @b=binding
  @view=true
  @instanceName=instanceName
  @params={}

  @basePaths=[@app.getAppDir,File.expand_path('..',__FILE__)]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *s) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/appswarm/http/http_controller.rb', line 167

def method_missing(name,*s)
  if s.length==0 and instance_variable_defined?("@"+name.to_s)
    instance_variable_get("@"+name.to_s)
  else
    super
  end
end

Instance Attribute Details

#contentTypeObject

Returns the value of attribute contentType.



33
34
35
# File 'lib/appswarm/http/http_controller.rb', line 33

def contentType
  @contentType
end

#paramsObject

Returns the value of attribute params.



34
35
36
# File 'lib/appswarm/http/http_controller.rb', line 34

def params
  @params
end

#sessionIdObject

Returns the value of attribute sessionId.



34
35
36
# File 'lib/appswarm/http/http_controller.rb', line 34

def sessionId
  @sessionId
end

Class Method Details

.controllerNameObject



210
211
212
# File 'lib/appswarm/http/http_controller.rb', line 210

def self.controllerName
  self.to_s.downcase.gsub(/.*:/,"")
end

.layout(layoutName) ⇒ Object



50
51
52
53
54
# File 'lib/appswarm/http/http_controller.rb', line 50

def self.layout(layoutName)
  @@layoutName||={}
  @@layoutName[self]=layoutName
  #log "SETTING LAYOUT: #{self}"
end

Instance Method Details

#controllerNameObject



214
215
216
# File 'lib/appswarm/http/http_controller.rb', line 214

def controllerName
  name.gsub(/.*:/,"")
end

#count(name, to = nil) ⇒ Object



229
230
231
232
233
234
235
236
237
# File 'lib/appswarm/http/http_controller.rb', line 229

def count(name,to=nil)
  @counters||={}
  @counters[name]||=0
  @counters[name]+=1
  if @counters[name]>=to
    @counters[name]=0
  end
  @counters[name]
end

#css(file) ⇒ Object



239
240
241
242
# File 'lib/appswarm/http/http_controller.rb', line 239

def css(file)
  file+=".css" unless file=~/\.css$/
  "<link rel=\"stylesheet\" href=\"#{url(:action=>file)}\" type=\"text/css\" media=\"screen\" />"
end

#findFile(fileName) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/appswarm/http/http_controller.rb', line 80

def findFile(fileName)
  @basePaths.each{|p|
    f=File.join(p,fileName)
    return f if File.exists?(f)
  }
  return nil
end

#layoutNameObject

log “SETTING LAYOUT: #self”



55
56
57
58
# File 'lib/appswarm/http/http_controller.rb', line 55

def layoutName
  @@layoutName||={}
  @@layoutName[self.class]
end

#log(*args) ⇒ Object



72
73
74
# File 'lib/appswarm/http/http_controller.rb', line 72

def log(*args)
  @app.log(*args)
end

#nameObject



217
218
219
# File 'lib/appswarm/http/http_controller.rb', line 217

def name
  self.class.to_s.downcase    
end

#redirect_to(purl) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/appswarm/http/http_controller.rb', line 179

def redirect_to(purl)
  purl=url(purl) if purl.is_a?(Hash)
  @res.header["Location"]=purl
  @res["Content-Type"]="text/html"
  @res.body="<HTML><A HREF=\"#{purl.to_s}\">#{purl.to_s}</A>.</HTML>\n"
  @res.status=302
  @view=false
end

#render_html(html) ⇒ Object



174
175
176
177
178
# File 'lib/appswarm/http/http_controller.rb', line 174

def render_html(html)
  @res.body=html
  @res["Content-Type"]="text/html"
  @view=false    
end

#render_partial(name, args = {}) ⇒ Object

FIXME args={} ???



221
222
223
224
225
226
227
# File 'lib/appswarm/http/http_controller.rb', line 221

def render_partial(name,args={}) # FIXME args={} ???
  res=DummyResult.new
  #log name
  run(@instance,res,"_"+name.to_s,args,[],@currentUrl)

  return res.body
end

#run(instance, res, action, args, path_rest, currentUrl) ⇒ Object



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
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
# File 'lib/appswarm/http/http_controller.rb', line 93

def run(instance,res,action,args,path_rest,currentUrl)
  log "controller-run"
  log "ACTION ",action
  @res=res
  @instance=instance
  @currentUrl=currentUrl
  @params=args

  path=path_rest

  action||="index"
  log "ACTION #{action} #{args}"
  if respond_to?(action)
    webRequest=WebRequest.new(path,args)
    m=method(action)
    if m
      if m.arity>0
        send(action,webRequest)
      else
        send(action)
      end
    end
  else
    log "#{self} DOES not respond to #{action}"
    res.status=404
  end
  log "VIEW:#{@view}"
  if @view
    begin
      #action=@mrhtml if @mrhtml
      mrhtml=@mrhtml||action
      #mrhtml=action
      cntrlName=controllerName
      path=File.join("views",cntrlName,"#{mrhtml}.rhtml")
      if cntrlName==@app.getAppName.underscored
        path=File.join("views","#{mrhtml}.rhtml")
      end
      filename=findFile(path)
      log "FILE #{filename} #{path}"
      @mrhtml=nil
      if filename
        c=File.read(filename)
        res.body=ERB.new(c).result binding
        res["Content-Type"]="text/html"
        #log "RES:",res

        log "LAYOUT #{self.class} #{layoutName}"

        if layoutName and not action=~/_.*/
          layout=layoutName.to_s
          #filename="apps/#{app.shortName.downcase}/layouts/#{layout}.rhtml"
          filename=findFile(File.join("layouts","#{layout}.rhtml"))
          if File.file?(filename)
            c=File.read(filename)
            content=res.body
            res.body=ERB.new(c).result binding
            res["Content-Type"]="text/html"
          end
        end
        log "OK"

        # FIXME: fix content-size !!!

      else
        raise UnknownAction.new(action)
      end


    rescue Exception=>e
      raise e if e.is_a?(UnknownAction)
      raise UnknownAction.new(e)
    end
  end
end

#sessionObject



76
77
78
# File 'lib/appswarm/http/http_controller.rb', line 76

def session
  @app.getApp(:sessionManager).get(@app,sessionId)
end

#url(a) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/appswarm/http/http_controller.rb', line 187

def url(a)

  ps=[]
  a.each{|n,v|
    if n!=:action
      ps << "#{n}=#{v}"
    end
  }
  args=ps.join("&")
  args="?#{args}" if args.length>0

  u=@currentUrl.gsub(/\/$/,'')

  if urlPart!=@app.mainController.urlPart
    u+="/#{urlPart}"
  end
  u+"/#{a[:action]}"+args
end

#urlPartObject



206
207
208
# File 'lib/appswarm/http/http_controller.rb', line 206

def urlPart
  self.class.to_s.gsub(/.*:/,'').underscored
end

#viewInstead(rhtml) ⇒ Object



88
89
90
# File 'lib/appswarm/http/http_controller.rb', line 88

def viewInstead(rhtml)
  @mrhtml=rhtml
end