Sha256: 854f3fe9c276b338d2d1b20ecd13f94fd55fceb1ce1297f2b68c99830e3b2b82

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

class Quandl::Operation::QDFormat::Load
  
  class << self
  
    def from_file(path)
      from_string(File.read(path).strip)
    end
  
    def from_string(input)
      nodes = []
      input.each_line do |line|
        # strip whitespace
        line = line.strip.rstrip
        # ignore comments and blank lines
        next if line[0] == '#' || line.blank?
        # code_format denotes the start of a new node
        nodes << { attributes: '', data: '' } if line =~ code_format
        # attribute_format denotes an attribute
        if line =~ attribute_format
          # add the attribute to attributes
          nodes[-1][:attributes] += "#{line}\n"
          # otherwise it must be data
        else
          nodes[-1][:data] += "#{line}\n"
        end
      end
      # append the current node
      nodes = parse_nodes(nodes)
      nodes
    end
    
    
    protected
    
    def parse_nodes(nodes)
      nodes.collect do |node|
        node[:attributes] = YAML.load( node[:attributes] )
        node[:attributes][:data] = CSV.parse(node[:data])
        Quandl::Operation::QDFormat::Node.new(node[:attributes])
      end
    end
  
    def code_format
      /^code: (.+)/
    end
  
    def attribute_format
      /^([a-z0-9_]+): (.+)/
    end
  
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
quandl_operation-0.1.20 lib/quandl/operation/qdformat/load.rb
quandl_operation-0.1.19 lib/quandl/operation/qdformat/load.rb