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.



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

def initialize(file_path)
  @file_path = file_path
  @uri       = nil
end

Instance Method Details

#[](key) ⇒ Object



35
36
37
38
39
40
41
42
# 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

#codeObject



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

def code
  File.exist?(@file_path) ? 200 : 404
end

#content_lengthObject



28
29
30
31
# 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

#eachObject



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

def each
end

#each_headerObject



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

def each_header; end

#get_fields(key) ⇒ Object



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

def get_fields(key)
  []
end

#http_versionObject



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

def http_version
  '0'
end

#messageObject



55
56
57
# File 'lib/mechanize/file_response.rb', line 55

def message
  File.exist?(@file_path) ? 'OK' : 'Not Found'
end

#read_bodyObject



11
12
13
14
15
16
17
18
19
20
21
22
# 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

#uriObject



59
60
61
# File 'lib/mechanize/file_response.rb', line 59

def uri
  @uri ||= URI "file://#{@file_path}"
end