95 lines
2.2 KiB
Ruby
95 lines
2.2 KiB
Ruby
require 'digest'
|
|
require 'vkontakte'
|
|
|
|
class ApplicationController < ActionController::Base
|
|
protect_from_forgery
|
|
before_filter :set_locale
|
|
|
|
def cache_for time
|
|
response.headers['Cache-Control'] = 'public, max-age=' + time.seconds.to_s
|
|
end
|
|
|
|
def compile_page params
|
|
compiler = lambda do |params|
|
|
@data = {}
|
|
unless params[:data].nil?
|
|
if params[:data].is_a?(Proc)
|
|
@data = params[:data].call
|
|
elsif params[:data].is_a?(Hash)
|
|
@data = params[:data]
|
|
else
|
|
@data = params[:data].serialize
|
|
end
|
|
end
|
|
|
|
@status = params[:status]
|
|
{
|
|
renderer: "unified",
|
|
data: @data,
|
|
html: render_compact_partial(params[:partial]),
|
|
title: params[:title],
|
|
status: (params[:status] unless params[:status].nil?),
|
|
callback: (params[:callback] unless params[:callback].nil?)
|
|
}.to_json.to_s
|
|
end
|
|
|
|
unless params[:cache_for].nil?
|
|
data = Rails.cache.fetch(params[:cache_key] || cache_key_for(params[:data]), expires_in: params[:cache_for]) do
|
|
compiler.call(params)
|
|
end
|
|
else
|
|
data = compiler.call(params)
|
|
end
|
|
|
|
render text: data, content_type: 'application/json'
|
|
end
|
|
|
|
def get_artist_name_from_query
|
|
params[:artist].gsub('%20', ' ').gsub('+', ' ').gsub('.html', '')
|
|
end
|
|
|
|
protected
|
|
|
|
def user_session
|
|
Session.find_by_key(session_key)
|
|
end
|
|
|
|
def current_user
|
|
user_session.user if user_present?
|
|
end
|
|
|
|
def user_present?
|
|
!user_session.nil? and !user_session.user.nil?
|
|
end
|
|
|
|
def set_locale
|
|
unless user_session.nil?
|
|
I18n.locale = user_session.user.lang
|
|
end
|
|
end
|
|
|
|
def authorize
|
|
unless Vkontakte.check(params)
|
|
render json: { status: 'login failed' }, status: 403
|
|
end
|
|
end
|
|
|
|
def session_key
|
|
if cookies[:beathaven_sid].nil?
|
|
cookies[:beathaven_sid] = {
|
|
:value => Session.generate_key,
|
|
:expire => 42.years.from_now.utc
|
|
}
|
|
end
|
|
cookies[:beathaven_sid]
|
|
end
|
|
|
|
def cache_key_for object
|
|
"#{object.class.to_s.underscore}_#{object.id}"
|
|
end
|
|
|
|
def render_compact_partial partial_name
|
|
(render_to_string :partial => partial_name.to_s).gsub(/\n(\s+)?/, '')
|
|
end
|
|
end
|