Class: Es::Timeframe

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

Constant Summary collapse

INTERVAL_UNITS =
[:day, :week, :month, :year]
DAY_WITHIN_PERIOD =
[:first, :last]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec, options = {}) ⇒ Timeframe

Returns a new instance of Timeframe.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/es.rb', line 58

def initialize(spec,options = {})
  validate_spec(spec)
  @now = options[:now] || Time.now
  @spec = spec
  @to = Chronic.parse(spec[:to], :now => @now)
  @from = spec[:from] ? Chronic.parse(spec[:from], :now => @now) : to.advance(:days => -1)
  @spec_from = spec[:from]
  @spec_to = spec[:to]
  @interval_unit = spec[:interval_unit] || :day
  @interval = spec[:interval] || 1
  @day_within_period = spec[:day_within_period] || :last
end

Instance Attribute Details

#day_within_periodObject

Returns the value of attribute day_within_period.



28
29
30
# File 'lib/es.rb', line 28

def day_within_period
  @day_within_period
end

#fromObject

Returns the value of attribute from.



28
29
30
# File 'lib/es.rb', line 28

def from
  @from
end

#intervalObject

Returns the value of attribute interval.



28
29
30
# File 'lib/es.rb', line 28

def interval
  @interval
end

#interval_unitObject

Returns the value of attribute interval_unit.



28
29
30
# File 'lib/es.rb', line 28

def interval_unit
  @interval_unit
end

#spec_fromObject

Returns the value of attribute spec_from.



28
29
30
# File 'lib/es.rb', line 28

def spec_from
  @spec_from
end

#spec_toObject

Returns the value of attribute spec_to.



28
29
30
# File 'lib/es.rb', line 28

def spec_to
  @spec_to
end

#toObject

Returns the value of attribute to.



28
29
30
# File 'lib/es.rb', line 28

def to
  @to
end

Class Method Details

.parse(spec, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/es.rb', line 30

def self.parse(spec, options={})
  if spec == 'latest' then
    Timeframe.new({
      :to => 'tomorrow',
      :from => 'yesterday'
    }, options)
  else
    Timeframe.new(spec, options)
  end
end

.parseOldFormat(spec, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/es.rb', line 41

def self.parseOldFormat(spec, options={})
  if spec == 'latest' then
    Timeframe.new({
      :to => 'today',
      :from => 'yesterday'
    })
  else
    Timeframe.new({
                   :to => spec[:endDate],
                   :from => spec[:startDate],
                   :interval_unit => spec[:intervalUnit],
                   :interval => spec[:interval],
                   :day_within_period => spec[:dayWithinPeriod].downcase
                  })
  end
end

Instance Method Details

#to_config_generator_extractObject



92
93
94
95
96
97
98
99
100
# File 'lib/es.rb', line 92

def to_config_generator_extract
  {
    :from                   => from.strftime('%Y-%m-%d'),
    :to                     => to.strftime('%Y-%m-%d'),
    :interval_unit          => interval_unit,
    :interval               => day_within_period.to_s.upcase,
    :day_within_period      => interval
  }
end

#to_extract_fragment(pid, options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/es.rb', line 81

def to_extract_fragment(pid, options = {})
  {
    :endDate            => to.strftime('%Y-%m-%d'),
    :startDate          => from.strftime('%Y-%m-%d'),
    :intervalUnit       => interval_unit,
    :dayWithinPeriod    => day_within_period.to_s.upcase,
    :interval           => interval
  }
end

#validate_spec(spec) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/es.rb', line 72

def validate_spec(spec)
  fail IncorrectSpecificationError.new("Timeframe should have a specification") if spec.nil?
  fail InsufficientSpecificationError.new("To key was not specified during the Timeframe creation") unless spec.has_key?(:to)
  fail InsufficientSpecificationError.new("From key was not specified during the Timeframe creation") unless spec.has_key?(:from)
  fail IncorrectSpecificationError.new("Interval key should be a number") if spec[:interval] && !spec[:interval].is_a?(Fixnum)
  fail IncorrectSpecificationError.new("Interval_unit key should be one of :day, :week, :month, :year") if spec[:interval_unit] && !INTERVAL_UNITS.include?(spec[:interval_unit].to_sym)
  fail IncorrectSpecificationError.new("Day within period should be one of #{DAY_WITHIN_PERIOD.join(', ')}") if spec[:day_within_period] && !DAY_WITHIN_PERIOD.include?(spec[:day_within_period].to_sym)
end