Class: Rhaiker::MultipartBuilder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rhaiker/multipart_builder.rb

Defined Under Namespace

Classes: MultipartData, MultipartFileData

Constant Summary collapse

EOL =
"\r\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(boundary = 'MultipartBuilder_boundary') ⇒ MultipartBuilder

Returns a new instance of MultipartBuilder.



18
19
20
21
# File 'lib/rhaiker/multipart_builder.rb', line 18

def initialize(boundary = 'MultipartBuilder_boundary')
  @boundary = boundary
  @data = {}
end

Instance Attribute Details

#boundaryObject

Returns the value of attribute boundary.



16
17
18
# File 'lib/rhaiker/multipart_builder.rb', line 16

def boundary
  @boundary
end

Instance Method Details

#buildObject Also known as: to_s



23
24
25
26
27
28
29
30
31
32
# File 'lib/rhaiker/multipart_builder.rb', line 23

def build
  return '' if @data.empty?
  body = ''
  @data.each do |name, value|
    body << '--' << @boundary << EOL
    body << value.to_s
  end
  body << '--' << @boundary << '--' << EOL
  return body
end

#eachObject



34
35
36
37
38
# File 'lib/rhaiker/multipart_builder.rb', line 34

def each
  @data.each do |name, value|
    yield(name, value.value)
  end
end

#fetch(name) ⇒ Object



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

def fetch(name)
  @data.fetch(name).value
end

#store(name, value) ⇒ Object



44
45
46
# File 'lib/rhaiker/multipart_builder.rb', line 44

def store(name, value)
  @data.store(name, MultipartData.new(value, name))
end

#store_file(name, fileobj) ⇒ Object



48
49
50
# File 'lib/rhaiker/multipart_builder.rb', line 48

def store_file(name, fileobj)
  @data.store(name, MultipartFileData.new(fileobj, name))
end

#update(other) ⇒ Object



52
53
54
55
56
# File 'lib/rhaiker/multipart_builder.rb', line 52

def update(other)
  other.each do |name, value|
    store(name, value)
  end
end