class Data_bcb
This class intends to organize the series data in a easy to use way, making it easier to group lots of data in a coese way, without lost of information.
Attributes
Public Class Methods
# File lib/eba/data.rb, line 54 def self.invalid_data() Data_bcb.new([nil, nil, nil, nil, 1, 1, 1900, 0, false]) end
Initialization is expected to express the state of a single row of data inside the BCB's Database.
# File lib/eba/data.rb, line 17 def initialize(xmlResult) if xmlResult.class.to_s.eql? 'Array' then series_name, series_code, series_periodicity, series_unit, series_day, series_month, series_year, series_value, seasonally_adjusted = xmlResult @name = series_name @pk = series_code @periodicity = series_periodicity.to_s @unit = series_unit @date = standardizes_date(series_day, series_month, series_year) @value = series_value.to_f @seasonally_adjusted = seasonally_adjusted else begin @name = xmlResult.search("NOME").text.sub("-_1532_-", "&") @pk = xmlResult.search("CODIGO").text.to_i @periodicity = xmlResult.search("PERIODICIDADE").text @unit = xmlResult.search("UNIDADE").text.sub("-_1532_-", "&") @date = standardizes_date(xmlResult.search("DIA").text, xmlResult.search("MES").text, xmlResult.search("ANO").text) @value = xmlResult.search("VALOR").text @seasonally_adjusted = false rescue => e puts e.message puts e.backtrace end end end
Public Instance Methods
Simple comparission between two DataBCB objects.
# File lib/eba/data.rb, line 172 def compare_to(data_bcb) return (@name == data_bcb.name and @pk == data_bcb.pk \ and @periodicity == data_bcb.periodicity \ and @unit == data_bcb.unit and @date == data_bcb.date \ and @value = data_bcb.value and @seasonally_adjusted == data_bcb.seasonally_adjusted) end
# File lib/eba/data.rb, line 122 def date return @date end
# File lib/eba/data.rb, line 58 def is_valid? if @name == nil or @name == '' then puts "BCB ERROR: Found invalid name! Value is '#{@name}' for #{@pk}." return false end if @periodicity == nil or @periodicity == '' or @periodicity.length > 1 then puts "BCB ERROR: Found invalid periodicity! Value is '#{@periodicity}' for #{@pk}." return false end if @unit == nil or @unit == '' then puts "BCB ERROR: Found invalid unit! Value is '#{@unit}' for #{@pk}." return false end if @date == nil or @date == '' then puts "BCB ERROR: Found invalid date! Value is '#{@date}' for #{@pk}." return false else if !(DateTime.parse(@date).to_date != nil rescue false) then puts "BCB ERROR: Found invalid date! Value is '#{@date}' for #{@pk}." return false end end if @value == nil then puts "BCB ERROR: Found invalid value! Value is '#{@value}' for #{@pk}." return false else if !(@value.to_f != nil rescue false) then puts "BCB ERROR: Found invalid value! Value is '#{@value}' for #{@pk}." return false end end if @pk == nil or @pk <= 0 then puts "BCB ERROR: Found invalid pk! Value is '#{@pk}'" return false end return true end
Return an “identification key” with data which should be unique to a series (grouped).
# File lib/eba/data.rb, line 50 def key() return @name + @periodicity.to_s + @unit end
Note that there are no set methods in this class, I built it in such a way that you are only intended to access data in the rawest form as possible as it comes from the BCB
Webservice.
# File lib/eba/data.rb, line 106 def pk return @pk end
# File lib/eba/data.rb, line 163 def print() return "Name: #{@name}\n" + "BCB Code: #{@pk}\n" + "Periodicity: #{@periodicity}\n" + "Unit: #{@unit} Seasonally Adjusted? #{@seasonally_adjusted ? 'YES' : 'NO'}\n" + "Date: #{@date} Value: #{@value}\n" end
The Webservice will always supply the date in three separate fields, this methods aim to convert it to a standard dd.mm.YYYY string.
# File lib/eba/data.rb, line 132 def standardizes_date(day, month, year) if day == '' or day == nil then day = "01" end if month == '' or month == nil then month = "01" end if year == '' or year == nil then year = "1900" end if month.to_i > 1900 then year = month month = "01" end return "#{standardizes_number(day.to_i)}.#{standardizes_number(month.to_i)}.#{year}" end
As we are building a dd.mm.yyyy string, we want to standardize the size of the fields.
# File lib/eba/data.rb, line 155 def standardizes_number(number) if (number < 10) return "0#{number}" else return "#{number}" end end