class 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 5
def initialize(file_path)
  @file_path = file_path
end

Public Instance Methods

[](key) click to toggle source
# 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 =~ %r#{extn}$/
  }
  nil
end
code() click to toggle source
# File lib/mechanize/file_response.rb, line 21
def code
  File.exist?(@file_path) ? 200 : 404
end
content_length() click to toggle source
# 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() click to toggle source
# File lib/mechanize/file_response.rb, line 41
def each
end
each_header() click to toggle source
# File lib/mechanize/file_response.rb, line 30
def each_header; end
get_fields(key) click to toggle source
# File lib/mechanize/file_response.rb, line 44
def get_fields(key)
  []
end
http_version() click to toggle source
# File lib/mechanize/file_response.rb, line 48
def http_version
  '0'
end
message() click to toggle source
# File lib/mechanize/file_response.rb, line 52
def message
  File.exist?(@file_path) ? 'OK' : 'Not Found'
end
read_body() { |dir_body| ... } click to toggle source
# 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