Class: Sidekiq::WebAction
- Inherits:
-
Object
- Object
- Sidekiq::WebAction
- Defined in:
- lib/sidekiq/web/action.rb
Constant Summary collapse
- RACK_SESSION =
'rack.session'.freeze
- TEXT_HTML =
{ "Content-Type" => "text/html", "Cache-Control" => "no-cache" }.freeze
- APPLICATION_JSON =
{ "Content-Type" => "application/json", "Cache-Control" => "no-cache" }.freeze
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#env ⇒ Object
Returns the value of attribute env.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #content_type(type) ⇒ Object
- #erb(content, options = {}) ⇒ Object
- #halt(res) ⇒ Object
-
#initialize(env, block) ⇒ WebAction
constructor
A new instance of WebAction.
- #json(payload) ⇒ Object
- #params ⇒ Object
- #redirect(location) ⇒ Object
- #render(engine, content, options = {}) ⇒ Object
- #request ⇒ Object
- #route_params ⇒ Object
- #session ⇒ Object
- #settings ⇒ Object
Constructor Details
#initialize(env, block) ⇒ WebAction
Returns a new instance of WebAction.
76 77 78 79 80 81 |
# File 'lib/sidekiq/web/action.rb', line 76 def initialize(env, block) @_erb = false @env = env @block = block @@files ||= {} end |
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block.
9 10 11 |
# File 'lib/sidekiq/web/action.rb', line 9 def block @block end |
#env ⇒ Object
Returns the value of attribute env.
9 10 11 |
# File 'lib/sidekiq/web/action.rb', line 9 def env @env end |
#type ⇒ Object
Returns the value of attribute type.
9 10 11 |
# File 'lib/sidekiq/web/action.rb', line 9 def type @type end |
Instance Method Details
#content_type(type) ⇒ Object
44 45 46 |
# File 'lib/sidekiq/web/action.rb', line 44 def content_type(type) @type = type end |
#erb(content, options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sidekiq/web/action.rb', line 48 def erb(content, = {}) if content.kind_of? Symbol unless respond_to?(:"_erb_#{content}") src = ERB.new(File.read("#{Web.settings.views}/#{content}.erb")).src WebAction.class_eval("def _erb_#{content}\n#{src}\n end") end end if @_erb _erb(content, [:locals]) else @_erb = true content = _erb(content, [:locals]) _render { content } end end |
#halt(res) ⇒ Object
19 20 21 |
# File 'lib/sidekiq/web/action.rb', line 19 def halt(res) throw :halt, res end |
#json(payload) ⇒ Object
72 73 74 |
# File 'lib/sidekiq/web/action.rb', line 72 def json(payload) [200, APPLICATION_JSON, [Sidekiq.dump_json(payload)]] end |
#params ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/sidekiq/web/action.rb', line 27 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
23 24 25 |
# File 'lib/sidekiq/web/action.rb', line 23 def redirect(location) throw :halt, [302, { "Location" => "#{request.base_url}#{location}" }, []] end |
#render(engine, content, options = {}) ⇒ Object
66 67 68 69 70 |
# File 'lib/sidekiq/web/action.rb', line 66 def render(engine, content, = {}) raise "Only erb templates are supported" if engine != :erb erb(content, ) end |
#request ⇒ Object
15 16 17 |
# File 'lib/sidekiq/web/action.rb', line 15 def request @request ||= ::Rack::Request.new(env) end |
#route_params ⇒ Object
36 37 38 |
# File 'lib/sidekiq/web/action.rb', line 36 def route_params env[WebRouter::ROUTE_PARAMS] end |
#session ⇒ Object
40 41 42 |
# File 'lib/sidekiq/web/action.rb', line 40 def session env[RACK_SESSION] end |