Class: Patty::Server

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/patty/server.rb

Constant Summary collapse

TIMELINE_STEPS =
{
  :second  => 1.0 / 24 / 60 / 2,
  :minute  => 1.0 / 24 / 60,
  :tminute => 1.0 / 24 / 6,
  :hour    => 1.0 / 24,
  :day     => 1.0,
  :month   => 25,
  :year    => 365
}
TIMELINE_MEASURES =
[:minute, :tminute, :hour, :day, :month, :year]

Instance Method Summary collapse

Instance Method Details

#handlerObject



22
23
24
# File 'lib/patty/server.rb', line 22

def handler
  Object.const_get("#{params[:report].capitalize}").new
end

#timelineObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/patty/server.rb', line 26

def timeline
  from = params[:from]
  to   = params[:to]
  step = params[:step].to_sym

  unless TIMELINE_STEPS.keys.include?(step)
    raise ArgumentError, "invalid step #{step} for timeline"
  end

  from_datetime = Patty::TimeSignature.new(from).to_datetime
  to_datetime   = Patty::TimeSignature.new(to).to_datetime

  from_datetime.step(to_datetime, TIMELINE_STEPS[step]).map do |s|
    current_step = Patty::TimeSignature.from_datetime(s)

    if step == :second
      current_step
    else
      current_step.parents[TIMELINE_MEASURES.index(step)]
    end
  end
end