Skip to content
Permalink
Browse files

Implement compatibility interface with sinatra

  • Loading branch information...
badosu committed Jul 29, 2016
1 parent 91fd77d commit 5c3a6bd103f192d9594f733b723bc353423205d1
@@ -34,6 +34,10 @@ class Web
}

class << self
def settings
self
end

def default_tabs
DEFAULT_TABS
end
@@ -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
@@ -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
@@ -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)
@@ -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

0 comments on commit 5c3a6bd

Please sign in to comment.
You can’t perform that action at this time.