Sha256: a7c59267670cfdd5de3786424583baf28a47095138599512fa27515b1de309ff

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

class DataIBGE
	def initialize(table_code, date, variable, location, product, unit, value, periodicity)
		@table_code = table_code
		@date = standardize_date(date, periodicity)
		@variable = variable
		@location = location
		@product = product
		@unit = unit
		@value = value

		#The standard is:
		# 4 : "Trimestral Móvel"
		# 5 : "Ano"
		@periodicity = periodicity 
	end

	def standardize_date(date, periodicity)
		#date is a four digit number
		if periodicity == 6 then
			return "01/01/#{date}"
		elsif periodicity == 5 then
			y = date[0..3]
			m = date[4..5]

			return "01/#{m}/#{y}"
		else
			puts "\nError parsing date for DataIBGE. Attempted to parse #{date}.\n"
			return "ERROR"
		end
	end

	def table_code
		return @table_code
	end

	def date
		return @date
	end

	def variable
		return @variable
	end

	def location
		return @location
	end
	
	def product
		return @product
	end

	def unit
		return @unit
	end
	
	def value
		return @value.to_f
	end

	def is_valid?
		if @table_code == '' or @table_code == nil or @table_code.to_i <= 0 then
			puts "Invalid table code supplied. Value is #{@table_code}."
			return false
		end

		if @date == '' or @date == "ERROR" or @date == nil then
			puts "Date found is invalid. Value is '#{@date}'."
			return false
		end
		
		if @variable == '' or @variable == nil then
			puts "Variable found is invalid. Value is '#{@variable}'."
			return false
		end
	
		if @location == '' or @location == nil then
			puts "Location found is invalid. Value is '#{@location}'."
			return false
		end

		if @unit == '' or @unit == nil then
			puts "Unit found is invalid. Value is '#{@unit}'."
			return false
		end

		if @value == '' or @value == nil then
			puts "Value found is invalid. Value is '#{@value}'."
			return false
		end

		return true 
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
eia-1.1.2 lib/eia/data.rb
eia-1.1.1 lib/eia/data.rb