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
- #uri ⇒ Object
Constructor Details
#initialize(file_path) ⇒ FileResponse
Returns a new instance of FileResponse.
6 7 8 9 |
# File 'lib/mechanize/file_response.rb', line 6 def initialize(file_path) @file_path = file_path @uri = nil end |
Instance Method Details
#[](key) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/mechanize/file_response.rb', line 35 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 |
#code ⇒ Object
24 25 26 |
# File 'lib/mechanize/file_response.rb', line 24 def code File.exist?(@file_path) ? 200 : 404 end |
#content_length ⇒ Object
28 29 30 31 |
# File 'lib/mechanize/file_response.rb', line 28 def content_length return dir_body.length if directory? File.exist?(@file_path) ? File.stat(@file_path).size : 0 end |
#each ⇒ Object
44 45 |
# File 'lib/mechanize/file_response.rb', line 44 def each end |
#each_header ⇒ Object
33 |
# File 'lib/mechanize/file_response.rb', line 33 def each_header; end |
#get_fields(key) ⇒ Object
47 48 49 |
# File 'lib/mechanize/file_response.rb', line 47 def get_fields(key) [] end |
#http_version ⇒ Object
51 52 53 |
# File 'lib/mechanize/file_response.rb', line 51 def http_version '0' end |
#message ⇒ Object
55 56 57 |
# File 'lib/mechanize/file_response.rb', line 55 def File.exist?(@file_path) ? 'OK' : 'Not Found' end |
#read_body ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/mechanize/file_response.rb', line 11 def read_body raise Mechanize::ResponseCodeError.new(self) unless File.exist? @file_path if directory? yield dir_body else open @file_path, 'rb' do |io| yield io.read end end end |
#uri ⇒ Object
59 60 61 |
# File 'lib/mechanize/file_response.rb', line 59 def uri @uri ||= URI "file://#{@file_path}" end |