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.exist?(@file_path) ? 200 : 404
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.exist?(@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

#http_versionObject



48
49
50
# File 'lib/mechanize/file_response.rb', line 48

def http_version
  '0'
end

#messageObject



52
53
54
# File 'lib/mechanize/file_response.rb', line 52

def message
  File.exist?(@file_path) ? 'OK' : 'Not Found'
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
  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