Class: Data_bcb

Inherits:
Object
  • Object
show all
Defined in:
lib/eba/data.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xmlResult) ⇒ Data_bcb

Initialization is expected to express the state of a single row of data inside the BCB’s Database.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/eba/data.rb', line 5

def name
  @name
end

#periodicityObject

Returns the value of attribute periodicity.



5
6
7
# File 'lib/eba/data.rb', line 5

def periodicity
  @periodicity
end

#seasonally_adjustedObject

Returns the value of attribute seasonally_adjusted.



5
6
7
# File 'lib/eba/data.rb', line 5

def seasonally_adjusted
  @seasonally_adjusted
end

#unitObject

Returns the value of attribute unit.



5
6
7
# File 'lib/eba/data.rb', line 5

def unit
  @unit
end

#valueObject

Returns the value of attribute value.



5
6
7
# File 'lib/eba/data.rb', line 5

def value
  @value
end

Class Method Details

.invalid_dataObject



54
55
56
# File 'lib/eba/data.rb', line 54

def self.invalid_data()
  Data_bcb.new([nil, nil, nil, nil, 1, 1, 1900, 0, false])
end

Instance Method Details

#compare_to(data_bcb) ⇒ Object

Simple comparission between two DataBCB objects.



172
173
174
175
176
177
# 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

#dateObject



122
123
124
# File 'lib/eba/data.rb', line 122

def date
  return @date
end

#is_valid?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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

#keyObject

Return an “identification key” with data which should be unique to a series (grouped).



50
51
52
# File 'lib/eba/data.rb', line 50

def key()
  return @name + @periodicity.to_s + @unit
end

#pkObject

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.



106
107
108
# File 'lib/eba/data.rb', line 106

def pk
  return @pk
end


163
164
165
166
167
168
169
# 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

#standardizes_date(day, month, year) ⇒ Object

The Webservice will always supply the date in three separate fields, this methods aim to convert it to a standard dd.mm.YYYY string.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# 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

#standardizes_number(number) ⇒ Object

As we are building a dd.mm.yyyy string, we want to standardize the size of the fields.



155
156
157
158
159
160
161
# File 'lib/eba/data.rb', line 155

def standardizes_number(number)
  if (number < 10)
    return "0#{number}"
  else
    return "#{number}"
  end
end