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 6
def initialize(file_path)
  @file_path = file_path
  @uri       = nil
end

Public Instance Methods

[](key) click to toggle source
# 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() click to toggle source
# File lib/mechanize/file_response.rb, line 24
def code
  File.exist?(@file_path) ? 200 : 404
end
content_length() click to toggle source
# 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() click to toggle source
# File lib/mechanize/file_response.rb, line 44
def each
end
each_header() click to toggle source
# File lib/mechanize/file_response.rb, line 33
def each_header; end
get_fields(key) click to toggle source
# File lib/mechanize/file_response.rb, line 47
def get_fields(key)
  []
end
http_version() click to toggle source
# File lib/mechanize/file_response.rb, line 51
def http_version
  '0'
end
message() click to toggle source
# File lib/mechanize/file_response.rb, line 55
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 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() click to toggle source
# File lib/mechanize/file_response.rb, line 59
def uri
  @uri ||= URI "file://#{@file_path}"
end