Module: Sidekiq::WebHelpers
- Defined in:
- lib/sidekiq/web_helpers.rb
Overview
This is not a public API
Constant Summary collapse
- SAFE_QPARAMS =
%w(page poll)
- RETRY_JOB_KEYS =
Set.new(%w( queue class args retry_count retried_at failed_at jid error_message error_class backtrace error_backtrace enqueued_at retry wrapped created_at ))
Instance Method Summary collapse
-
#add_to_head(&block) ⇒ Object
This view helper provide ability display you html code in to head of page.
-
#capture(&block) ⇒ Object
Simple capture method for erb templates.
- #clear_caches ⇒ Object
- #csrf_tag ⇒ Object
- #current_path ⇒ Object
- #current_status ⇒ Object
- #display_args(args, truncate_after_chars = 2000) ⇒ Object
- #display_custom_head ⇒ Object
- #environment_title_prefix ⇒ Object
-
#filtering ⇒ Object
This is a hook for a Sidekiq Pro feature.
- #find_locale_files(lang) ⇒ Object
- #get_locale ⇒ Object
- #h(text) ⇒ Object
- #job_params(job, score) ⇒ Object
-
#locale ⇒ Object
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.
- #locale_files ⇒ Object
- #location ⇒ Object
- #namespace ⇒ Object
- #number_with_delimiter(number) ⇒ Object
- #parse_params(params) ⇒ Object
- #processes ⇒ Object
- #product_version ⇒ Object
-
#qparams(options) ⇒ Object
Merge options with current params, filter safe params, and stringify to query string.
-
#redirect_with_query(url) ⇒ Object
Any paginated list that performs an action needs to redirect back to the proper page after performing that action.
- #redis_connection ⇒ Object
- #redis_connection_and_namespace ⇒ Object
- #redis_info ⇒ Object
- #relative_time(time) ⇒ Object
- #retries_with_score(score) ⇒ Object
- #retry_extra_items(retry_job) ⇒ Object
- #root_path ⇒ Object
- #stats ⇒ Object
- #strings(lang) ⇒ Object
- #t(msg, options = {}) ⇒ Object
- #to_display(arg) ⇒ Object
- #truncate(text, truncate_after_chars = 2000) ⇒ Object
- #workers ⇒ Object
Instance Method Details
#add_to_head(&block) ⇒ Object
This view helper provide ability display you html code in to head of page. Example:
<% add_to_head do %>
<link rel="stylesheet" .../>
<meta .../>
<% end %>
47 48 49 50 |
# File 'lib/sidekiq/web_helpers.rb', line 47 def add_to_head(&block) @head_html ||= [] @head_html << block if block_given? end |
#capture(&block) ⇒ Object
Simple capture method for erb templates. The origin was capture method from sinatra-contrib library.
59 60 61 62 |
# File 'lib/sidekiq/web_helpers.rb', line 59 def capture(&block) block.call eval('', block.binding) end |
#clear_caches ⇒ Object
20 21 22 23 |
# File 'lib/sidekiq/web_helpers.rb', line 20 def clear_caches @@strings = nil @@locale_files = nil end |
#csrf_tag ⇒ Object
173 174 175 |
# File 'lib/sidekiq/web_helpers.rb', line 173 def csrf_tag "<input type='hidden' name='authenticity_token' value='#{session[:csrf]}'/>" end |
#current_path ⇒ Object
132 133 134 |
# File 'lib/sidekiq/web_helpers.rb', line 132 def current_path @current_path ||= request.path_info.gsub(/^\//,'') end |
#current_status ⇒ Object
136 137 138 |
# File 'lib/sidekiq/web_helpers.rb', line 136 def current_status workers.size == 0 ? 'idle' : 'active' end |
#display_args(args, truncate_after_chars = 2000) ⇒ Object
167 168 169 170 171 |
# File 'lib/sidekiq/web_helpers.rb', line 167 def display_args(args, truncate_after_chars = 2000) args.map do |arg| h(truncate(to_display(arg))) end.join(", ") end |
#display_custom_head ⇒ Object
52 53 54 55 |
# File 'lib/sidekiq/web_helpers.rb', line 52 def display_custom_head return unless defined?(@head_html) @head_html.map { |block| capture(&block) }.join end |
#environment_title_prefix ⇒ Object
237 238 239 240 241 |
# File 'lib/sidekiq/web_helpers.rb', line 237 def environment_title_prefix environment = Sidekiq.[:environment] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development' "[#{environment.upcase}] " unless environment == "production" end |
#filtering ⇒ Object
This is a hook for a Sidekiq Pro feature. Please don’t touch.
36 37 |
# File 'lib/sidekiq/web_helpers.rb', line 36 def filtering(*) end |
#find_locale_files(lang) ⇒ Object
31 32 33 |
# File 'lib/sidekiq/web_helpers.rb', line 31 def find_locale_files(lang) locale_files.select { |file| file =~ /\/#{lang}\.yml$/ } end |
#get_locale ⇒ Object
81 82 83 |
# File 'lib/sidekiq/web_helpers.rb', line 81 def get_locale strings(locale) end |
#h(text) ⇒ Object
217 218 219 220 221 222 223 |
# File 'lib/sidekiq/web_helpers.rb', line 217 def h(text) ::Rack::Utils.escape_html(text) rescue ArgumentError => e raise unless e..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) ⇒ Object
144 145 146 |
# File 'lib/sidekiq/web_helpers.rb', line 144 def job_params(job, score) "#{score}-#{job['jid']}" end |
#locale ⇒ Object
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
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/sidekiq/web_helpers.rb', line 68 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 ⇒ Object
25 26 27 28 29 |
# File 'lib/sidekiq/web_helpers.rb', line 25 def locale_files @@locale_files ||= settings.locales.flat_map do |path| Dir["#{path}/*.yml"] end end |
#location ⇒ Object
112 113 114 |
# File 'lib/sidekiq/web_helpers.rb', line 112 def location Sidekiq.redis { |conn| conn.client.location } end |
#namespace ⇒ Object
120 121 122 |
# File 'lib/sidekiq/web_helpers.rb', line 120 def namespace @@ns ||= Sidekiq.redis { |conn| conn.respond_to?(:namespace) ? conn.namespace : nil } end |
#number_with_delimiter(number) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/sidekiq/web_helpers.rb', line 204 def number_with_delimiter(number) begin Float(number) rescue ArgumentError, TypeError return number end = {delimiter: ',', separator: '.'} parts = number.to_s.to_str.split('.') parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{[:delimiter]}") parts.join([:separator]) end |
#parse_params(params) ⇒ Object
148 149 150 151 |
# File 'lib/sidekiq/web_helpers.rb', line 148 def parse_params(params) score, jid = params.split("-") [score.to_f, jid] end |
#processes ⇒ Object
98 99 100 |
# File 'lib/sidekiq/web_helpers.rb', line 98 def processes @processes ||= Sidekiq::ProcessSet.new end |
#product_version ⇒ Object
243 244 245 |
# File 'lib/sidekiq/web_helpers.rb', line 243 def product_version "Sidekiq v#{Sidekiq::VERSION}" end |
#qparams(options) ⇒ Object
Merge options with current params, filter safe params, and stringify to query string
156 157 158 159 160 161 |
# File 'lib/sidekiq/web_helpers.rb', line 156 def qparams() = .stringify_keys params.merge().map do |key, value| SAFE_QPARAMS.include?(key) ? "#{key}=#{value}" : next end.compact.join("&") end |
#redirect_with_query(url) ⇒ Object
Any paginated list that performs an action needs to redirect back to the proper page after performing that action.
227 228 229 230 231 232 233 234 235 |
# File 'lib/sidekiq/web_helpers.rb', line 227 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 ⇒ Object
116 117 118 |
# File 'lib/sidekiq/web_helpers.rb', line 116 def redis_connection Sidekiq.redis { |conn| conn.client.id } end |
#redis_connection_and_namespace ⇒ Object
247 248 249 250 251 252 |
# File 'lib/sidekiq/web_helpers.rb', line 247 def redis_connection_and_namespace @redis_connection_and_namespace ||= begin namespace_suffix = namespace == nil ? '' : "##{namespace}" "#{redis_connection}#{namespace_suffix}" end end |
#redis_info ⇒ Object
124 125 126 |
# File 'lib/sidekiq/web_helpers.rb', line 124 def redis_info Sidekiq.redis_info end |
#relative_time(time) ⇒ Object
140 141 142 |
# File 'lib/sidekiq/web_helpers.rb', line 140 def relative_time(time) %{<time datetime="#{time.getutc.iso8601}">#{time}</time>} end |
#retries_with_score(score) ⇒ Object
106 107 108 109 110 |
# File 'lib/sidekiq/web_helpers.rb', line 106 def retries_with_score(score) Sidekiq.redis do |conn| conn.zrangebyscore('retry', score, score) end.map { |msg| Sidekiq.load_json(msg) } end |
#retry_extra_items(retry_job) ⇒ Object
196 197 198 199 200 201 202 |
# File 'lib/sidekiq/web_helpers.rb', line 196 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 ⇒ Object
128 129 130 |
# File 'lib/sidekiq/web_helpers.rb', line 128 def root_path "#{env['SCRIPT_NAME']}/" end |
#stats ⇒ Object
102 103 104 |
# File 'lib/sidekiq/web_helpers.rb', line 102 def stats @stats ||= Sidekiq::Stats.new end |
#strings(lang) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/sidekiq/web_helpers.rb', line 6 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.deep_merge!(strs[lang]) end end end end |
#t(msg, options = {}) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/sidekiq/web_helpers.rb', line 85 def t(msg, ={}) string = get_locale[msg] || msg if .empty? string else string % end end |
#to_display(arg) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/sidekiq/web_helpers.rb', line 177 def to_display(arg) begin arg.inspect rescue begin arg.to_s rescue => ex "Cannot display argument: [#{ex.class.name}] #{ex.}" end end end |
#truncate(text, truncate_after_chars = 2000) ⇒ Object
163 164 165 |
# File 'lib/sidekiq/web_helpers.rb', line 163 def truncate(text, truncate_after_chars = 2000) truncate_after_chars && text.size > truncate_after_chars ? "#{text[0..truncate_after_chars]}..." : text end |