Class: Barista::Server

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

Defined Under Namespace

Classes: Proxy

Constant Summary collapse

CACHE_FOR_SECONDS =
300
JS_CONTENT_TYPE =
'text/javascript'
COFFEE_CONTENT_TYPE =
'text/coffeescript'
PATH_REGEXP =
/^\/(coffee|java)scripts\//
EXTENSION_MAPPING =

Extensions to the type.

{
  '.coffee' => :coffeescript,
  '.js'     => :javascript
}
CONTENT_TYPE_MAPPING =

Content types for responses.

{
  :coffeescript => COFFEE_CONTENT_TYPE,
  :javascript   => JS_CONTENT_TYPE
}

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.


41
42
43
44
45
# File 'lib/barista/server.rb', line 41

def initialize
  # Cache the responses for common errors.
  forbidden
  not_found
end

Instance Method Details

#_call(env) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/barista/server.rb', line 51

def _call(env)
  @path_info = Rack::Utils.unescape(env['PATH_INFO'].to_s)
  return not_found unless @path_info =~ PATH_REGEXP
  # Strip the prefix.
  @path_info.gsub! PATH_REGEXP, ''
  # Check it's a valid path.
  return forbidden if @path_info.include?('..')
  
  # If coffeescript.js is the request, render the coffeescript compiler code.
  if @path_info == 'coffeescript.js'
    return response_for_text(CoffeeScript::Source.contents)
  end
  # Look up the type of the file based off of the extension.
  @result_type = EXTENSION_MAPPING[File.extname(@path_info)]
  return not_found if @result_type.nil? || (@result_type == :coffeescript && !Barista.embedded_interpreter?)
  # Process the difference in content type.
  content, last_modified = Barista::Compiler.compile_as(@path_info, @result_type)
  if content.nil?
    not_found
  else
    response_for_text content, CONTENT_TYPE_MAPPING[@result_type], last_modified
  end
end

#call(env) ⇒ Object


47
48
49
# File 'lib/barista/server.rb', line 47

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