Class: Sidekiq::WebApplication

Inherits:
Object
  • Object
show all
Extended by:
WebRouter
Defined in:
lib/sidekiq/web/application.rb

Constant Summary collapse

CONTENT_LENGTH =
"Content-Length".freeze
CONTENT_TYPE =
"Content-Type".freeze
REDIS_KEYS =
%w(redis_version uptime_in_days connected_clients used_memory_human used_memory_peak_human)
NOT_FOUND =
[404, {"Content-Type" => "text/plain", "X-Cascade" => "pass" }.freeze, ["Not Found"]]

Constants included from WebRouter

Sidekiq::WebRouter::DELETE, Sidekiq::WebRouter::GET, Sidekiq::WebRouter::HEAD, Sidekiq::WebRouter::PATCH, Sidekiq::WebRouter::PATH_INFO, Sidekiq::WebRouter::POST, Sidekiq::WebRouter::PUT, Sidekiq::WebRouter::REQUEST_METHOD, Sidekiq::WebRouter::ROUTE_PARAMS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WebRouter

delete, get, match, patch, post, put, route

Constructor Details

#initialize(klass) ⇒ WebApplication

Returns a new instance of WebApplication.



12
13
14
# File 'lib/sidekiq/web/application.rb', line 12

def initialize(klass)
  @klass = klass
end

Class Method Details

.after(path = nil, &block) ⇒ Object



316
317
318
# File 'lib/sidekiq/web/application.rb', line 316

def self.after(path=nil, &block)
  afters << [path && Regexp.new("\\A#{path.gsub("*", ".*")}\\z"), block]
end

.aftersObject



337
338
339
# File 'lib/sidekiq/web/application.rb', line 337

def self.afters
  @afters ||= []
end

.before(path = nil, &block) ⇒ Object



312
313
314
# File 'lib/sidekiq/web/application.rb', line 312

def self.before(path=nil, &block)
  befores << [path && Regexp.new("\\A#{path.gsub("*", ".*")}\\z"), block]
end

.beforesObject



333
334
335
# File 'lib/sidekiq/web/application.rb', line 333

def self.befores
  @befores ||= []
end

.helpers(mod = nil, &block) ⇒ Object



304
305
306
307
308
309
310
# File 'lib/sidekiq/web/application.rb', line 304

def self.helpers(mod=nil, &block)
  if block_given?
    WebAction.class_eval(&block)
  else
    WebAction.send(:include, mod)
  end
end

.run_afters(app, action) ⇒ Object



324
325
326
# File 'lib/sidekiq/web/application.rb', line 324

def self.run_afters(app, action)
  run_hooks(afters, app, action)
end

.run_befores(app, action) ⇒ Object



320
321
322
# File 'lib/sidekiq/web/application.rb', line 320

def self.run_befores(app, action)
  run_hooks(befores, app, action)
end

.run_hooks(hooks, app, action) ⇒ Object



328
329
330
331
# File 'lib/sidekiq/web/application.rb', line 328

def self.run_hooks(hooks, app, action)
  hooks.select { |p,_| !p || p =~ action.env[WebRouter::PATH_INFO] }.
        each {|_,b| action.instance_exec(action.env, app, &b) }
end

.set(key, val) ⇒ Object



28
29
30
# File 'lib/sidekiq/web/application.rb', line 28

def self.set(key, val)
  # nothing, backwards compatibility
end

.settingsObject



20
21
22
# File 'lib/sidekiq/web/application.rb', line 20

def self.settings
  Sidekiq::Web.settings
end

.tabsObject



24
25
26
# File 'lib/sidekiq/web/application.rb', line 24

def self.tabs
  Sidekiq::Web.tabs
end

Instance Method Details

#call(env) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/sidekiq/web/application.rb', line 263

def call(env)
  action = self.class.match(env)
  return NOT_FOUND unless action

  resp = catch(:halt) do
    app = @klass
    self.class.run_befores(app, action)
    begin
      resp = action.instance_exec env, &action.block
    ensure
      self.class.run_afters(app, action)
    end

    resp
  end

  resp = case resp
  when Array
    resp
  when Fixnum
    [resp, {}, []]
  else
    type_header = case action.type
    when :json
      WebAction::APPLICATION_JSON
    when String
      { WebAction::CONTENT_TYPE => action.type, "Cache-Control" => "no-cache" }
    else
      WebAction::TEXT_HTML
    end

    [200, type_header, [resp]]
  end

  resp[1] = resp[1].dup

  resp[1][CONTENT_LENGTH] = resp[2].inject(0) { |l, p| l + p.bytesize }.to_s

  resp
end

#settingsObject



16
17
18
# File 'lib/sidekiq/web/application.rb', line 16

def settings
  @klass.settings
end