1

My goal is to find the place where save_attachments_to is called in this gmail gem readme example:

folder = "/where/ever"
gmail.mailbox("Faxes").emails do |email|
  if !email.message.attachments.empty?
    email.message.save_attachments_to(folder)
  end
end

I run a "puts email.message.attachments.methods and a "email.message.attachments.class" in the loop:

Mail::AttachmentsList
guess_encoding
set_mime_type
inspect

Then I run a "puts email.message.methods and a "puts email.message.class" for good measure. The example method call is not in the list.

So I go diving into https://github.com/nu7hatch/gmail/blob/master/lib/gmail/message.rb.

No methods are defined there either, but I notice that mime/message is defined, so I go over there to look at its methods: http://rubydoc.info/gems/mime/0.1/MIME/Message

There is no save_attachments_to method here either.

Where the deuce is this method? The gmail gem does not define attachment methods, so the whole thing must be inherited from somewhere. Where? And where's the call that inherits it?

4

2 に答える 2

2

見つからないのは存在しないからです。理由はわかりません。私は宝石をダウンロードして、しばらくの間それで遊んだirb

1.9.3-p194 :066 > x.message.attachments
 => [#<Mail::Part:70234804200840, Multipart: false, Headers: <Content-Type: application/vnd.ms-excel; name="MVBINGO.xls">, <Content-Transfer-Encoding: base64>, <Content-Disposition: attachment; filename="MVBINGO.xls">, <Content-Description: MVBINGO.xls>>] 
1.9.3-p194 :063 > x.message.save_attachments_to(folder)
NoMethodError: undefined method `save_attachments_to' for #<Mail::Message:0x007fc1a3875818>
    from /Users/Qsario/.rvm/gems/ruby-1.9.3-p194/gems/mail-2.4.4/lib/mail/message.rb:1289:in `method_missing'
    from (irb):63
    from /Users/Qsario/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'

あまり役に立ちません。通常、あなたは次のようなことをすることができます

puts my_obj.method(:some_method_name).source_location

しかし、問題の方法が存在しない場合、それはあまり役に立ちません。編集:今私が見ると、この正確なバグはすでに彼らの課題追跡システムにあります。存在しない関数を実装するためのコードを投稿した人もいます。たとえば、abによるこのコードは次のとおりです。

folder = Dir.pwd # for example
email.message.attachments.each do |f|
  File.write(File.join(folder, f.filename), f.body.decoded)
end
于 2012-08-05T08:46:24.107 に答える
2

健全性チェック Qsario に感謝します。:-)

Ruby 1.9.3 (1.9.3-p194) で動作するコードは次のとおりです。

gmail = Gmail.connect('username@gmail.com', 'pass') 
gmail.inbox.emails.each do |email|
  email.message.attachments.each do |f|
    File.write(File.join(local_path, f.filename), f.body.decoded)
  end
end

1.9.2 (1.9.2-p320) および 1.9.3 (1.9.3-p194) で動作するコードは次のとおりです。

gmail = Gmail.connect('username@gmail.com', 'pass') 
gmail.inbox.emails.each do |email|
  email.message.attachments.each do |file|
    File.open(File.join(local_path, "name-of-file.doc or use file.filename"), "w+b", 0644 ) { |f| f.write file.body.decoded }
  end
end
于 2012-08-05T16:10:07.427 に答える