Class: Datasets::EStatJapan::StatsData
- Defined in:
- lib/datasets/e-stat-japan.rb
Overview
wrapper class for e-Stat API service
Instance Attribute Summary collapse
-
#app_id ⇒ Object
Returns the value of attribute app_id.
-
#id ⇒ Object
Returns the value of attribute id.
Attributes inherited from Dataset
Instance Method Summary collapse
- #areas ⇒ Object
- #columns ⇒ Object
-
#each ⇒ Object
fetch data records from Remote API.
-
#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
constructor
generate accessor instance for e-Stat API's endpoint
getStatsData
. - #schema ⇒ Object
- #time_tables ⇒ Object
Methods inherited from Dataset
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
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 |
# 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.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_id ⇒ Object
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 |
#id ⇒ Object
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
#areas ⇒ Object
140 141 142 143 |
# File 'lib/datasets/e-stat-japan.rb', line 140 def areas load_data @areas end |
#columns ⇒ Object
150 151 152 153 |
# File 'lib/datasets/e-stat-japan.rb', line 150 def columns load_data @columns end |
#each ⇒ Object
fetch data records from Remote API
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/datasets/e-stat-japan.rb', line 120 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 |
#schema ⇒ Object
155 156 157 158 |
# File 'lib/datasets/e-stat-japan.rb', line 155 def schema load_data @schema end |
#time_tables ⇒ Object
145 146 147 148 |
# File 'lib/datasets/e-stat-japan.rb', line 145 def time_tables load_data @time_tables end |