lib/rack/logs/viewer.rb in rack-logs-0.0.3 vs lib/rack/logs/viewer.rb in rack-logs-0.0.4

- old
+ new

@@ -6,11 +6,16 @@ @config = config end attr_reader :config def call env - [200, headers, joined_logs] + contents = joined_logs(env.fetch('PATH_INFO','/')) + if contents.empty? + [404, headers, ['No Such File']] + else + [200, headers, contents] + end end private def headers @@ -25,10 +30,14 @@ def initialize filenames, lines @filenames = filenames @lines = lines end + def empty? + @filenames.empty? + end + def each &block @filenames.each do |filename| block.call "## #{filename}\n\n" ::File.open(filename) do |file| total = 0 @@ -44,22 +53,24 @@ end end end end - def joined_logs - JoinedFiles.new files, @config.lines + def joined_logs path + JoinedFiles.new files(path), @config.lines end def logs files.inject({}) do |hash, filename| hash[filename] = ::File.read(filename) hash end end - def files - Dir[@config.log_dir+'/'+@config.pattern] + def files path + Dir[@config.log_dir+'/'+@config.pattern].select do |filename| + filename =~ Regexp.new( @config.log_dir + path ) + end end end end end