class Mechanize::FileResponse
Fake response for dealing with file:/// requests
Attributes
Public Class Methods
Source
# File lib/mechanize/file_response.rb, line 9 def initialize(file_path) @file_path = file_path @uri = nil end
Public Instance Methods
Source
# File lib/mechanize/file_response.rb, line 38 def [](key) return nil if key.casecmp('Content-Type') != 0 return 'text/html' if directory? return 'text/html' if ['.html', '.xhtml'].any? { |extn| @file_path.end_with?(extn) } nil end
Source
# File lib/mechanize/file_response.rb, line 27 def code File.exist?(@file_path) ? 200 : 404 end
Source
# File lib/mechanize/file_response.rb, line 31 def content_length return dir_body.length if directory? File.exist?(@file_path) ? File.stat(@file_path).size : 0 end
Source
# File lib/mechanize/file_response.rb, line 58 def message File.exist?(@file_path) ? 'OK' : 'Not Found' end
Source
# File lib/mechanize/file_response.rb, line 14 def read_body raise Mechanize::ResponseCodeError.new(self) unless File.exist? @file_path if directory? yield dir_body else ::File.open(@file_path, 'rb') do |io| yield io.read end end end
Source
# File lib/mechanize/file_response.rb, line 62 def uri @uri ||= URI "file://#{@file_path}" end
Private Instance Methods
Source
# File lib/mechanize/file_response.rb, line 68 def dir_body body = %w[<html><body>] body.concat Dir[File.join(@file_path, '*')].map { |f| "<a href=\"file://#{f}\">#{File.basename(f)}</a>" } body << %w[</body></html>] body.join("\n").force_encoding(Encoding::BINARY) end
Source
# File lib/mechanize/file_response.rb, line 78 def directory? File.directory?(@file_path) end