Class: Fuse::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/fuse/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Server

Returns a new instance of Server.



5
6
7
8
# File 'lib/fuse/server.rb', line 5

def initialize(options)
  @options = options
  Fuse.log "Starting Fuse Server v#{Fuse::VERSION}", :success
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fuse/server.rb', line 10

def call(env)

  request = Rack::Request.new(env)

  call_options = @options.merge Hash[request.GET.map{ |k, v| [k.to_sym, v] }]

  if @root && (asset = Fuse::Document::Asset.for(request.path))
    log env, "Serve asset #{asset.path}"
    return asset.call(env)
  end

  begin
    doc = Fuse::Document.new(call_options)
    @root = doc.root
    result = doc.to_s
    log env, "Using    #{doc.xsl_path} for transformation" if doc.xsl_path
    log env, "Rendered #{doc.source_path} (#{result.length} bytes)", :success
  rescue Fuse::Exception::SourceUnknown::TooManySources
    result = render_list($!.options, $!.option_name, request)
    log env, 'Multiple source document options', :notice
  rescue Fuse::Exception
    if $!.message
      result = render_error($!.message)
      log env, $!.message, :notice
    else
      raise
    end
  end

  [200, {'Content-Type' => 'text/html'}, [result]]

end