class Stockboy::Providers::IMAP::SearchOptions

Helper for building standard IMAP options passed to [::Net::IMAP#search]

Constants

OPTION_FORMATS
VMS_DATE

Corresponds to %v mode in +DateTime#strftime+

Public Class Methods

new(options={}) click to toggle source

Read options from a hash

@param [Hash] options

# File lib/stockboy/providers/imap/search_options.rb, line 38
def initialize(options={})
  @options = options.each_with_object(Hash.new) do |(k,v), h|
    h[imap_key(k)] = v
  end
end

Public Instance Methods

boolean_format(pair) click to toggle source

Format a key-value pair for setting true/false on IMAP keys (e.g. DELETED)

@param [Array] pair @return [Array] pair

# File lib/stockboy/providers/imap/search_options.rb, line 110
def boolean_format(pair)
  return [] unless pair[1] == true || pair[1] == false

  if pair[1]
    [pair[0]]
  else
    ['NOT', pair[0]]
  end
end
date_format(pair) click to toggle source

Format a key-value pair for IMAP date keys (e.g. SINCE, ON, BEFORE)

@param [Array] pair @return [Array] pair

# File lib/stockboy/providers/imap/search_options.rb, line 93
def date_format(pair)
  pair[1] = case value = pair[1]
  when Date, Time, DateTime
    value.strftime('%v')
  when Numeric
    Time.at(value).strftime('%v')
  when String
    value =~ VMS_DATE ? value : Date.parse(value).strftime('%v').upcase!
  end
  pair
end
imap_key(key) click to toggle source

Convert a rubyish key to IMAP string key format

@param [String, Symbol] key @return [String]

# File lib/stockboy/providers/imap/search_options.rb, line 71
def imap_key(key)
  key.to_s.upcase.gsub(/[^A-Z]/,'').freeze
end
imap_pair(pair) click to toggle source

Format a key-value pair for IMAP, according to the correct type

@param [Array] pair @return [Array] pair

# File lib/stockboy/providers/imap/search_options.rb, line 80
def imap_pair(pair)
  if format = OPTION_FORMATS[pair[0]]
    send(format, pair)
  else
    pair
  end
end
to_hash() click to toggle source

Return a hash with merged and normalized key strings

@example

opt = Stockboy::Providers::IMAP::SearchOptions.new(since: Date.new(2012, 12, 21))
opt.to_hash #=> {"SINCE" => #<Date 2012, 12, 21>}
# File lib/stockboy/providers/imap/search_options.rb, line 50
def to_hash
  @options
end
to_imap() click to toggle source

Return an array of IMAP search keys

@example

opt = Stockboy::Providers::IMAP::SearchOptions.new(since: Date.new(2012, 12, 21))
opt.to_imap #=> ["SINCE", "21-DEC-12"]
# File lib/stockboy/providers/imap/search_options.rb, line 60
def to_imap
  @options.reduce([]) do |a, pair|
    a.concat imap_pair(pair)
  end
end