Sha256: 85730d516a660325c66b87a612e1de01b9d55f784faf23802eea6be070fb411d

Contents?: true

Size: 1.11 KB

Versions: 6

Compression:

Stored size: 1.11 KB

Contents

module Quandl
  class List
    attr_reader :meta
    attr_reader :values

    def initialize(klass, values, meta)
      @klass = klass
      @values = values.map { |v| klass.new(v, meta: meta) }
      @meta = meta
    end

    def more_results?
      fail(QuandlError, "#{@klass} does not support pagination yet") if !@meta.key?('total_pages') && !@meta.key?('current_page')
      @meta['total_pages'] > @meta['current_page']
    end

    def to_a
      @values.map(&:to_a)
    end

    def to_csv
      fail(QuandlError, 'No values to export') if @values.empty?

      CSV.generate do |csv|
        csv << @values.first.column_names
        @values.each do |row|
          csv << row.to_a
        end
      end
    end

    def inspect
      { meta: @meta, values: @values }.to_s
    end

    private

    def method_missing(method_name, *args, &block)
      return @meta[method_name.to_s] if @meta.key?(method_name.to_s)
      return @meta[*args] if method_name.to_s == '[]' && @meta.key?(args[0].to_s)
      return @values.method(method_name).call(*args, &block) if @values.respond_to?(method_name)
      super
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
quandl-1.0.3 lib/quandl/model/list.rb
quandl-1.0.2 lib/quandl/model/list.rb
quandl-1.0.1 lib/quandl/model/list.rb
quandl-1.0.0 lib/quandl/model/list.rb
quandl-1.0.0.rc2 lib/quandl/model/list.rb
quandl-1.0.0.rc1 lib/quandl/model/list.rb