Class: HTTPMultipart::Part
- Inherits:
-
Object
- Object
- HTTPMultipart::Part
- Includes:
- Net::HTTPHeader
- Defined in:
- lib/jasper-client/http_multipart.rb
Overview
An RFC2387 multipart part.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
Instance Method Summary collapse
-
#content_id ⇒ Object
get the content id.
-
#content_subtype ⇒ Object
Content type subtype.
-
#content_supertype ⇒ Object
Content type supertype (for text/html, this would be ‘text’).
-
#initialize(part_str) ⇒ Part
constructor
Initialize this response part.
-
#suggested_filename ⇒ Object
The suggested filename is the content_id witout the surrounding <> characters.
-
#write_to_file(name = :internal) ⇒ Object
Write the content from this part to a file having name.
Constructor Details
#initialize(part_str) ⇒ Part
Initialize this response part.
30 31 32 33 34 |
# File 'lib/jasper-client/http_multipart.rb', line 30 def initialize(part_str) h,b = part_str.split("\r\n\r\n", 2) initialize_http_header Hash[ *h.split("\r\n").map { |hdr| hdr.split(/:\s*/, 2) }.flatten ] @body = b end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
28 29 30 |
# File 'lib/jasper-client/http_multipart.rb', line 28 def body @body end |
Instance Method Details
#content_id ⇒ Object
get the content id. Each part has a content-id It’s typical to use this as a basis for a fhile name.
54 55 56 |
# File 'lib/jasper-client/http_multipart.rb', line 54 def content_id to_hash.fetch('content-id') end |
#content_subtype ⇒ Object
Content type subtype. (for text/html, this woudl be ‘html’)
42 43 44 |
# File 'lib/jasper-client/http_multipart.rb', line 42 def content_subtype content_type.split('/')[1] end |
#content_supertype ⇒ Object
Content type supertype (for text/html, this would be ‘text’)
37 38 39 |
# File 'lib/jasper-client/http_multipart.rb', line 37 def content_supertype content_type.split('/')[0] end |
#suggested_filename ⇒ Object
The suggested filename is the content_id witout the surrounding <> characters. the extension is derived from the mime-subtype.
48 49 50 |
# File 'lib/jasper-client/http_multipart.rb', line 48 def suggested_filename "%s.%s" % [ content_id.first.gsub(/<|>/, ''), content_subtype ] end |
#write_to_file(name = :internal) ⇒ Object
Write the content from this part to a file having name.
59 60 61 62 63 64 65 |
# File 'lib/jasper-client/http_multipart.rb', line 59 def write_to_file(name = :internal) name = suggested_filename if :internal == name open(name, 'w') do |fh| fh.write self.body end end |