require 'uri'
module EmberCliDeployRedis
# This is not a public API
module WebHelpers
def strings(lang)
@@strings ||= {}
@@strings[lang] ||= begin
# Allow sidekiq-web extensions to add locale paths
# so extensions can be localized
settings.locales.each_with_object({}) do |path, global|
find_locale_files(lang).each do |file|
strs = YAML.load(File.open(file))
global.merge!(strs[lang])
end
end
end
end
def locale_files
@@locale_files = settings.locales.flat_map do |path|
Dir["#{path}/*.yml"]
end
end
def find_locale_files(lang)
locale_files.select { |file| file =~ /\/#{lang}\.yml$/ }
end
# This view helper provide ability display you html code in
# to head of page. Example:
#
# <% add_to_head do %>
#
#
# <% end %>
#
def add_to_head(&block)
@head_html ||= []
@head_html << block if block_given?
end
def display_custom_head
return unless defined?(@head_html)
@head_html.map { |block| capture(&block) }.join
end
# Simple capture method for erb templates. The origin was
# capture method from sinatra-contrib library.
def capture(&block)
block.call
eval('', block.binding)
end
# Given a browser request Accept-Language header like
# "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,ru;q=0.2", this function
# will return "fr" since that's the first code with a matching
# locale in web/locales
def locale
@locale ||= begin
locale = 'en'.freeze
languages = request.env['HTTP_ACCEPT_LANGUAGE'.freeze] || 'en'.freeze
languages.downcase.split(','.freeze).each do |lang|
next if lang == '*'.freeze
lang = lang.split(';'.freeze)[0]
break locale = lang if find_locale_files(lang).any?
end
locale
end
end
def get_locale
strings(locale)
end
def t(msg, options={})
string = get_locale[msg] || msg
if options.empty?
string
else
string % options
end
end
def location
EmberCliDeployRedis.configuration.redis do |conn| conn.client.location end
end
def redis_connection
EmberCliDeployRedis.configuration.redis do |conn| conn.client.id end
end
def namespace
@@ns ||= EmberCliDeployRedis.configuration.redis do |conn|
conn.respond_to?(:namespace) ? conn.namespace : nil
end
end
def redis_info
EmberCliDeployRedis.configuration.redis do |conn|
# admin commands can't go through redis-namespace starting
# in redis-namespace 2.0
if conn.respond_to?(:namespace)
conn.redis.info
else
conn.info
end
end
end
def root_path
"#{env['SCRIPT_NAME']}/"
end
def relative_time(time)
%{}
end
def csrf_tag
""
end
def h(text)
::Rack::Utils.escape_html(text)
rescue ArgumentError => e
raise unless e.message.eql?('invalid byte sequence in UTF-8')
text.encode!('UTF-16', 'UTF-8', invalid: :replace, replace: '').encode!('UTF-8', 'UTF-16')
retry
end
# Any paginated list that performs an action needs to redirect
# back to the proper page after performing that action.
def redirect_with_query(url)
r = request.referer
if r && r =~ /\?/
ref = URI(r)
redirect("#{url}?#{ref.query}")
else
redirect url
end
end
def product_version
"ember_cli_deploy_redis-ruby v#{EmberCliDeployRedis::VERSION}"
end
def redis_connection_and_namespace
@redis_connection_and_namespace ||= begin
namespace_suffix = namespace == nil ? '' : "##{namespace}"
"#{redis_connection}#{namespace_suffix}"
end
end
def revision_specifier_query_param
EmberCliDeployRedis.configuration.revision_specifier_query_param
end
def revision_test_url(revision)
"/?#{revision_specifier_query_param}[#{revision.application.name}]=#{revision.name}"
end
end
end