Sha256: aab855fcc26ecd0cbb6349dd6e455dd6131af7cd0d4b77b6f8e0fcadeb45f53a

Contents?: true

Size: 1.1 KB

Versions: 10

Compression:

Stored size: 1.1 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', 'Connection' => 'Keep-Alive', 'Keep-Alive' => 'timeout=60', 'Cache-Control' => 'max-age=0' }, "#{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

10 entries across 10 versions & 1 rubygems

Version Path
frank-1.0.12 lib/frank/middleware/refresh.rb
frank-1.0.11 lib/frank/middleware/refresh.rb
frank-1.0.10 lib/frank/middleware/refresh.rb
frank-1.0.9 lib/frank/middleware/refresh.rb
frank-1.0.8 lib/frank/middleware/refresh.rb
frank-1.0.7 lib/frank/middleware/refresh.rb
frank-1.0.6 lib/frank/middleware/refresh.rb
frank-1.0.5 lib/frank/middleware/refresh.rb
frank-1.0.4 lib/frank/middleware/refresh.rb
frank-1.0.3 lib/frank/middleware/refresh.rb