ruby-gmail에서 어느 날 메일로 밀어넣고

12738 단어 Ruby
루비-gmail에서 어느 날의 메일을 압축해서 얻으려면
: 날짜를 ON으로 설정합니다.
: BEFORE 또는:AFTER로 설정할 수 없습니다.
: BEFORE 및:AFTER에서 동일한 날짜를 설정하면 축소되지 않습니다.
디자인이 까다롭다.д・`)
사용이 불편하여 코드를 변경했습니다.
다음 처리 삽입:.
lib/gmail/mailbox.rb
if opts[:after] === opts[:before]
  opts[:on]     = opts[:before]
  opts[:after]  = nil
  opts[:before] = nil
end
엉터리 영어로 본가에'Pull Request'를 던지고 통과했어!!
해봐야지.

다음 버전에 반영되겠죠.
또한 코드의 전모는 다음과 같다.
lib/gmail/mailbox.rb
require 'date'
require 'time'
class Object
  def to_imap_date
    Date.parse(to_s).strftime("%d-%B-%Y")
  end
end

class Gmail
  class Mailbox
    attr_reader :name

    def initialize(gmail, name)
      @gmail = gmail
      @name = name
    end

    def inspect
      "<#Mailbox name=#{@name}>"
    end

    def to_s
      name
    end

    # Method: emails
    # Args: [ :all | :unread | :read ]
    # Opts: {:since => Date.new}
    def emails(key_or_opts = :all, opts={})
      if key_or_opts.is_a?(Hash) && opts.empty?
        search = ['ALL']
        opts = key_or_opts
      elsif key_or_opts.is_a?(Symbol) && opts.is_a?(Hash)
        aliases = {
          :all => ['ALL'],
          :unread => ['UNSEEN'],
          :read => ['SEEN']
        }
        search = aliases[key_or_opts]
      elsif key_or_opts.is_a?(Array) && opts.empty?
        search = key_or_opts
      else
        raise ArgumentError, "Couldn't make sense of arguments to #emails - should be an optional hash of options preceded by an optional read-status bit; OR simply an array of parameters to pass directly to the IMAP uid_search call."
      end
      if !opts.empty?

        if opts[:after] === opts[:before]
          opts[:on]     = opts[:before]
          opts[:after]  = nil
          opts[:before] = nil
        end

        # Support for several search macros
        # :before => Date, :on => Date, :since => Date, :from => String, :to => String
        search.concat ['SINCE', opts[:after].to_imap_date] if opts[:after]
        search.concat ['BEFORE', opts[:before].to_imap_date] if opts[:before]
        search.concat ['ON', opts[:on].to_imap_date] if opts[:on]
        search.concat ['FROM', opts[:from]] if opts[:from]
        search.concat ['TO', opts[:to]] if opts[:to]
        search.concat ['SUBJECT', opts[:subject]] if opts[:subject]

        # Gmail offers us to search the same way we do on the web interface
        # https://developers.google.com/gmail/imap_extensions
        # example: gmail.emails(gm: 'has:attachment in:unread "who is john galt"')
        # 
        search.concat ['X-GM-RAW', opts[:gm]] if opts[:gm]
      end

      # puts "Gathering #{(aliases[key] || key).inspect} messages for mailbox '#{name}'..."
      @gmail.in_mailbox(self) do
        @gmail.imap.uid_search(search).collect { |uid| messages[uid] ||= Message.new(@gmail, self, uid) }
      end
    end

    # This is a convenience method that really probably shouldn't need to exist, but it does make code more readable
    # if seriously all you want is the count of messages.
    def count(*args)
      emails(*args).length
    end

    def messages
      @messages ||= {}
    end
  end
end

좋은 웹페이지 즐겨찾기