class Sidekiq::WebAction

Constants

RACK_SESSION

Attributes

block[RW]
env[RW]
type[RW]

Public Class Methods

new(env, block) click to toggle source
# File lib/sidekiq/web/action.rb, line 74
def initialize(env, block)
  @_erb = false
  @env = env
  @block = block
  @files ||= {}
end

Public Instance Methods

erb(content, options = {}) click to toggle source
# File lib/sidekiq/web/action.rb, line 42
    def erb(content, options = {})
      if content.is_a? Symbol
        unless respond_to?(:"_erb_#{content}")
          src = ERB.new(File.read("#{Web.settings.views}/#{content}.erb")).src
          WebAction.class_eval <<-RUBY, __FILE__, __LINE__ + 1
            def _erb_#{content}
              #{src}
            end
          RUBY
        end
      end

      if @_erb
        _erb(content, options[:locals])
      else
        @_erb = true
        content = _erb(content, options[:locals])

        _render { content }
      end
    end
halt(res) click to toggle source
# File lib/sidekiq/web/action.rb, line 17
def halt(res)
  throw :halt, [res, {"content-type" => "text/plain"}, [res.to_s]]
end
json(payload) click to toggle source
# File lib/sidekiq/web/action.rb, line 70
def json(payload)
  [200, {"content-type" => "application/json", "cache-control" => "private, no-store"}, [Sidekiq.dump_json(payload)]]
end
params() click to toggle source
# File lib/sidekiq/web/action.rb, line 25
def params
  indifferent_hash = Hash.new { |hash, key| hash[key.to_s] if Symbol === key }

  indifferent_hash.merge! request.params
  route_params.each { |k, v| indifferent_hash[k.to_s] = v }

  indifferent_hash
end
redirect(location) click to toggle source
# File lib/sidekiq/web/action.rb, line 21
def redirect(location)
  throw :halt, [302, {"location" => "#{request.base_url}#{location}"}, []]
end
render(engine, content, options = {}) click to toggle source
# File lib/sidekiq/web/action.rb, line 64
def render(engine, content, options = {})
  raise "Only erb templates are supported" if engine != :erb

  erb(content, options)
end
request() click to toggle source
# File lib/sidekiq/web/action.rb, line 13
def request
  @request ||= ::Rack::Request.new(env)
end
route_params() click to toggle source
# File lib/sidekiq/web/action.rb, line 34
def route_params
  env[WebRouter::ROUTE_PARAMS]
end
session() click to toggle source
# File lib/sidekiq/web/action.rb, line 38
def session
  env[RACK_SESSION]
end
settings() click to toggle source
# File lib/sidekiq/web/action.rb, line 9
def settings
  Web.settings
end

Private Instance Methods

_erb(file, locals) click to toggle source
# File lib/sidekiq/web/action.rb, line 83
def _erb(file, locals)
  locals&.each { |k, v| define_singleton_method(k) { v } unless singleton_methods.include? k }

  if file.is_a?(String)
    ERB.new(file).result(binding)
  else
    send(:"_erb_#{file}")
  end
end