lib/sup/util.rb in sup-0.1 vs lib/sup/util.rb in sup-0.2

- old
+ new

@@ -61,30 +61,38 @@ ## more monkeypatching! module RMail class EncodingUnsupportedError < StandardError; end class Message - def add_attachment fn + def add_file_attachment fn bfn = File.basename fn a = Message.new t = MIME::Types.type_for(bfn).first || MIME::Types.type_for("exe").first - a.header.add "Content-Disposition", "attachment; filename=#{bfn}" - a.header.add "Content-Type", "#{t.content_type}; name=#{bfn}" + a.header.add "Content-Disposition", "attachment; filename=#{bfn.to_s.inspect}" + a.header.add "Content-Type", "#{t.content_type}; name=#{bfn.to_s.inspect}" a.header.add "Content-Transfer-Encoding", t.encoding a.body = case t.encoding when "base64" [IO.read(fn)].pack "m" when "quoted-printable" [IO.read(fn)].pack "M" + when "7bit", "8bit" + IO.read(fn) else raise EncodingUnsupportedError, t.encoding end add_part a end + + def charset + if header.field?("content-type") && header.fetch("content-type") =~ /charset="?(.*?)"?(;|$)/ + $1 + end + end end end class Range ## only valid for integer ranges (unless I guess it's exclusive) @@ -121,25 +129,11 @@ klass = klass.superclass end ret end - ## takes a value which it yields and then returns, so that code - ## like: - ## - ## x = expensive_operation - ## log "got #{x}" - ## x - ## - ## now becomes: - ## - ## with(expensive_operation) { |x| log "got #{x}" } - ## - ## i'm sure there's pithy comment i could make here about the - ## superiority of lisp, but fuck lisp. - ## - ## addendum: apparently this is a "k combinator". whoda thunk it? + ## "k combinator" def returning x; yield x; x; end ## clone of java-style whole-method synchronization ## assumes a @mutex variable def synchronized *meth @@ -275,17 +269,17 @@ best end ## returns the maximum shared prefix of an array of strings ## optinally excluding a prefix - def shared_prefix exclude="" + def shared_prefix caseless=false, exclude="" return "" if empty? prefix = "" (0 ... first.length).each do |i| - c = first[i] - break unless all? { |s| s[i] == c } + c = (caseless ? first.downcase : first)[i] + break unless all? { |s| (caseless ? s.downcase : s)[i] == c } next if exclude[i] == c - prefix += c.chr + prefix += first[i].chr end prefix end def max_of