Class: Stockboy::Providers::IMAP::SearchOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/stockboy/providers/imap/search_options.rb

Overview

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

Constant Summary collapse

VMS_DATE =

Corresponds to %v mode in DateTime#strftime

/\A\d{2}-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{2}\z/i
OPTION_FORMATS =
{
  'BEFORE'     => :date_format,
  'ON'         => :date_format,
  'SINCE'      => :date_format,
  'SENTBEFORE' => :date_format,
  'SENTON'     => :date_format,
  'SENTSINCE'  => :date_format,
  'FLAGGED'    => :boolean_format,
  'UNFLAGGED'  => :boolean_format,
  'SEEN'       => :boolean_format,
  'UNSEEN'     => :boolean_format,
  'ANSWERED'   => :boolean_format,
  'UNANSWERED' => :boolean_format,
  'DELETED'    => :boolean_format,
  'UNDELETED'  => :boolean_format,
  'DRAFT'      => :boolean_format,
  'UNDRAFT'    => :boolean_format,
  'NEW'        => :boolean_format,
  'RECENT'     => :boolean_format,
  'OLD'        => :boolean_format
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SearchOptions

Read options from a hash

Parameters:

  • options (Hash) (defaults to: {})


38
39
40
41
42
# 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

Instance Method Details

#boolean_format(pair) ⇒ Array

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

Parameters:

  • pair (Array)

Returns:

  • (Array)

    pair



110
111
112
113
114
115
116
117
118
# 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) ⇒ Array

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

Parameters:

  • pair (Array)

Returns:

  • (Array)

    pair



93
94
95
96
97
98
99
100
101
102
103
# 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) ⇒ String

Convert a rubyish key to IMAP string key format

Parameters:

  • key (String, Symbol)

Returns:

  • (String)


71
72
73
# 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) ⇒ Array

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

Parameters:

  • pair (Array)

Returns:

  • (Array)

    pair



80
81
82
83
84
85
86
# 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_hashObject

Return a hash with merged and normalized key strings

Examples:

opt = Stockboy::Providers::IMAP::SearchOptions.new(since: Date.new(2012, 12, 21))
opt.to_hash #=> {"SINCE" => #<Date 2012, 12, 21>}


50
51
52
# File 'lib/stockboy/providers/imap/search_options.rb', line 50

def to_hash
  @options
end

#to_imapObject

Return an array of IMAP search keys

Examples:

opt = Stockboy::Providers::IMAP::SearchOptions.new(since: Date.new(2012, 12, 21))
opt.to_imap #=> ["SINCE", "21-DEC-12"]


60
61
62
63
64
# 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