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