Method: Page#respond_to_request

Defined in:
lib/yodel/models/pages/page.rb

#respond_to_request(request, response, mime_type) ⇒ Object

request handling



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/yodel/models/pages/page.rb', line 127

def respond_to_request(request, response, mime_type)
  # initialise request & response for use by the environment accessors
  @_request   = request
  @_response  = response
  @_mime_type = mime_type
  
  # determine the default action name for the request
  http_method = request.request_method.downcase
  action = "respond_to_#{http_method}_with_#{mime_type.name}"
  
  # if there is no action for the request, default to the first for the http_method of the request
  if !respond_to?(action)
    action = "default_response_to_#{http_method}"
    
    if !respond_to?(action)
      response.write "Unable to respond to a request using http method: #{http_method}"
      response['Content-Type'] = 'text/plain'
      return
    else
      default_mime = self.class.instance_variable_get("@default_response_to_#{http_method}_mime_type")
      mime_type = default_mime if mime_type != default_mime
      Yodel.config.logger.warn "No response matches this request, falling back to a default response."
    end
  end
  
  # only send a builder object as a parameter to the action if required
  if mime_type.has_builder?
    data = send(action, mime_type.create_builder)
  else
    data = send(action)
  end
  return if @finished
  
  # process the response and set headers
  response.write mime_type.process(data)
  response['Content-Type'] = "#{mime_type.default_mime_type}; charset=utf-8"
  
  # write the flash to the session if appropriate
  @flash.finalize if @flash
end