Skip to content

Commit

Permalink
Implement compatibility interface with sinatra
Browse files Browse the repository at this point in the history
  • Loading branch information
badosu committed Jul 29, 2016
1 parent 91fd77d commit 5c3a6bd
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 123 deletions.
16 changes: 14 additions & 2 deletions lib/sidekiq/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class Web
}

class << self
def settings
self
end

def default_tabs
DEFAULT_TABS
end
Expand All @@ -47,12 +51,20 @@ def locales
@locales ||= LOCALES
end

def views
@views ||= VIEWS
end

def session_secret=(secret)
@secret = secret
@session_secret = secret
end

attr_accessor :app_url, :session_secret
attr_writer :locales
attr_writer :locales, :views
end

def settings
self.class.settings
end

def initialize
Expand Down
65 changes: 46 additions & 19 deletions lib/sidekiq/web/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,35 @@ class WebAction

LOCATION = "Location".freeze

TEXT_HTML = { "Content-Type".freeze => "text/html".freeze }
APPLICATION_JSON = { "Content-Type".freeze => "application/json".freeze }
CONTENT_TYPE = "Content-Type".freeze
TEXT_HTML = { CONTENT_TYPE => "text/html".freeze }
APPLICATION_JSON = { CONTENT_TYPE => "application/json".freeze }

attr_accessor :env, :app
attr_accessor :env, :app, :type

def settings
Web.settings
end

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

def halt(res)
throw :halt, res
end

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

def params
request.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

def route_params
Expand All @@ -27,28 +45,29 @@ def session
env[RACK_SESSION]
end

def erb(content, options = {})
b = binding
def content_type(type)
@type = type
end

if locals = options[:locals]
locals.each {|k, v| b.local_variable_set(k, v) }
def erb(content, options = {})
if content.kind_of? Symbol
content = File.read("#{Web.settings.views}/#{content}.erb")
end

_render { ERB.new(content).result(b) }
end
if @_erb
_erb(content, options[:locals])
else
@_erb = true
content = _erb(content, options[:locals])

def partial(file, locals = {})
ERB.new(File.read "#{Web::VIEWS}/_#{file}.erb").result(binding)
end

def redirect(location)
[302, { LOCATION => "#{request.base_url}#{root_path}#{location}" }, []]
_render { content }
end
end

def render(file, locals = {})
output = erb(File.read "#{Web::VIEWS}/#{file}.erb", locals: locals)
def render(engine, content, options = {})
raise "Only erb templates are supported" if engine != :erb

[200, TEXT_HTML, [output]]
erb(content, options)
end

def json(payload)
Expand All @@ -59,5 +78,13 @@ def initialize(env, app)
@env = env
@app = app
end

private

def _erb(file, locals)
locals.each {|k, v| define_singleton_method(k){ v } } if locals

ERB.new(file).result(binding)
end
end
end
Loading

0 comments on commit 5c3a6bd

Please sign in to comment.