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'.freeze

Instance Method Summary collapse

Instance Method Details

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



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

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



52
53
54
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 52

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

#daystart(date) ⇒ Object



68
69
70
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 68

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

#field_key_name(value) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 80

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



64
65
66
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 64

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

#previous_sunday(date) ⇒ Object



72
73
74
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 72

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

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

rubocop:disable Naming/MethodParameterName



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

def set_time(date, h, m, s) # rubocop:disable Naming/MethodParameterName
  Time.new(date.year, date.month, date.day, h, m, s)
end

#sunday(date) ⇒ Object



60
61
62
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 60

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

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



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

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



76
77
78
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 76

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

#to_date_filter(date) ⇒ Object



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

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

#todayObject



56
57
58
# File 'lib/eco/api/usecases/ooze_samples/helpers/filters.rb', line 56

def today
  Date.today.to_time
end

#weeks(num) ⇒ Object



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

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