Class: Pretzel::Core

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Extension, Routing::Methods
Includes:
Routing::Evaluation
Defined in:
lib/pretzel/core.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extension

extends

Methods included from Routing::Methods

error

Methods included from Routing::Evaluation

included

Constructor Details

#initializeCore

end self



51
52
53
54
# File 'lib/pretzel/core.rb', line 51

def initialize
  self.class.use Rack::Session::Cookie, secret: self.class.token
  self.class.use Rack::Static, :urls => ["/assets"]
end

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters.



22
23
24
# File 'lib/pretzel/core.rb', line 22

def parameters
  @parameters
end

#requestObject (readonly)

Returns the value of attribute request.



22
23
24
# File 'lib/pretzel/core.rb', line 22

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



22
23
24
# File 'lib/pretzel/core.rb', line 22

def response
  @response
end

Class Method Details

.builderObject



46
47
48
# File 'lib/pretzel/core.rb', line 46

def builder
  @builder ||= Rack::Builder.new
end

.filtersObject



36
37
38
# File 'lib/pretzel/core.rb', line 36

def filters
  @filters ||= Hash.new
end

.new(*args, &block) ⇒ Object



41
42
43
44
# File 'lib/pretzel/core.rb', line 41

def new(*args, &block)
  builder.run new!(*args, &block)
  builder
end

.new!Object



40
# File 'lib/pretzel/core.rb', line 40

alias_method :new!, :new

.routesObject



32
33
34
# File 'lib/pretzel/core.rb', line 32

def routes
  @routes ||= Hash.new { |hash, key| hash[key] = [] }
end

.tokenObject



28
29
30
# File 'lib/pretzel/core.rb', line 28

def token
  @token ||= SecureRandom.hex(32)
end

Instance Method Details

#call(env) ⇒ Object



56
57
58
# File 'lib/pretzel/core.rb', line 56

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pretzel/core.rb', line 60

def call!(env)
  @request = Rack::Request.new(env)
  @response = Rack::Response.new
  @parameters = request.params

  @response["Server"] = "Ruby/#{RUBY_VERSION}"
  @response["X-Generated-By"] = "Pretzel/#{Pretzel::VERSION}"

  evaluate_route

  @response.finish
end

#halt(*res) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pretzel/core.rb', line 73

def halt(*res)
  response.status = res.detect{|x| x.is_a?(Fixnum) } || 200
  response.header.merge!(res.detect{|x| x.is_a?(Hash) } || {})

  if self.class.routes[response.status].empty?
    response.body = [res.detect{|x| x.is_a?(String) } || ""]
  else
    self.class.routes[response.status].each do |route, block|
      response.write instance_eval(&block)
    end
  end

  throw :halt, response
end