Class: Part
- Inherits:
-
Struct
- Object
- Struct
- Part
- Defined in:
- lib/multipart_body/part.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#content_disposition ⇒ Object
Returns the value of attribute content_disposition.
-
#content_type ⇒ Object
Returns the value of attribute content_type.
-
#encoding ⇒ Object
Returns the value of attribute encoding.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#encoded_body ⇒ Object
TODO: Implement encodings.
- #from_args(name, body, filename = nil) ⇒ Object
- #from_hash(hash) ⇒ Object
- #header ⇒ Object
-
#initialize(*args) ⇒ Part
constructor
A new instance of Part.
- #to_s ⇒ Object
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
#body ⇒ Object
Returns the value of attribute body
1 2 3 |
# File 'lib/multipart_body/part.rb', line 1 def body @body end |
#content_disposition ⇒ Object
Returns the value of attribute content_disposition
1 2 3 |
# File 'lib/multipart_body/part.rb', line 1 def content_disposition @content_disposition end |
#content_type ⇒ Object
Returns the value of attribute content_type
1 2 3 |
# File 'lib/multipart_body/part.rb', line 1 def content_type @content_type end |
#encoding ⇒ Object
Returns the value of attribute encoding
1 2 3 |
# File 'lib/multipart_body/part.rb', line 1 def encoding @encoding end |
#filename ⇒ Object
Returns the value of attribute filename
1 2 3 |
# File 'lib/multipart_body/part.rb', line 1 def filename @filename end |
#name ⇒ Object
Returns the value of attribute name
1 2 3 |
# File 'lib/multipart_body/part.rb', line 1 def name @name end |
Instance Method Details
#encoded_body ⇒ Object
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 |
#header ⇒ Object
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_s ⇒ Object
48 49 50 |
# File 'lib/multipart_body/part.rb', line 48 def to_s "#{header}\r\n#{encoded_body}" end |