class Elektra::Base
Attributes
request[R]
response[R]
Public Class Methods
endpoints()
click to toggle source
# File lib/elektra/routes.rb, line 19 def endpoints @endpoints ||= Hash.new { |hash, key| hash[key] = [] } end
filter_collection()
click to toggle source
# File lib/elektra/filters.rb, line 10 def filter_collection @filters ||= Hash.new { |hash, key| hash[key] = [] } end
get_path(pattern)
click to toggle source
# File lib/elektra/routes.rb, line 23 def get_path(pattern) placeholders = [] pattern.gsub!(/(:\w+)/) do |match| placeholders << $1[1..-1] "([^/?#]+)" end [%r{^#{pattern}$}, placeholders] end
helpers(*args) { || ... }
click to toggle source
# File lib/elektra/helpers.rb, line 5 def helpers(*args) if block_given? command = Proc.new { yield } class_eval(&command) helpers_collection << command else args.each do |helper| command = "include #{ helper}" class_eval(command) helpers_collection << command end end end
helpers_collection()
click to toggle source
# File lib/elektra/helpers.rb, line 19 def helpers_collection @helpers ||= [] end
route_methods(*methods)
click to toggle source
# File lib/elektra/routes.rb, line 8 def self.route_methods(*methods) Array(methods).each do |method_name| define_method(method_name) do |*args, &block| pattern = args[0] endpoints[method_name] << [get_path(pattern), block] end end end
set(key, value)
click to toggle source
# File lib/elektra/config.rb, line 7 def set(key, value) @@configuration[key] = value end
Public Instance Methods
call(env)
click to toggle source
# File lib/elektra/base.rb, line 33 def call(env) @request = Rack::Request.new(env) @response = Rack::Response.new generate_response_for_request @response.finish end
execute_after_filters()
click to toggle source
# File lib/elektra/base.rb, line 85 def execute_after_filters self.class.filter_collection[:after].each do |after_filter| instance_eval(&after_filter) end end
execute_block_and_before_filters()
click to toggle source
# File lib/elektra/base.rb, line 75 def execute_block_and_before_filters block_to_execute = get_block_for_request # require "pry"; binding.pry self.class.filter_collection[:before].each do |before_filter| instance_eval(&before_filter) end instance_eval(&block_to_execute) end
generate_response_for_request()
click to toggle source
# File lib/elektra/base.rb, line 46 def generate_response_for_request catch :halt do execute_block_result = execute_block_and_before_filters if execute_block_result update_response_with execute_block_result execute_after_filters return end halt 404 end end
get_block_for_request()
click to toggle source
# File lib/elektra/base.rb, line 92 def get_block_for_request path, verb = path_and_verb self.class.endpoints[verb].each do |route, block| pattern, placeholders = route if path =~ route[0] placeholders.each_with_index do |placeholder, index| @request.update_param(placeholder, eval("$#{index + 1}")) end return block end end end
halt(*response)
click to toggle source
# File lib/elektra/base.rb, line 27 def halt(*response) response = response.first if response.length == 1 update_response_with response throw :halt end
instance_variable_to_view()
click to toggle source
# File lib/elektra/render.rb, line 15 def instance_variable_to_view var_to_be_passed = instance_variables - [:@request, :@response] var_to_be_passed .map { |name| [name, instance_variable_get(name)] } end
path_and_verb()
click to toggle source
# File lib/elektra/base.rb, line 42 def path_and_verb [@request.path_info, @request.request_method.downcase.to_sym] end
redirect(*response)
click to toggle source
# File lib/elektra/redirect.rb, line 3 def redirect(*response) redirect_path = response.shift @response.redirect(redirect_path) update_response_with response throw :halt end
render(template, layout: true)
click to toggle source
# File lib/elektra/render.rb, line 4 def render(template, layout: true) filename = File.join(@@configuration[:views_folder], "#{template}.slim") @response["Content-Type"] = "text/html" scope = Object.new instance_variable_to_view.each { |key, value| scope.instance_variable_set(key, value) } compiled_template = Slim::Template.new(filename).render(scope) compiled_template = Slim::Template.new("#{@@configuration[:views_folder]}/#{@@configuration[:layout]}.slim"). render { compiled_template } if layout compiled_template end
to(path)
click to toggle source
# File lib/elektra/redirect.rb, line 10 def to(path) port = ":#{@request.port}" unless @request.port == 80 "#{@request.host}#{port}#{path}" end
update_response_with(execute_block_result)
click to toggle source
# File lib/elektra/base.rb, line 58 def update_response_with(execute_block_result) if execute_block_result.is_a? String @response.write execute_block_result elsif execute_block_result.is_a? Fixnum @response.status = execute_block_result elsif execute_block_result.length == 3 @response.status = execute_block_result[0] execute_block_result[1].each { |key, value| @response[key] = value } @response.body = execute_block_result[2] elsif execute_block_result.length == 2 @response.status = execute_block_result[0] @response.body = execute_block_result[1] elsif execute_block_result.respond_to?(:each) @response.body = execute_block_result end end