Sha256: e43c235cacaca9ebea1fc320e70ff0bb9d5494c6dd934d2c23f9c46d70473466

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

# subclasses
require 'quandl/operation/collapse/guess'
# collapse
module Quandl
module Operation

class Collapse

  class << self
  
    def perform(data, frequency)
      data = Parse.sort( data )
      data = collapse_and_log(data, frequency)
      data
    end
    
    def collapse_and_log(*args)
      t1 = Time.now
      r = collapse(*args)
      CommonLogger.debug "#{self.name}.perform (#{t1.elapsed.microseconds}ms)"
      r
    end
  
    def collapse(data, frequency)
      # store the new collapsed data
      collapsed_data = {}
      range = find_end_of_range( data[0][0], frequency )
      # iterate over the data
      data.each do |row|
        # grab date and value
        date, value = row[0], row[1..-1]
        value = value.first if value.count == 1
        # bump to the next range if it exceeds the current one
        range = find_end_of_range(date, frequency) unless inside_range?(date, range)
        # consider the value for the next range
        collapsed_data[range] = value if inside_range?(date, range) && value.present?
      end
      collapsed_data
    end
  
    def frequency?(data)
      Guess.frequency(data)
    end
  
    def inside_range?(date, range)
      date <= range
    end
  
    def find_end_of_range(date, frequency)
      Date.jd(date).end_of_frequency(frequency).jd
    end
  
  end

end
end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
quandl_operation-0.0.4 lib/quandl/operation/collapse.rb
quandl_operation-0.0.3 lib/quandl/operation/collapse.rb
quandl_operation-0.0.2 lib/quandl/operation/collapse.rb
quandl_operation-0.0.1 lib/quandl/operation/collapse.rb