Class: Sidekiq::WebAction

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/web/action.rb

Constant Summary collapse

RACK_SESSION =
"rack.session"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, block) ⇒ WebAction

Returns a new instance of WebAction.



96
97
98
99
100
101
# File 'lib/sidekiq/web/action.rb', line 96

def initialize(env, block)
  @_erb = false
  @env = env
  @block = block
  @files ||= {}
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



7
8
9
# File 'lib/sidekiq/web/action.rb', line 7

def block
  @block
end

#envObject

Returns the value of attribute env.



7
8
9
# File 'lib/sidekiq/web/action.rb', line 7

def env
  @env
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/sidekiq/web/action.rb', line 7

def type
  @type
end

Instance Method Details

#erb(content, options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sidekiq/web/action.rb', line 59

def erb(content, options = {})
  if content.is_a? Symbol
    unless respond_to?(:"_erb_#{content}")
      views = options[:views] || Web.settings.views
      filename = "#{views}/#{content}.erb"
      src = ERB.new(File.read(filename)).src

      # Need to use lineno less by 1 because erb generates a
      # comment before the source code.
      WebAction.class_eval <<-RUBY, filename, -1 # standard:disable Style/EvalWithLocation
        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) ⇒ Object



17
18
19
# File 'lib/sidekiq/web/action.rb', line 17

def halt(res)
  throw :halt, [res, {Rack::CONTENT_TYPE => "text/plain"}, [res.to_s]]
end

#json(payload) ⇒ Object



92
93
94
# File 'lib/sidekiq/web/action.rb', line 92

def json(payload)
  [200, {Rack::CONTENT_TYPE => "application/json", Rack::CACHE_CONTROL => "private, no-store"}, [Sidekiq.dump_json(payload)]]
end

#paramsObject

deprecated, will warn in 8.0



31
32
33
34
35
36
37
38
# File 'lib/sidekiq/web/action.rb', line 31

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) ⇒ Object



21
22
23
# File 'lib/sidekiq/web/action.rb', line 21

def redirect(location)
  throw :halt, [302, {Web::LOCATION => "#{request.base_url}#{location}"}, []]
end

#reload_pageObject



25
26
27
28
# File 'lib/sidekiq/web/action.rb', line 25

def reload_page
  current_location = request.referer.gsub(request.base_url, "")
  redirect current_location
end

#render(engine, content, options = {}) ⇒ Object



86
87
88
89
90
# File 'lib/sidekiq/web/action.rb', line 86

def render(engine, content, options = {})
  raise "Only erb templates are supported" if engine != :erb

  erb(content, options)
end

#requestObject



13
14
15
# File 'lib/sidekiq/web/action.rb', line 13

def request
  @request ||= ::Rack::Request.new(env)
end

#route_params(key = nil) ⇒ Object

Use like ‘route_params(:name)` within your action blocks key is required in 8.0, nil is only used for backwards compatibility



47
48
49
50
51
52
53
# File 'lib/sidekiq/web/action.rb', line 47

def route_params(key = nil)
  if key
    env[WebRouter::ROUTE_PARAMS][key]
  else
    env[WebRouter::ROUTE_PARAMS]
  end
end

#sessionObject



55
56
57
# File 'lib/sidekiq/web/action.rb', line 55

def session
  env[RACK_SESSION]
end

#settingsObject



9
10
11
# File 'lib/sidekiq/web/action.rb', line 9

def settings
  Web.settings
end

#url_params(key) ⇒ Object

Use like ‘url_params(“page”)` within your action blocks



41
42
43
# File 'lib/sidekiq/web/action.rb', line 41

def url_params(key)
  request.params[key]
end