Class: Assette::Server

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

Overview

Methods for finding files are ugly, should fix someday

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Server

Returns a new instance of Server.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/assette/server.rb', line 5

def initialize(env)
  @env = env
  @path = env["PATH_INFO"]
  
  @params = env["QUERY_STRING"].split('&').inject({}) do |resp,v|
    k,v = v.split('=')
    
    if v == '1' || v.nil?
      v = true
    elsif v == '0'
      v = false
    end
    
    resp[k.to_sym] = v
    resp
  end
  
  # Assette.config.asset_path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/assette/server.rb', line 4

def path
  @path
end

Class Method Details

.call(env) ⇒ Object



104
105
106
107
108
# File 'lib/assette/server.rb', line 104

def call(env)
  s = new(env)
  
  s.rack_resp
end

Instance Method Details

#content_typeObject



55
56
57
# File 'lib/assette/server.rb', line 55

def content_type
  path_mime_type ? path_mime_type.content_type : 'text/plain'
end

#find_compiled_fileObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/assette/server.rb', line 25

def find_compiled_file
  f = nil
  Assette.config.file_paths.each do |p|
    
    Assette::Reader.possible_targets(File.join(p,path)).each do |pp|
      Assette.logger.debug('Checking for compiled file') {pp}
      f = Assette::File.rack_resp_if_exists( pp, @params.merge(:env => @env) )
      Assette.logger.info("Found File") {"Compiled file at #{pp}"} if f
      break if f
    end
    
    break if f
  end
  
  f
end

#find_fileObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/assette/server.rb', line 59

def find_file
  f = nil
  
  Assette.config.file_paths.each do |p|
    new_path = File.join(p,path)
    if File.exist?(new_path)
      new_path = File.join(Dir.pwd,new_path)
      Assette.logger.info("Found File") {"Raw file at #{new_path}"}
      f = Rack::File.new(p).call(@env)
    end
    
    break if f
  end
  
  f
end

#has_registered_reader?Boolean

Returns:

  • (Boolean)


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

def has_registered_reader?
  !Assette::Reader::OUTPUTS[path_extension.to_sym].empty?
end

#looking_for_template?Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/assette/server.rb', line 76

def looking_for_template?
  m = path.match(/__templates\/(.+)/)
  m[1].gsub(/.js$/,'').split(':') if m
end

#path_extensionObject



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

def path_extension
  m = path.match(/\.(\w+)$/)
  m ? m[1] : :none
end

#path_mime_typeObject



51
52
53
# File 'lib/assette/server.rb', line 51

def path_mime_type
  @path_mime_type ||= MIME::Types.type_for(path).first
end

#rack_respObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/assette/server.rb', line 81

def rack_resp
  if templates = looking_for_template?
    set = TemplateSet.new(templates)
    return [200,{"Content-Type" => Reader::Js.mime_type.content_type},[set.compile]]
  end
  
  begin
    if has_registered_reader? && (f = find_compiled_file)
      f
    elsif f = find_file
      f
    else
      possible_paths = Assette.config.file_paths.collect do |p|
        File.join(Dir.pwd,p,path)
      end
      
      [404,{"Content-Type" => "text/plain"},["File Not Found\n#{possible_paths.join("\n")}"]]
    end  
  end
end