Sha256: d8b7fbbf2cf30b14f0eda5ac94f9670a13e3cdd65d4afe7d0209a0dd540d4fe1

Contents?: true

Size: 1.01 KB

Versions: 5

Compression:

Stored size: 1.01 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-1.0.2 lib/frank/middleware/refresh.rb
frank-1.0.1 lib/frank/middleware/refresh.rb
frank-1.0.0 lib/frank/middleware/refresh.rb
frank-0.4.1 lib/frank/middleware/refresh.rb
frank-0.4.0 lib/frank/middleware/refresh.rb