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
35
36
37
# File 'lib/formdata.rb', line 26

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

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

#content_typeObject



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

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

#eof?(*args) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/formdata.rb', line 53

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

#post_request(*args) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/formdata.rb', line 58

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



48
49
50
51
# File 'lib/formdata.rb', line 48

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

#sizeObject Also known as: length



43
44
45
# File 'lib/formdata.rb', line 43

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