Class: Gulp::Pipeline::Rails::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/gulp/pipeline/rails/server.rb

Overview

‘Server` provides a Rack compatible `call` interface and url generation helpers.

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Server

Returns a new instance of Server.



11
12
13
14
15
16
17
18
19
# File 'lib/gulp/pipeline/rails/server.rb', line 11

def initialize(app, options = {})
  config = app.config

  @debug = config.assets.debug
  @file_server = Rack::File.new(app.root.join('public', Assets.base_path).to_s)
  @cache_duration = options[:duration] || 1
  @duration_in_seconds = duration_in_seconds
  @duration_in_words = duration_in_words
end

Instance Method Details

#call(env) ⇒ Object

A request for ‘“/assets/javascripts/foo/bar.js”` will search the public directory.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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/gulp/pipeline/rails/server.rb', line 22

def call(env)
  if env['REQUEST_METHOD'] != 'GET'
    return method_not_allowed_response
  end

  msg = "Served asset #{env['PATH_INFO']} -"

  # Extract the path from everything after the leading slash
  path = Rack::Utils.unescape(env['PATH_INFO'].to_s.sub(/^\//, ''))

  status, headers, body =
    if forbidden_request?(path)
      # URLs containing a `..` are rejected for security reasons.
      forbidden_response
    else
      # # If not debug, enforce/require fingerprinted url. Do we really need this or just paranoia?
      # if (!@debug)
      #   fingerprint = path_fingerprint(path)
      #   if (fingerprint.nil?)
      #     msg += ' not a manifested file'
      #     return not_found_response
      #   end
      # end

      # standard file serve
      @file_server.call(env)
    end

  # log our status
  case status
    when :ok
      logger.info "#{msg} 200 OK "
    when :not_modified
      logger.info "#{msg} 304 Not Modified "
    when :not_found
      logger.info "#{msg} 404 Not Found "
    else
      logger.info "#{msg} #{status} Unknown status message "
  end

  # add cache headers
  headers['Cache-Control'] ="max-age=#{@duration_in_seconds}, public"
  headers['Expires'] = @duration_in_words

  # return results
  [status, headers, body]

rescue Exception => e
  logger.error "Error serving asset #{path}:"
  logger.error "#{e.class.name}: #{e.message}"
  raise
end