class Grass::Front

Constants

DEFAULT_PAGE

Public Instance Methods

get(env) click to toggle source
# File lib/grass/endpoints/front.rb, line 51
def get(env)
  
  set_locale

  id = get_id
  
  data = id =~ /scripts|styles/ ? {} : request_data
  
  headers = {}
  
  return fresh(id,data) if Grass.env == "development" && !config['enable_cache_for_development']

  # try memcache or render freshly
  if cached_response = Source.read_cache(Source.generate_cachekey(id,data))
    # puts "----> CACHED!!!"

    mime_type, body = cached_response
    headers = {"Content-Type" => mime_type}     
    status = 200   
      
  else
    status, headers, body = fresh(id,data)
    
  end
  
  [status,headers,body]

end
response(env) click to toggle source
# File lib/grass/endpoints/front.rb, line 47
def response(env)
  self.public_send env['REQUEST_METHOD'].downcase, env
end

Private Instance Methods

fresh(id, data = {}) click to toggle source
# File lib/grass/endpoints/front.rb, line 111
def fresh id, data = {}
  file = get_file(id)
  if file.type == "page"   
    file.render(data) 
    file.cache!
  end
  [200, {"Content-Type" => file.mime_type} ,file.read]
end
get_file(key) click to toggle source
# File lib/grass/endpoints/front.rb, line 102
def get_file key
  raise Goliath::Validation::NotFoundError unless source = Source[key].first
  # if Grass.env == "development"
  #   source.file.read
  #   source.commit!
  # end
  source
end
get_id() click to toggle source
# File lib/grass/endpoints/front.rb, line 82
def get_id
  # set default home
  id = env["REQUEST_PATH"] == "/" ? "/pages/#{DEFAULT_PAGE}" : env["REQUEST_PATH"]

  # remove trailing slash
  id = id[0..-2] if id.end_with?("/")

  # ensure locale
  unless id =~ /#{Key::KEY_REGEX[:locale]}/      
    id = "/#{I18n.locale}/#{id}"
  end

  # add pages as default collection
  unless id =~ /#{Key::KEY_REGEX[:dir]}/      
    id = id.split("/").insert(2,"pages").join("/") 
  end

  id.gsub("//","/")
end