2016-07-27 16:18:52 -03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Sidekiq
|
|
|
|
class WebAction
|
2018-02-16 13:11:54 -08:00
|
|
|
RACK_SESSION = 'rack.session'
|
2016-07-27 16:18:52 -03:00
|
|
|
|
2016-07-29 12:09:00 -07:00
|
|
|
attr_accessor :env, :block, :type
|
2016-07-28 23:36:21 -03:00
|
|
|
|
|
|
|
def settings
|
|
|
|
Web.settings
|
|
|
|
end
|
2016-07-27 16:18:52 -03:00
|
|
|
|
|
|
|
def request
|
2016-07-29 12:09:00 -07:00
|
|
|
@request ||= ::Rack::Request.new(env)
|
2016-07-27 16:18:52 -03:00
|
|
|
end
|
|
|
|
|
2016-07-28 23:36:21 -03:00
|
|
|
def halt(res)
|
|
|
|
throw :halt, res
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirect(location)
|
2016-09-14 10:15:28 -07:00
|
|
|
throw :halt, [302, { "Location" => "#{request.base_url}#{location}" }, []]
|
2016-07-28 23:36:21 -03:00
|
|
|
end
|
|
|
|
|
2016-07-27 16:18:52 -03:00
|
|
|
def params
|
2016-07-28 23:36:21 -03:00
|
|
|
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
|
2016-07-27 16:18:52 -03:00
|
|
|
end
|
|
|
|
|
|
|
|
def route_params
|
|
|
|
env[WebRouter::ROUTE_PARAMS]
|
|
|
|
end
|
|
|
|
|
|
|
|
def session
|
|
|
|
env[RACK_SESSION]
|
|
|
|
end
|
|
|
|
|
2016-07-28 23:36:21 -03:00
|
|
|
def erb(content, options = {})
|
|
|
|
if content.kind_of? Symbol
|
2016-07-29 19:41:42 -03:00
|
|
|
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")
|
2016-07-29 16:14:07 -03:00
|
|
|
end
|
2016-07-27 16:18:52 -03:00
|
|
|
end
|
|
|
|
|
2016-07-28 23:36:21 -03:00
|
|
|
if @_erb
|
|
|
|
_erb(content, options[:locals])
|
|
|
|
else
|
|
|
|
@_erb = true
|
|
|
|
content = _erb(content, options[:locals])
|
2016-07-27 16:18:52 -03:00
|
|
|
|
2016-07-28 23:36:21 -03:00
|
|
|
_render { content }
|
|
|
|
end
|
2016-07-27 16:18:52 -03:00
|
|
|
end
|
|
|
|
|
2016-07-28 23:36:21 -03:00
|
|
|
def render(engine, content, options = {})
|
|
|
|
raise "Only erb templates are supported" if engine != :erb
|
2016-07-27 16:18:52 -03:00
|
|
|
|
2016-07-28 23:36:21 -03:00
|
|
|
erb(content, options)
|
2016-07-27 16:18:52 -03:00
|
|
|
end
|
|
|
|
|
|
|
|
def json(payload)
|
2016-09-29 13:24:49 -07:00
|
|
|
[200, { "Content-Type" => "application/json", "Cache-Control" => "no-cache" }, [Sidekiq.dump_json(payload)]]
|
2016-07-27 16:18:52 -03:00
|
|
|
end
|
|
|
|
|
2016-07-29 12:09:00 -07:00
|
|
|
def initialize(env, block)
|
2016-08-30 13:29:03 -07:00
|
|
|
@_erb = false
|
2016-07-27 16:18:52 -03:00
|
|
|
@env = env
|
2016-07-29 12:09:00 -07:00
|
|
|
@block = block
|
2016-07-29 16:14:07 -03:00
|
|
|
@@files ||= {}
|
2016-07-27 16:18:52 -03:00
|
|
|
end
|
2016-07-28 23:36:21 -03:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def _erb(file, locals)
|
2017-12-18 17:01:28 +05:30
|
|
|
locals.each {|k, v| define_singleton_method(k){ v } unless (singleton_methods.include? k)} if locals
|
2016-07-28 23:36:21 -03:00
|
|
|
|
2016-07-29 16:14:07 -03:00
|
|
|
if file.kind_of?(String)
|
|
|
|
ERB.new(file).result(binding)
|
|
|
|
else
|
2016-07-29 19:41:42 -03:00
|
|
|
send(:"_erb_#{file}")
|
2016-07-29 16:14:07 -03:00
|
|
|
end
|
2016-07-28 23:36:21 -03:00
|
|
|
end
|
2016-07-27 16:18:52 -03:00
|
|
|
end
|
|
|
|
end
|