Class: Rack::Zippy::ServeableFile
- Inherits:
-
Object
- Object
- Rack::Zippy::ServeableFile
- Defined in:
- lib/rack-zippy/serveable_file.rb
Instance Attribute Summary collapse
-
#full_path_info ⇒ Object
readonly
Returns the value of attribute full_path_info.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #cache_headers ⇒ Object
- #encoding_variants? ⇒ Boolean
- #gzipped? ⇒ Boolean
- #headers ⇒ Object
-
#initialize(options) ⇒ ServeableFile
constructor
A new instance of ServeableFile.
- #response_body ⇒ Object
Constructor Details
#initialize(options) ⇒ ServeableFile
Returns a new instance of ServeableFile.
7 8 9 10 11 12 13 14 |
# File 'lib/rack-zippy/serveable_file.rb', line 7 def initialize() raise ArgumentError.new(':has_encoding_variants option must be given') unless .has_key?(:has_encoding_variants) @path = [:path] @full_path_info = [:full_path_info] @has_encoding_variants = [:has_encoding_variants] @is_gzipped = [:is_gzipped] end |
Instance Attribute Details
#full_path_info ⇒ Object (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 |
#path ⇒ Object (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() asset_compiler = [:asset_compiler] path_info = [:path_info].chomp('/') return nil if asset_compiler.compiles?(path_info) asset_root = [: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 = [: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
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_headers ⇒ Object
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
100 101 102 |
# File 'lib/rack-zippy/serveable_file.rb', line 100 def encoding_variants? return @has_encoding_variants end |
#gzipped? ⇒ Boolean
104 105 106 |
# File 'lib/rack-zippy/serveable_file.rb', line 104 def gzipped? return @is_gzipped end |
#headers ⇒ Object
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_body ⇒ Object
45 46 47 |
# File 'lib/rack-zippy/serveable_file.rb', line 45 def response_body [::File.read(path)] end |