Class: RubySlippers::Engine::App

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_slippers/app.rb

Constant Summary collapse

@@site =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, &blk) ⇒ App

Returns a new instance of App.



8
9
10
11
12
# File 'lib/ruby_slippers/app.rb', line 8

def initialize config = {}, &blk
  @config = config.is_a?(Config) ? config : Config.new(config)
  @config.instance_eval(&blk) if block_given?
  @site ||= @@site = Site.new(@config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/ruby_slippers/app.rb', line 4

def config
  @config
end

#siteObject (readonly)

Returns the value of attribute site.



4
5
6
# File 'lib/ruby_slippers/app.rb', line 4

def site
  @site
end

Class Method Details

.siteObject



14
15
16
# File 'lib/ruby_slippers/app.rb', line 14

def self.site
  @@site
end

Instance Method Details

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_slippers/app.rb', line 18

def call env
  @request  = Rack::Request.new env
  @response = Rack::Response.new

  return [400, {}, []] unless @request.get?

  path, mime = @request.path_info.split('.')
  route = (path || '/').split('/').reject {|i| i.empty? }

  response = @site.go(route, env, *(mime ? mime : []))

  @response.body = [response[:body]]
  @response['Content-Length'] = response[:body].length.to_s unless response[:body].empty?
  @response['Content-Type']   = Rack::Mime.mime_type(".#{response[:type]}")

  # Set http cache headers
  @response['Cache-Control'] = if RubySlippers::Engine.env == 'production'
    "public, max-age=#{@config[:cache]}"
  else
    "no-cache, must-revalidate"
  end

  @response['ETag'] = %("#{Digest::SHA1.hexdigest(response[:body])}")

  @response.status = response[:status]
  @response.finish
end