Class: BrwyRails::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/brwy_rails/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



3
4
5
# File 'lib/brwy_rails/middleware.rb', line 3

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/brwy_rails/middleware.rb', line 39

def call(env)
  req = Rack::Request.new env
  if is_target_js? req.path
    tmp_path = get_tmp_path(req.path)
    loop do
      if file_is_readable? tmp_path
        break
      else
        sleep 1
      end
    end
  end
  @app.call(env)
end

#file_is_readable?(path) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/brwy_rails/middleware.rb', line 31

def file_is_readable?(path)
  File.exists?(path) && File.size(path) > 0
end

#get_tmp_path(path) ⇒ Object



24
25
26
27
28
29
# File 'lib/brwy_rails/middleware.rb', line 24

def get_tmp_path path
  target_suffix = Rails.application.config.brwy_rails.target_suffix
  m = /assets\/(.*)#{target_suffix.sub(".", "\.")}/.match path
  name = m[1]
  "#{Rails.root.to_s}/#{Rails.application.config.brwy_rails.tmpdir}/#{name}#{target_suffix}.js"
end

#in_targets?(path) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
# File 'lib/brwy_rails/middleware.rb', line 17

def in_targets?(path)
  targets = Rails.application.config.brwy_rails.targets
  canditates = targets.map {|t| to_asset_path(t)}
  puts canditates
  canditates.any? {|c| path.start_with?(c)}
end

#is_asset_js?(path) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/brwy_rails/middleware.rb', line 7

def is_asset_js?(path)
  path.start_with?("/assets/") && File.extname(path) == ".js"
end

#is_target_js?(path) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/brwy_rails/middleware.rb', line 35

def is_target_js?(path)
  is_asset_js?(path) && in_targets?(path)
end

#to_asset_path(target) ⇒ Object



11
12
13
14
15
# File 'lib/brwy_rails/middleware.rb', line 11

def to_asset_path(target)
  target_suffix = Rails.application.config.brwy_rails.target_suffix
  extname = File.extname(target)
  "/assets/" + File.basename(target).sub(extname, "") + target_suffix
end