Class: HTTPMultipart::Part

Inherits:
Object
  • Object
show all
Includes:
Net::HTTPHeader
Defined in:
lib/jasper-client/http_multipart.rb

Overview

An RFC2387 multipart part.

www.faqs.org/rfcs/rfc2387.html

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject (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_idObject

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_subtypeObject

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_supertypeObject

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_filenameObject

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