Class: WWW::Mechanize::FileResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/www/mechanize/file_response.rb

Overview

Fake response for dealing with file:/// requests

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ FileResponse

Returns a new instance of FileResponse.



6
7
8
# File 'lib/www/mechanize/file_response.rb', line 6

def initialize(file_path)
  @file_path = file_path
end

Instance Method Details

#[](key) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/www/mechanize/file_response.rb', line 33

def [](key)
  return nil unless key.downcase == 'content-type'
  return 'text/html' if directory?
  return 'text/html' if ['.html', '.xhtml'].any? { |extn|
    @file_path =~ /#{extn}$/
  }
  nil
end

#codeObject



22
23
24
# File 'lib/www/mechanize/file_response.rb', line 22

def code
  ::File.exists?(@file_path) ? 200 : 400
end

#content_lengthObject



26
27
28
29
# File 'lib/www/mechanize/file_response.rb', line 26

def content_length
  return dir_body.length if directory?
  ::File.exists?(@file_path) ? ::File.stat(@file_path).size : 0
end

#eachObject



42
43
# File 'lib/www/mechanize/file_response.rb', line 42

def each
end

#each_headerObject



31
# File 'lib/www/mechanize/file_response.rb', line 31

def each_header; end

#get_fields(key) ⇒ Object



45
46
47
# File 'lib/www/mechanize/file_response.rb', line 45

def get_fields(key)
  []
end

#read_bodyObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/www/mechanize/file_response.rb', line 10

def read_body
  if ::File.exists?(@file_path)
    if directory?
      yield dir_body
    else
      yield ::File.read(@file_path)
    end
  else
    yield ''
  end
end