File: file.rb

package info (click to toggle)
ruby-mechanize 2.7.6-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,480 kB
  • sloc: ruby: 11,380; makefile: 5; sh: 4
file content (93 lines) | stat: -rw-r--r-- 2,292 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
##
# This is the base class for the Pluggable Parsers.  If Mechanize cannot find
# an appropriate class to use for the content type, this class will be used.
# For example, if you download an image/jpeg, Mechanize will not know how to
# parse it, so this class will be instantiated.
#
# This is a good class to use as the base class for building your own
# pluggable parsers.
#
# == Example
#
#   require 'mechanize'
#
#   agent = Mechanize.new
#   agent.get('http://example.com/foo.jpg').class  #=> Mechanize::File

class Mechanize::File

  include Mechanize::Parser

  ##
  # The HTTP response body, the raw file contents

  attr_accessor :body

  ##
  # The filename for this file based on the content-disposition of the
  # response or the basename of the URL

  attr_accessor :filename

  alias content body

  ##
  # Creates a new file retrieved from the given +uri+ and +response+ object.
  # The +body+ is the HTTP response body and +code+ is the HTTP status.

  def initialize uri = nil, response = nil, body = nil, code = nil
    @uri  = uri
    @body = body
    @code = code

    @full_path = false unless defined? @full_path

    fill_header response
    extract_filename

    yield self if block_given?
  end

  ##
  # Use this method to save the content of this object to +filename+.
  # returns the filename
  #
  #   file.save 'index.html'
  #   file.save 'index.html' # saves to index.html.1
  #
  #   uri = URI 'http://localhost/test.html'
  #   file = Mechanize::File.new uri, nil, ''
  #   filename = file.save  # saves to test.html
  #   puts filename         # test.html

  def save filename = nil
    filename = find_free_name filename
    save! filename
  end

  alias save_as save

  ##
  # Use this method to save the content of this object to +filename+.
  # This method will overwrite any existing filename that exists with the
  # same name.
  # returns the filename
  #
  #   file.save 'index.html'
  #   file.save! 'index.html' # overwrite original file
  #   filename = file.save! 'index.html' # overwrite original file with filename 'index.html'

  def save! filename = nil
    filename ||= @filename
    dirname = File.dirname filename
    FileUtils.mkdir_p dirname

    ::File.open(filename, 'wb')do |f|
      f.write body
    end

    filename
  end

end