Class: Part

Inherits:
Struct
  • Object
show all
Defined in:
lib/multipart_body/part.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Part

Returns a new instance of Part.



2
3
4
5
6
7
8
# File 'lib/multipart_body/part.rb', line 2

def initialize(*args)
  if args.flatten.first.is_a? Hash
    from_hash(args.flatten.first)
  elsif args.length > 0
    from_args(*args)
  end
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



1
2
3
# File 'lib/multipart_body/part.rb', line 1

def body
  @body
end

#content_dispositionObject

Returns the value of attribute content_disposition

Returns:

  • (Object)

    the current value of content_disposition



1
2
3
# File 'lib/multipart_body/part.rb', line 1

def content_disposition
  @content_disposition
end

#content_typeObject

Returns the value of attribute content_type

Returns:

  • (Object)

    the current value of content_type



1
2
3
# File 'lib/multipart_body/part.rb', line 1

def content_type
  @content_type
end

#encodingObject

Returns the value of attribute encoding

Returns:

  • (Object)

    the current value of encoding



1
2
3
# File 'lib/multipart_body/part.rb', line 1

def encoding
  @encoding
end

#filenameObject

Returns the value of attribute filename

Returns:

  • (Object)

    the current value of filename



1
2
3
# File 'lib/multipart_body/part.rb', line 1

def filename
  @filename
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



1
2
3
# File 'lib/multipart_body/part.rb', line 1

def name
  @name
end

Instance Method Details

#encoded_bodyObject

TODO: Implement encodings



39
40
41
42
43
44
45
46
# File 'lib/multipart_body/part.rb', line 39

def encoded_body
  case encoding
  when nil
    body
  else
    raise "Encodings have not been implemented"
  end
end

#from_args(name, body, filename = nil) ⇒ Object



21
22
23
# File 'lib/multipart_body/part.rb', line 21

def from_args(name, body, filename=nil)
  self.from_hash(:name => name, :body => body, :filename => filename)
end

#from_hash(hash) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/multipart_body/part.rb', line 10

def from_hash(hash)
  hash.each_pair do |k, v|
    if k.to_s == 'body' && (v.is_a?(File) || v.is_a?(Tempfile))
      self[k] = v.read
      self['filename'] = File.basename(v)
    else
      self[k] = v
    end
  end
end

#headerObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/multipart_body/part.rb', line 25

def header
  header = ""
  if content_disposition || name
    header << "Content-Disposition: #{content_disposition || 'form-data'}"
    header << "; name=\"#{name}\"" if name && !content_disposition
    header << "; filename=\"#{filename}\"" if filename && !content_disposition
    header << "\r\n"
  end
  header << "Content-Type: #{content_type}\r\n" if content_type
  header << "Content-Transfer-Encoding: #{encoding}\r\n" if encoding
  header
end

#to_sObject



48
49
50
# File 'lib/multipart_body/part.rb', line 48

def to_s
  "#{header}\r\n#{encoded_body}"
end