Class: Toto::Server

Inherits:
Object show all
Defined in:
lib/toto.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Server.



251
252
253
254
# File 'lib/toto.rb', line 251

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



249
250
251
# File 'lib/toto.rb', line 249

def config
  @config
end

Instance Method Details

#call(env) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/toto.rb', line 256

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 = Toto::Site.new(@config).go(route, *(mime ? mime : []))

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

  # Cache for one day
  @response['Cache-Control'] = "public, max-age=86400"
  @response['Etag'] = Digest::SHA1.hexdigest(response[:body])

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