Class | WWW::Mechanize::FileResponse |
In: |
lib/www/mechanize/file_response.rb
|
Parent: | Object |
Fake response for dealing with file:/// requests
# File lib/www/mechanize/file_response.rb, line 6 6: def initialize(file_path) 7: @file_path = file_path 8: end
# File lib/www/mechanize/file_response.rb, line 33 33: def [](key) 34: return nil unless key.downcase == 'content-type' 35: return 'text/html' if directory? 36: return 'text/html' if ['.html', '.xhtml'].any? { |extn| 37: @file_path =~ /#{extn}$/ 38: } 39: nil 40: end
# File lib/www/mechanize/file_response.rb, line 22 22: def code 23: ::File.exists?(@file_path) ? 200 : 400 24: end
# File lib/www/mechanize/file_response.rb, line 26 26: def content_length 27: return dir_body.length if directory? 28: ::File.exists?(@file_path) ? ::File.stat(@file_path).size : 0 29: end
# File lib/www/mechanize/file_response.rb, line 10 10: def read_body 11: if ::File.exists?(@file_path) 12: if directory? 13: yield dir_body 14: else 15: yield ::File.read(@file_path) 16: end 17: else 18: yield '' 19: end 20: end
# File lib/www/mechanize/file_response.rb, line 50 50: def dir_body 51: '<html><body>' + 52: Dir[::File.join(@file_path, '*')].map { |f| 53: "<a href=\"file://#{f}\">#{::File.basename(f)}</a>" 54: }.join("\n") + '</body></html>' 55: end