Class: Mechanize::FileResponse
- Inherits:
-
Object
- Object
- Mechanize::FileResponse
- Defined in:
- lib/mechanize/file_response.rb
Overview
Fake response for dealing with file:/// requests
Instance Method Summary collapse
- #[](key) ⇒ Object
- #code ⇒ Object
- #content_length ⇒ Object
- #each ⇒ Object
- #each_header ⇒ Object
- #get_fields(key) ⇒ Object
- #http_version ⇒ Object
-
#initialize(file_path) ⇒ FileResponse
constructor
A new instance of FileResponse.
- #message ⇒ Object
- #read_body ⇒ Object
Constructor Details
#initialize(file_path) ⇒ FileResponse
Returns a new instance of FileResponse.
5 6 7 |
# File 'lib/mechanize/file_response.rb', line 5 def initialize(file_path) @file_path = file_path end |
Instance Method Details
#[](key) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/mechanize/file_response.rb', line 32 def [](key) return nil unless key.downcase == 'content-type' return 'text/html' if directory? return 'text/html' if ['.html', '.xhtml'].any? { |extn| @file_path =~ /#{extn}$/ } nil end |
#code ⇒ Object
21 22 23 |
# File 'lib/mechanize/file_response.rb', line 21 def code File.exist?(@file_path) ? 200 : 404 end |
#content_length ⇒ Object
25 26 27 28 |
# File 'lib/mechanize/file_response.rb', line 25 def content_length return dir_body.length if directory? File.exist?(@file_path) ? File.stat(@file_path).size : 0 end |
#each ⇒ Object
41 42 |
# File 'lib/mechanize/file_response.rb', line 41 def each end |
#each_header ⇒ Object
30 |
# File 'lib/mechanize/file_response.rb', line 30 def each_header; end |
#get_fields(key) ⇒ Object
44 45 46 |
# File 'lib/mechanize/file_response.rb', line 44 def get_fields(key) [] end |
#http_version ⇒ Object
48 49 50 |
# File 'lib/mechanize/file_response.rb', line 48 def http_version '0' end |
#message ⇒ Object
52 53 54 |
# File 'lib/mechanize/file_response.rb', line 52 def File.exist?(@file_path) ? 'OK' : 'Not Found' end |
#read_body ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mechanize/file_response.rb', line 9 def read_body raise Mechanize::ResponseCodeError, self unless File.exist? @file_path if directory? yield dir_body else open @file_path, 'rb' do |io| yield io.read end end end |