Class: Jpnstacapi::Stats

Inherits:
Api
  • Object
show all
Defined in:
lib/jpnstacapi/stats.rb

Instance Method Summary collapse

Methods inherited from Api

#httpRequest, #setParameters

Constructor Details

#initializeStats

Returns a new instance of Stats.



2
3
4
5
# File 'lib/jpnstacapi/stats.rb', line 2

def initialize
  super
  @url = @url + 'getStatsData'
end

Instance Method Details

#fetch(id) ⇒ Object



7
8
9
10
# File 'lib/jpnstacapi/stats.rb', line 7

def fetch id
  @params[:statsDataId] = id
  return getXml
end

#getXmlObject



12
13
14
15
16
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
47
48
# File 'lib/jpnstacapi/stats.rb', line 12

def getXml
  xml = super
  xml = xml.elements['GET_STATS_DATA/STATISTICAL_DATA[1]']
  unless table_inf = xml.elements['TABLE_INF']
    return false
  end
  unless class_inf = xml.elements['CLASS_INF']
    return false
  end
  unless data_inf = xml.elements['DATA_INF']
    return false
  end
  stats = {
    :name => table_inf.elements['TITLE'].text,
    :meta => Hash.new,
    :data => Array.new
  }
  class_inf.elements.each('CLASS_OBJ') do |class_obj|
    items = Hash.new
    class_obj.elements.each('CLASS') do |item|
      items.store item.attributes['code'], item.attributes['name']
    end
    stats[:meta]
      .store class_obj.attributes['id'], {:name=>class_obj.attributes['name'], :items=>items}
  end
  data_inf.elements.each('VALUE') do |value|
    row = Array.new
    stats[:meta].keys.each do |meta|
      row.push stats[:meta]
        .fetch(meta)[:items]
        .fetch(value.attributes[meta])
    end
    row.push value.text
    stats[:data].push row
  end
  return stats
end