Class: Stockboy::Providers::IMAP::SearchOptions
- Inherits:
-
Object
- Object
- Stockboy::Providers::IMAP::SearchOptions
- 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
-
#boolean_format(pair) ⇒ Array
Format a key-value pair for setting true/false on IMAP keys (e.g. DELETED).
-
#date_format(pair) ⇒ Array
Format a key-value pair for IMAP date keys (e.g. SINCE, ON, BEFORE).
-
#imap_key(key) ⇒ String
Convert a rubyish key to IMAP string key format.
-
#imap_pair(pair) ⇒ Array
Format a key-value pair for IMAP, according to the correct type.
-
#initialize(options = {}) ⇒ SearchOptions
constructor
Read options from a hash.
-
#to_hash ⇒ Object
Return a hash with merged and normalized key strings.
-
#to_imap ⇒ Object
Return an array of IMAP search keys.
Constructor Details
#initialize(options = {}) ⇒ SearchOptions
Read options from a hash
38 39 40 41 42 |
# File 'lib/stockboy/providers/imap/search_options.rb', line 38 def initialize(={}) @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)
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)
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
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
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_hash ⇒ Object
Return a hash with merged and normalized key strings
50 51 52 |
# File 'lib/stockboy/providers/imap/search_options.rb', line 50 def to_hash @options end |
#to_imap ⇒ Object
Return an array of IMAP search keys
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 |