Class: Weathercock::KeyBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/weathercock/key_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(namespace:, klass:) ⇒ KeyBuilder

Returns a new instance of KeyBuilder.



7
8
9
10
# File 'lib/weathercock/key_builder.rb', line 7

def initialize(namespace:, klass:)
  @namespace = namespace
  @klass_key = klass.name.gsub("::", "_").downcase
end

Instance Method Details

#base(event) ⇒ Object



12
13
14
# File 'lib/weathercock/key_builder.rb', line 12

def base(event)
  "#{@namespace}:#{@klass_key}:#{event}"
end

#bucket(base, type, time) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/weathercock/key_builder.rb', line 20

def bucket(base, type, time)
  case type
  when :hours  then "#{base}:#{time.strftime("%Y-%m-%d-%H")}"
  when :days   then "#{base}:#{time.strftime("%Y-%m-%d")}"
  when :months then "#{base}:#{time.strftime("%Y-%m")}"
  end
end

#total(base) ⇒ Object



16
17
18
# File 'lib/weathercock/key_builder.rb', line 16

def total(base)
  "#{base}:total"
end

#union_dest(base, type, count) ⇒ Object



41
42
43
# File 'lib/weathercock/key_builder.rb', line 41

def union_dest(base, type, count)
  "#{base}:top:#{type}:#{count}"
end

#window_keys(base, type, count) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/weathercock/key_builder.rb', line 28

def window_keys(base, type, count)
  now = Time.now
  case type
  when :hours
    count.times.map { |i| bucket(base, type, now - (i * 3600)) }
  when :days
    count.times.map { |i| bucket(base, type, now - (i * 86400)) }
  when :months
    d = Date.new(now.year, now.month)
    count.times.map { |i| bucket(base, type, d << i) }
  end
end