Class: FormData::FormData

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFormData

Returns a new instance of FormData.



15
16
17
18
19
20
21
22
23
24
# File 'lib/formdata.rb', line 15

def initialize
  @id = SecureRandom.hex(10).freeze

  @buffer = Tempfile.new(@id)
  @buffer.binmode

  @boundary = "----RubyFormBoundary#{@id}".freeze

  @finalized = false
end

Instance Attribute Details

#boundaryObject (readonly)

Returns the value of attribute boundary.



13
14
15
# File 'lib/formdata.rb', line 13

def boundary
  @boundary
end

Instance Method Details

#append(name, value = nil, opts = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/formdata.rb', line 26

def append(name, value = nil, opts = {})
  name.each { |n, v| append(n, v) } if name.is_a?(Hash)

  if value.respond_to?(:read)
    append_file(name, value, opts)
  else
    append_text(name, value)
  end
end

#content_typeObject



36
37
38
# File 'lib/formdata.rb', line 36

def content_type
  "multipart/form-data; boundary=#{@boundary}"
end

#eof?(*args) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/formdata.rb', line 50

def eof?(*args)
  return false unless @finalized
  @buffer.eof?(*args)
end

#post_request(*args) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/formdata.rb', line 55

def post_request(*args)
  req = Net::HTTP::Post.new(*args)
  req.content_type = content_type
  req.content_length = size
  req.body_stream = self

  req
end

#read(*args) ⇒ Object



45
46
47
48
# File 'lib/formdata.rb', line 45

def read(*args)
  finalize! unless @finalized
  @buffer.read(*args)
end

#sizeObject Also known as: length



40
41
42
# File 'lib/formdata.rb', line 40

def size
  @finalized ? @buffer.size : @buffer.size + epilogue.size
end