Class: Mechanize::FileResponse

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

Overview

Fake response for dealing with file:/// requests

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ FileResponse

Returns a new instance of FileResponse.



9
10
11
12
# File 'lib/mechanize/file_response.rb', line 9

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

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



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

def file_path
  @file_path
end

Instance Method Details

#[](key) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/mechanize/file_response.rb', line 38

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



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

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

#content_lengthObject



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

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

#eachObject



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

def each
end

#each_headerObject



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

def each_header; end

#get_fields(key) ⇒ Object



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

def get_fields(key)
  []
end

#http_versionObject



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

def http_version
  '0'
end

#messageObject



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

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

#read_bodyObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mechanize/file_response.rb', line 14

def read_body
  raise Mechanize::ResponseCodeError.new(self) unless
    File.exist? @file_path

  if directory?
    yield dir_body
  else
    ::File.open(@file_path, 'rb') do |io|
      yield io.read
    end
  end
end

#uriObject



62
63
64
# File 'lib/mechanize/file_response.rb', line 62

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