-
-
Save abuiles/a8f214173d6d5e037053 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RedisController < ApplicationController | |
private | |
def render_from_redis(prefix, variation = nil) | |
manifest_id = params[:manifest_id] | |
if manifest_id.blank? | |
current_manifest_key = if variation | |
"#{prefix}:#{variation}:current" | |
else | |
"#{prefix}:current" | |
end | |
manifest_id = $redis.get(current_manifest_key) | |
if manifest_id.blank? | |
manifest_id = Rails.env | |
end | |
end | |
raise "invalid id #{manifest_id}" unless manifest_id =~ /\A(dev|test|[0-9a-f]{32})\Z/i | |
html_key = "#{prefix}:#{manifest_id}" | |
html = $redis.get(html_key) | |
raise ActionController::RoutingError, "#{html_key} not found" if html.blank? | |
insert_current_user_meta(html) | |
insert_csrf_meta(html) | |
transform_html(html) | |
render :text => html | |
end | |
def insert_current_user_meta(html) | |
user_serializer = current_user_serializer_class.new(current_user) | |
user_json = Rack::Utils.escape_html(user_serializer.to_json) | |
html.insert(head_pos(html), <<-HTML) | |
<meta name="yapp-user" content="#{user_json}"> | |
HTML | |
end | |
def current_user_serializer_class | |
Api::Auth::UserSerializer | |
end | |
def insert_csrf_meta(html) | |
html.insert(head_pos(html), render_to_string(inline: '<%= csrf_meta_tags %>')) | |
end | |
def head_pos(html) | |
head_open = html.index("<head") | |
html.index(">", head_open) + 1 | |
end | |
def body_bottom_pos(html) | |
html.index("</body>") | |
end | |
def transform_html(html) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment