Module: Tempest::DateFilter

Defined in:
lib/tempest/date_filter.rb

Class Method Summary collapse

Class Method Details

.filter(posts, since: nil, until_at: nil) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/tempest/date_filter.rb', line 20

def filter(posts, since: nil, until_at: nil)
  posts.select do |p|
    ts = p[:created_at] || p["created_at"]
    next false if ts.nil?
    t = Time.iso8601(ts)
    (since.nil? || t >= since) && (until_at.nil? || t < until_at)
  end
end

.local_midnight(now) ⇒ Object



29
30
31
32
# File 'lib/tempest/date_filter.rb', line 29

def local_midnight(now)
  l = now.respond_to?(:localtime) ? now.localtime : now
  Time.local(l.year, l.month, l.day, 0, 0, 0)
end

.parse(raw, now: Time.now) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/tempest/date_filter.rb', line 7

def parse(raw, now: Time.now)
  case raw
  when "today"     then local_midnight(now)
  when "yesterday" then local_midnight(now) - 86_400
  when /\A(\d+)d\z/ then local_midnight(now) - (Regexp.last_match(1).to_i * 86_400)
  when /\A\d{4}-\d{2}-\d{2}\z/ then Time.local(*raw.split("-").map(&:to_i))
  else
    Time.iso8601(raw)
  end
rescue ArgumentError
  raise ArgumentError, "invalid date: #{raw.inspect}"
end