Parent

Mechanize::FileResponse

Fake response for dealing with file:/// requests

Public Class Methods

new(file_path) click to toggle source
   # File lib/mechanize/file_response.rb, line 6
6:   def initialize(file_path)
7:     @file_path = file_path
8:     @uri       = nil
9:   end

Public Instance Methods

[](key) click to toggle source
    # File lib/mechanize/file_response.rb, line 35
35:   def [](key)
36:     return nil if key.casecmp('Content-Type') != 0
37:     return 'text/html' if directory?
38:     return 'text/html' if ['.html', '.xhtml'].any? { |extn|
39:       @file_path.end_with?(extn)
40:     }
41:     nil
42:   end
code() click to toggle source
    # File lib/mechanize/file_response.rb, line 24
24:   def code
25:     File.exist?(@file_path) ? 200 : 404
26:   end
content_length() click to toggle source
    # File lib/mechanize/file_response.rb, line 28
28:   def content_length
29:     return dir_body.length if directory?
30:     File.exist?(@file_path) ? File.stat(@file_path).size : 0
31:   end
each() click to toggle source
    # File lib/mechanize/file_response.rb, line 44
44:   def each
45:   end
each_header() click to toggle source
    # File lib/mechanize/file_response.rb, line 33
33:   def each_header; end
get_fields(key) click to toggle source
    # File lib/mechanize/file_response.rb, line 47
47:   def get_fields(key)
48:     []
49:   end
http_version() click to toggle source
    # File lib/mechanize/file_response.rb, line 51
51:   def http_version
52:     '0'
53:   end
message() click to toggle source
    # File lib/mechanize/file_response.rb, line 55
55:   def message
56:     File.exist?(@file_path) ? 'OK' : 'Not Found'
57:   end
read_body() click to toggle source
    # File lib/mechanize/file_response.rb, line 11
11:   def read_body
12:     raise Mechanize::ResponseCodeError.new(self) unless
13:       File.exist? @file_path
14: 
15:     if directory?
16:       yield dir_body
17:     else
18:       open @file_path, 'rb' do |io|
19:         yield io.read
20:       end
21:     end
22:   end
uri() click to toggle source
    # File lib/mechanize/file_response.rb, line 59
59:   def uri
60:     @uri ||= URI "file://#{@file_path}"
61:   end

Private Instance Methods

dir_body() click to toggle source
    # File lib/mechanize/file_response.rb, line 65
65:   def dir_body
66:     body = ]<html><body>]
67:     body.concat Dir[File.join(@file_path, '*')].map { |f|
68:       "<a href=\"file://#{f}\">#{File.basename(f)}</a>"
69:     }
70:     body << ]</body></html>]
71: 
72:     body = body.join "\n"
73:     body.force_encoding Encoding::BINARY if body.respond_to? :force_encoding
74:     body
75:   end
directory?() click to toggle source
    # File lib/mechanize/file_response.rb, line 77
77:   def directory?
78:     File.directory?(@file_path)
79:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.