Sha256: 0b655145eb7d8c4211b0f3a31637ca1debaa7b40e0a45cc2bb54ca0ecc04cbf3

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

module Frank
  module Middleware
    class Refresh
 
      def initialize(app, options={})
        @app     = app
        @folders = options[:watch]
      end
    
      # catch __refrank__ path and
      # return the most recent timestamp
      def call(env)
        request = Rack::Request.new(env)
        if request.path_info.match /^\/__refresh__$/
          [ 200, { 'Content-Type' => 'application/json' }, "[#{get_mtime}]" ]
        else
          @app.call(env)
        end
      
      end
    
      private
    
      # build list of mtimes for watched files
      # return the most recent
      def get_mtime
        pwd        = Dir.pwd
        timestamps = []
        helpers    = File.join(pwd, 'helpers.rb')
        
        timestamps << File.mtime(helpers).to_i if File.exist? helpers
        @folders.each do |folder|
          Dir[File.join(pwd, folder, '**/*.*')].each do |found|
            timestamps << File.mtime(found).to_i unless File.directory?(found)
          end
        end
        timestamps.sort.last
      end
      
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
frank-0.3.2 lib/frank/middleware/refresh.rb
frank-0.3.1 lib/frank/middleware/refresh.rb
frank-0.3.0 lib/frank/middleware/refresh.rb
frank-0.3.0.beta2 lib/frank/middleware/refresh.rb
frank-0.3.0.beta lib/frank/middleware/refresh.rb