module Roundhouse::WebHelpers
This is not a public API
Constants
- RETRY_JOB_KEYS
- SAFE_QPARAMS
Public Instance Methods
add_to_head(&block)
click to toggle source
This view helper provide ability display you html code in to head of page. Example:
<% add_to_head do %> <link rel="stylesheet" .../> <meta .../> <% end %>
# File lib/roundhouse/web_helpers.rb, line 42 def add_to_head(&block) @head_html ||= [] @head_html << block if block_given? end
capture(&block)
click to toggle source
Simple capture method for erb templates. The origin was capture method from sinatra-contrib library.
# File lib/roundhouse/web_helpers.rb, line 54 def capture(&block) block.call eval('', block.binding) end
csrf_tag()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 176 def csrf_tag "<input type='hidden' name='authenticity_token' value='#{session[:csrf]}'/>" end
current_path()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 135 def current_path @current_path ||= request.path_info.gsub(/^\//,'') end
current_status()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 139 def current_status workers.size == 0 ? 'idle' : 'active' end
display_args(args, truncate_after_chars = 2000)
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 170 def display_args(args, truncate_after_chars = 2000) args.map do |arg| h(truncate(to_display(arg))) end.join(", ") end
display_custom_head()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 47 def display_custom_head return unless defined?(@head_html) @head_html.map { |block| capture(&block) }.join end
environment_title_prefix()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 239 def environment_title_prefix environment = Roundhouse.options[:environment] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development' "[#{environment.upcase}] " unless environment == "production" end
filtering(*)
click to toggle source
This is a hook for a Roundhouse
Pro feature. Please don’t touch.
# File lib/roundhouse/web_helpers.rb, line 31 def filtering(*) end
find_locale_files(lang)
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 26 def find_locale_files(lang) locale_files.select { |file| file =~ /\/#{lang}\.yml$/ } end
get_locale()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 76 def get_locale strings(locale) end
h(text)
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 219 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
job_params(job, score)
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 147 def job_params(job, score) "#{score}-#{job['jid']}" end
locale()
click to toggle source
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
# File lib/roundhouse/web_helpers.rb, line 63 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
locale_files()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 20 def locale_files @@locale_files = settings.locales.flat_map do |path| Dir["#{path}/*.yml"] end end
location()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 107 def location Roundhouse.redis { |conn| conn.client.location } end
namespace()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 115 def namespace @@ns ||= Roundhouse.redis { |conn| conn.respond_to?(:namespace) ? conn.namespace : nil } end
number_with_delimiter(number)
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 206 def number_with_delimiter(number) begin Float(number) rescue ArgumentError, TypeError return number end options = {delimiter: ',', separator: '.'} parts = number.to_s.to_str.split('.') parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}") parts.join(options[:separator]) end
parse_params(params)
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 151 def parse_params(params) score, jid = params.split("-") [score.to_f, jid] end
processes()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 93 def processes @processes ||= Roundhouse::ProcessSet.new end
product_version()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 245 def product_version "Roundhouse v#{Roundhouse::VERSION}" end
qparams(options)
click to toggle source
Merge options with current params, filter safe params, and stringify to query string
# File lib/roundhouse/web_helpers.rb, line 159 def qparams(options) options = options.stringify_keys params.merge(options).map do |key, value| SAFE_QPARAMS.include?(key) ? "#{key}=#{value}" : next end.join("&") end
redirect_with_query(url)
click to toggle source
Any paginated list that performs an action needs to redirect back to the proper page after performing that action.
# File lib/roundhouse/web_helpers.rb, line 229 def redirect_with_query(url) r = request.referer if r && r =~ /\?/ ref = URI(r) redirect("#{url}?#{ref.query}") else redirect url end end
redis_connection()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 111 def redis_connection Roundhouse.redis { |conn| conn.client.id } end
redis_info()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 119 def redis_info Roundhouse.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
relative_time(time)
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 143 def relative_time(time) %{<time datetime="#{time.getutc.iso8601}">#{time}</time>} end
retries_with_score(score)
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 101 def retries_with_score(score) Roundhouse.redis do |conn| conn.zrangebyscore('retry', score, score) end.map { |msg| Roundhouse.load_json(msg) } end
retry_extra_items(retry_job)
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 198 def retry_extra_items(retry_job) @retry_extra_items ||= {}.tap do |extra| retry_job.item.each do |key, value| extra[key] = value unless RETRY_JOB_KEYS.include?(key) end end end
root_path()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 131 def root_path "#{env['SCRIPT_NAME']}/" end
stats()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 97 def stats @stats ||= Roundhouse::Stats.new end
strings(lang)
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 6 def strings(lang) @@strings ||= {} @@strings[lang] ||= begin # Allow roundhouse-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.deep_merge!(strs[lang]) end end end end
t(msg, options={})
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 80 def t(msg, options={}) string = get_locale[msg] || msg if options.empty? string else string % options end end
to_display(arg)
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 180 def to_display(arg) begin arg.inspect rescue begin arg.to_s rescue => ex "Cannot display argument: [#{ex.class.name}] #{ex.message}" end end end
truncate(text, truncate_after_chars = 2000)
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 166 def truncate(text, truncate_after_chars = 2000) truncate_after_chars && text.size > truncate_after_chars ? "#{text[0..truncate_after_chars]}..." : text end
workers()
click to toggle source
# File lib/roundhouse/web_helpers.rb, line 89 def workers @workers ||= Roundhouse::Workers.new end