Class: Mechanize::FileResponse
- Inherits:
-
Object
- Object
- Mechanize::FileResponse
- Defined in:
- lib/mechanize/file_response.rb
Overview
Fake response for dealing with file:/// requests
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
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.
9 10 11 12 |
# File 'lib/mechanize/file_response.rb', line 9 def initialize(file_path) @file_path = file_path @uri = nil end |
Instance Attribute Details
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
7 8 9 |
# File 'lib/mechanize/file_response.rb', line 7 def file_path @file_path end |
Instance Method Details
#[](key) ⇒ Object
38 39 40 41 42 43 44 45 |
# 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 |
#code ⇒ Object
27 28 29 |
# File 'lib/mechanize/file_response.rb', line 27 def code File.exist?(@file_path) ? 200 : 404 end |
#content_length ⇒ Object
31 32 33 34 |
# 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 |
#each ⇒ Object
47 48 |
# File 'lib/mechanize/file_response.rb', line 47 def each end |
#each_header ⇒ Object
36 |
# File 'lib/mechanize/file_response.rb', line 36 def each_header; end |
#get_fields(key) ⇒ Object
50 51 52 |
# File 'lib/mechanize/file_response.rb', line 50 def get_fields(key) [] end |
#http_version ⇒ Object
54 55 56 |
# File 'lib/mechanize/file_response.rb', line 54 def http_version '0' end |
#message ⇒ Object
58 59 60 |
# File 'lib/mechanize/file_response.rb', line 58 def File.exist?(@file_path) ? 'OK' : 'Not Found' end |
#read_body ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# 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 |
#uri ⇒ Object
62 63 64 |
# File 'lib/mechanize/file_response.rb', line 62 def uri @uri ||= URI "file://#{@file_path}" end |