Class: Datasets::EStatJapan::StatsData

Inherits:
Dataset
  • Object
show all
Defined in:
lib/datasets/e-stat-japan.rb

Overview

wrapper class for e-Stat API service

Instance Attribute Summary collapse

Attributes inherited from Dataset

#metadata

Instance Method Summary collapse

Methods inherited from Dataset

#clear_cache!, #to_table

Constructor Details

#initialize(id, app_id: nil, areas: nil, categories: nil, times: nil, skip_levels: [1], hierarchy_selection: 'child', skip_nil_column: true, skip_nil_row: false, time_range: nil) ⇒ StatsData

generate accessor instance for e-Stat API's endpoint getStatsData. for detail spec : https://www.e-stat.go.jp/api/api-info/e-stat-manual

Examples:

stats_data = Datasets::EStatJapan::StatsData.new(
  "0000020201", # A Population and household (key name: A 人口・世帯)
  categories: ["A1101"], # Population (key name: A1101_人口総数)
  areas: ["01105", "01106"], # Toyohira-ku Sapporo-shi Hokkaido, Minami-ku Sapporo-shi Hokkaido
  times: ["1981100000", "1982100000"],
  hierarchy_selection: 'child',
  skip_child_area: true,
  skip_nil_column: true,
  skip_nil_row: false,
)

Parameters:

  • id (String)

    Statistical data id

  • areas (Array<String>) (defaults to: nil)

    Target areas (fetch all if omitted)

  • categories (Array<String>) (defaults to: nil)

    Category IDs (fetch all if omitted)

  • times (Array<String>) (defaults to: nil)

    Time axes (fetch all if omitted)

  • skip_levels (Array<Number>) (defaults to: [1])

    Skip levels for parsing (defaults to [1])

  • hierarchy_selection (String) (defaults to: 'child')

    Select target from 'child', 'parent', or 'both'. (Example: 札幌市○○区 -> 'child':札幌市○○区 only; 'parent':札幌市 only; 'both': Both selected) (defaults to both)

  • skip_nil_column (Boolean) (defaults to: true)

    Skip column if contains nil

  • skip_nil_row (Boolean) (defaults to: false)

    Skip row if contains nil



57
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
101
102
103
104
105
# File 'lib/datasets/e-stat-japan.rb', line 57

def initialize(id,
               app_id: nil,
               areas: nil, categories: nil, times: nil,
               skip_levels: [1],
               hierarchy_selection: 'child',
               skip_nil_column: true,
               skip_nil_row: false,
               time_range: nil)
  @app_id = app_id || fetch_app_id
  if @app_id.nil? || @app_id.empty?
    raise ArgumentError, 'Please set app_id via `Datasets::EStatJapan.configure` method, environment var `ESTATJAPAN_APP_ID` or keyword argument `:app_id`'
  end

  super()

  @api_version = '3.0'
  @base_url = "https://api.e-stat.go.jp/rest/#{@api_version}/app/json/getStatsData"
  @metadata.id = "e-stat-japan-#{@api_version}"
  @metadata.name = "e-Stat API #{@api_version}"
  @metadata.url = @base_url
  @metadata.licenses = ["CC-BY-4.0"]
  @metadata.description = "e-Stat API #{@api_version}"

  @id = id
  @areas = areas
  @categories = categories
  @times = times
  @skip_levels = skip_levels
  case hierarchy_selection
  when 'child' then
    @skip_child_area = false
    @skip_parent_area = true
  when 'parent' then
    @skip_child_area = true
    @skip_parent_area = false
  else # 'both'
    @skip_child_area = false
    @skip_parent_area = false
  end
  @skip_nil_column = skip_nil_column
  @skip_nil_row = skip_nil_row
  @time_range = time_range

  @url = generate_url
  option_hash = Digest::MD5.hexdigest(@url.to_s)
  base_name = "e-stat-japan-#{option_hash}.json"
  @data_path = cache_dir_path + base_name
  @loaded = false
end

Instance Attribute Details

#app_idObject

Returns the value of attribute app_id.



32
33
34
# File 'lib/datasets/e-stat-japan.rb', line 32

def app_id
  @app_id
end

#idObject

Returns the value of attribute id.



32
33
34
# File 'lib/datasets/e-stat-japan.rb', line 32

def id
  @id
end

Instance Method Details

#areasObject



141
142
143
144
# File 'lib/datasets/e-stat-japan.rb', line 141

def areas
  load_data
  @areas
end

#columnsObject



151
152
153
154
# File 'lib/datasets/e-stat-japan.rb', line 151

def columns
  load_data
  @columns
end

#eachObject

fetch data records from Remote API

Examples:

indices = []
rows = []
map_id_name = {}
estat.each do |record|
  # Select Hokkaido prefecture only
  next unless record.id.to_s.start_with? '01'
  indices << record.id
  rows << record.values
  map_id_name[record.id] = record.name
end


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/datasets/e-stat-japan.rb', line 121

def each
  return to_enum(__method__) unless block_given?

  load_data

  # create rows
  @areas.each do |a_key, a_value|
    rows = []
    @time_tables.reject { |_key, x| x[:skip] }.each do |st_key, _st_value|
      row = @columns.reject { |_key, x| x[:skip] }.map do |c_key, _c_value|
        @indexed_data.dig(st_key, a_key, c_key)
      end
      rows << row
    end
    next if @skip_nil_row && rows.flatten.count(nil).positive?

    yield Record.new(a_key, a_value['@name'], rows.flatten)
  end
end

#schemaObject



156
157
158
159
# File 'lib/datasets/e-stat-japan.rb', line 156

def schema
  load_data
  @schema
end

#time_tablesObject



146
147
148
149
# File 'lib/datasets/e-stat-japan.rb', line 146

def time_tables
  load_data
  @time_tables
end