Sha256: a95773e80229a8dcd07fc47836e89d18dc3df18719c827a3a21f40b63a836fbf

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

module WorldbankAsDataframe

  class DataQuery < Query

    def initialize(name, id, model)
      super
      clear_params!
    end
    
    def fetch_as_hsh
      results = super
      clear_params!
      results
    end
    
    def fetch
      require 'polars-df'

      results = super
      clear_params!
      Polars::DataFrame.new(results.map(&:to_h))
    end
    
    def lending_type(lending_type)
      ensure_unconflicting_qualifiers
      parsed = indifferent_number lending_type
      @param_dir = ['lendingTypes', parsed]
      self
    end

    def income_level(income_level)
      ensure_unconflicting_qualifiers
      parsed = indifferent_number income_level
      @param_dir = ['incomeLevels', parsed]
      self
    end

    def region(regions)
      ensure_unconflicting_qualifiers
      parsed = indifferent_number regions
      @param_dir = ['country', parsed]
      self
    end

    def country(country)
      ensure_unconflicting_qualifiers
      parsed = indifferent_type country
      parsed = ensure_country_id parsed
      @param_dir = ['country', parsed]
      self
    end

    def indicator(indicators)
      parsed = indifferent_number indicators
      @id = parsed
      self
    end
    alias_method(:find, :indicator)

    def source(sources)
      ensure_unconflicting_qualifiers
      parsed = indifferent_number sources
      @param_dir = ['sources', parsed]
      self
    end

    private

    def ensure_unconflicting_qualifiers
      if @params_filled
        raise ArgumentError,
          "Only one of 'income_level', 'lending_type', 'country', or 'source' can be called on the same query"
      end
      @params_filled = true
    end
    
    def clear_params!
      @param_dir = []
      @params_filled = false      
    end    
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
worldbank_as_dataframe-0.2.1 lib/worldbank_as_dataframe/data_query.rb
worldbank_as_dataframe-0.2 lib/worldbank_as_dataframe/data_query.rb
worldbank_as_dataframe-0.1.1 lib/worldbank_as_dataframe/data_query.rb
worldbank_as_dataframe-0.1.0 lib/worldbank_as_dataframe/data_query.rb