Class: IBGE

Inherits:
Object
  • Object
show all
Defined in:
lib/eia/ibge.rb

Instance Method Summary collapse

Constructor Details

#initializeIBGE

Returns a new instance of IBGE.



6
7
8
# File 'lib/eia/ibge.rb', line 6

def initialize()
    @webservice_url = 'http://api.sidra.ibge.gov.br' 
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
# File 'lib/eia/ibge.rb', line 10

def connected?()
  response = Net::HTTP.get(URI(@webservice_url + '/values/t/1612/n1/1'))

  if response.to_s == '' then
    return false
  else
    return true
  end
end

#consume_json(json_string, table_code) ⇒ Object



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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/eia/ibge.rb', line 66

def consume_json(json_string, table_code)
  begin
    output = JSON.parse(json_string)
    heading = output.delete_at(0)
    identifier = heading["D1N"]

    if identifier.include? "Trimestre Móvel" then 
      periodicity = 6
    elsif identifier.eql? "Trimestre" then
      periodicity = 4
    elsif identifier.include? "Ano" then
      periodicity = 5
    elsif identifier.include? "Mês" then
      periodicity = 2
    else
      puts "Error! Unexpected case! Found is: #{identifier}. Report to the dev team."
      return Array.new
    end

    data_array = Array.new

    output.each do |hash|
      data = nil
      date = nil
      variable = nil
      classification = Array.new
      location = nil
      unit = nil
      val = nil

      hash.each do |key, value|
        case key
          when "D1C" #date
            date = value
          when "D2N" #variable
            variable = value
          when "D3N" #location
            location = value
          when "MN" #unit
            unit = value
          when "V" #value
            val = value.to_f
          else
            if key.include? "D" and key.include? "N" and key[1].to_i > 3 then
              classification << "#{heading[key]}: #{value}"
            end
            #Else this information is discarded
        end
      end

      data_array << DataIBGE.new(table_code, date, variable, location, 
                                 classification, unit, val, periodicity)
    end 
    
    return data_array 

  rescue Exception => e
    if e.to_s.include? "incompatível com a tabela" then
      return Array.new
    else
      puts "Error parsing the JSON response: #{e}\nTrace: #{e.backtrace}"
      return nil
    end
  end
end

#get_series(code, period, variables, territorial_level, classification) ⇒ Object

string form specifying terriories classification : a string in the form c_lvl_k | list_of_products; c_lvl_j | list_of_products; …



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/eia/ibge.rb', line 28

def get_series(code, period, variables, territorial_level, classification)
  url = @webservice_url + "/values"

  if code != '' and code != nil then
    url += "/t/#{code}"
  end

  if period != '' and period != nil then
    url += "/p/#{period}"
  else
    url += "/p/all"
  end

  if variables != '' and variables != nil then
    url += "/v/#{variables}"
  else
    url += "/v/all"
  end

  if territorial_level != '' and territorial_level != nil then
    territorial_level.split(';').each do |word|
      ws = word.split('|')
      url += "/n#{ws[0]}/#{ws[1]}" 
    end
  else
    url += "/n1/all"
  end
  
  if classification != '' and classification != nil then
    classification.split(';').each do |word|
      ws = word.split('|')
      url += "/c#{ws[0]}/#{ws[1]}"
    end
  end

  return consume_json(Net::HTTP.get(URI(url)), code)
end