Class: Rack::Zippy::ServeableFile

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-zippy/serveable_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ServeableFile

Returns a new instance of ServeableFile.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
# File 'lib/rack-zippy/serveable_file.rb', line 7

def initialize(options)
  raise ArgumentError.new(':has_encoding_variants option must be given') unless options.has_key?(:has_encoding_variants)

  @path = options[:path]
  @full_path_info = options[:full_path_info]
  @has_encoding_variants = options[:has_encoding_variants]
  @is_gzipped = options[:is_gzipped]
end

Instance Attribute Details

#full_path_infoObject (readonly)

Returns the value of attribute full_path_info.



5
6
7
# File 'lib/rack-zippy/serveable_file.rb', line 5

def full_path_info
  @full_path_info
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/rack-zippy/serveable_file.rb', line 5

def path
  @path
end

Class Method Details

.find_first(options) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rack-zippy/serveable_file.rb', line 49

def self.find_first(options)
  asset_compiler = options[:asset_compiler]
  path_info = options[:path_info].chomp('/')
  
  return nil if asset_compiler.compiles?(path_info)

  asset_root = options[:asset_root]

  candidate_path_infos = []
  if !path_info.empty?
    candidate_path_infos << path_info
    candidate_path_infos << "#{path_info}#{DEFAULT_STATIC_EXTENSION}"
  end
  candidate_path_infos << "#{path_info}/index#{DEFAULT_STATIC_EXTENSION}"

  file_path = nil

  full_path_info = candidate_path_infos.find do |candidate_path_info|
    file_path = ::File.join(asset_root, candidate_path_info)
    readable_file?(file_path)
  end

  return nil if full_path_info.nil? || !has_static_extension?(full_path_info)

  include_gzipped = options[:include_gzipped]

  gzipped_file_path = "#{file_path}.gz"
  gzipped_file_present = readable_file?(gzipped_file_path)

  has_encoding_variants = gzipped_file_present

  if include_gzipped && gzipped_file_present
    return ServeableFile.new(
        :path => gzipped_file_path,
        :full_path_info => full_path_info,
        :has_encoding_variants => has_encoding_variants,
        :is_gzipped => true
    )
  end

  return ServeableFile.new(
      :path => file_path,
      :full_path_info => full_path_info,
      :has_encoding_variants => has_encoding_variants
  )
end

.has_static_extension?(path) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/rack-zippy/serveable_file.rb', line 96

def self.has_static_extension?(path)
  path =~ AssetServer::STATIC_EXTENSION_REGEX
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



108
109
110
111
112
113
114
115
116
# File 'lib/rack-zippy/serveable_file.rb', line 108

def ==(other)
  return false if other.nil?
  return true if self.equal?(other)
  return self.class == other.class &&
    self.gzipped? == other.gzipped? &&
    self.encoding_variants? == other.encoding_variants? &&
    self.path == other.path &&
    self.full_path_info == other.full_path_info
end

#cache_headersObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rack-zippy/serveable_file.rb', line 27

def cache_headers
  case full_path_info
    when PRECOMPILED_ASSETS_SUBDIR_REGEX
      lifetime = :year
      last_modified = CACHE_FRIENDLY_LAST_MODIFIED
    when '/favicon.ico'
      lifetime = :month
      last_modified = CACHE_FRIENDLY_LAST_MODIFIED
    else
      lifetime = :day
  end

  headers = { 'Cache-Control' => "public, max-age=#{SECONDS_IN[lifetime]}" }
  headers['Last-Modified'] = last_modified if last_modified

  return headers
end

#encoding_variants?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/rack-zippy/serveable_file.rb', line 100

def encoding_variants?
  return @has_encoding_variants
end

#gzipped?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/rack-zippy/serveable_file.rb', line 104

def gzipped?
  return @is_gzipped
end

#headersObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/rack-zippy/serveable_file.rb', line 16

def headers
  headers = { 'Content-Type'  => Rack::Mime.mime_type(::File.extname(full_path_info)) }
  headers.merge! cache_headers

  headers['Vary'] = 'Accept-Encoding' if encoding_variants?
  headers['Content-Encoding'] = 'gzip' if gzipped?

  headers['Content-Length'] = ::File.size(path).to_s
  return headers
end

#response_bodyObject



45
46
47
# File 'lib/rack-zippy/serveable_file.rb', line 45

def response_body
  [::File.read(path)]
end