Module: Eco::API::UseCases::OozeSamples::Helpers::Filters

Included in:
Eco::API::UseCases::OozeSamples::Helpers
Defined in:
lib/eco/api/usecases/ooze_samples/helpers/filters.rb

Constant Summary collapse

FILTER_TIME_FORMAT =
'%Y-%m-%dT%H:%M:%SZ'

Instance Method Summary collapse

Instance Method Details

#date_range_filter(from: nil, to: nil, key: :updated_at) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 23

def date_range_filter(from: nil, to: nil, key: :updated_at)
  return nil unless from || to
  key, name = field_key_name(key)
  {
    "relstart": "today",
    "time_zone": "Pacific/Auckland",
    "relative": false,
    "key": key,
    "name": name,
    "type": "date_filter"
  }.tap do |out|
    out.merge!("lbound": to_date_filter(from)) if from
    out.merge!("ubound": to_date_filter(to)) if to
  end
end

#days(num) ⇒ Object



45
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 45

def days(num);  num * 60 * 60 * 24; end

#daystart(date) ⇒ Object



49
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 49

def daystart(date); set_time(date, 0, 0, 0); end

#field_key_name(value) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 53

def field_key_name(value)
  case value
  when :updated_at
    ["updated_at", "Last updated"]
  when :created_at
    ["created_at", "Page created"]
  when Ecoportal::API::V2::Page::Component
    [value.ref_backend, value.label]
  when :tags
    ["tags", "Location Tags"]
  else
    [nil , nil]
  end
end

#midnight(date) ⇒ Object



48
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 48

def midnight(date); set_time(date, 23, 59, 59); end

#previous_sunday(date) ⇒ Object



50
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 50

def previous_sunday(date); midnight(sunday(date - weeks(1)));  end

#set_time(date, h, m, s) ⇒ Object



43
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 43

def set_time(date, h, m, s); Time.new(date.year, date.month, date.day, h, m, s); end

#sunday(date) ⇒ Object



47
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 47

def sunday(date); date + days(7 - weekday(date)); end

#tags_filter(tags, any: true, negate: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 9

def tags_filter(tags, any: true, negate: false)
  tags = [tags].flatten.compact
  return nil if tags.empty?
  key, name = field_key_name(:tags)
  {
    "tags": tags,
    "mode": any ? "any" : "all",
    "negate": negate,
    "key": key,
    "name": name,
    "type": "tag_filter"
  }
end

#this_monday(date) ⇒ Object



51
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 51

def this_monday(date); set_time(previous_sunday(as_at_date) + days(1), 0, 0, 0);  end

#to_date_filter(date) ⇒ Object



39
40
41
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 39

def to_date_filter(date)
  daystart(date).utc.strftime(FILTER_TIME_FORMAT)
end

#todayObject



46
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 46

def today; Date.today.to_time; end

#weeks(num) ⇒ Object



44
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 44

def weeks(num); num * days(7); end