Class: Mechanize::FileResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/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.



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

def initialize(file_path)
  @file_path = file_path
end

Instance Method Details

#[](key) ⇒ Object



32
33
34
35
36
37
38
39
# 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 =~ /#{extn}$/
  }
  nil
end

#codeObject



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

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

#content_lengthObject



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

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

#eachObject



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

def each
end

#each_headerObject



30
# File 'lib/mechanize/file_response.rb', line 30

def each_header; end

#get_fields(key) ⇒ Object



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

def get_fields(key)
  []
end

#read_bodyObject



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

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