resas-api 0.1.6 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,60 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext'
3
+ require 'active_support/concern'
4
+
5
+ require 'uri'
6
+
7
+ require_relative './../client/util/path_settings_normalizer'
8
+ require_relative './../client/util/request_settings_normalizer'
9
+ require_relative './../response/body'
10
+
11
+ # RESAS (Regional Economy Society Analyzing System) に関する機能を格納する名前空間
12
+ # @see {https://resas.go.jp/}
13
+ module Resas
14
+
15
+ # RESAS API を扱うための Gem - トップの名前空間
16
+ # @see {https://opendata.resas-portal.go.jp/}
17
+ # @see {https://opendata.resas-portal.go.jp/docs/api/v1-rc.1/index.html}
18
+ module Api
19
+
20
+ module ClientExt
21
+
22
+ # RESAS API へのリクエストを処理するメソッドを格納するモジュール
23
+ module Request
24
+
25
+ extend ActiveSupport::Concern
26
+
27
+ included do
28
+ undef :post
29
+ end
30
+
31
+ # GET メソッドで RESAS API へアクセスするメソッド
32
+ # @return [Resas::Api::Response::Body]
33
+ def get( *args )
34
+ super
35
+ end
36
+
37
+ # @return [URI::HTTPS or URI::Generic]
38
+ def path( *args )
39
+ path, options, *others = Client::Util::PathSettingsNormalizer.process( args )
40
+ full = others.shift
41
+
42
+ uri = ( full ? URI( "#{ base_endpoint }/#{ path }" ) : URI( path ) )
43
+ uri.query = options.to_query if options.present?
44
+ uri
45
+ end
46
+
47
+ private
48
+
49
+ # @return [Resas::Api::Response::Body]
50
+ def request( method, *args )
51
+ _path, options, parse_json, generate_instance = Client::Util::RequestSettingsNormalizer.process( args )
52
+ con = connection( parse_json: parse_json, generate_instance: generate_instance )
53
+ response = __request__( con, method, _path, options )
54
+ Resas::Api::Response::Body.new( response.body, self, _path, options )
55
+ end
56
+
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,22 @@
1
+ # RESAS (Regional Economy Society Analyzing System) に関する機能を格納する名前空間
2
+ # @see {https://resas.go.jp/}
3
+ module Resas
4
+
5
+ # RESAS API を扱うための Gem - トップの名前空間
6
+ # @see {https://opendata.resas-portal.go.jp/}
7
+ # @see {https://opendata.resas-portal.go.jp/docs/api/v1-rc.1/index.html}
8
+ module Api
9
+
10
+ module Response
11
+
12
+ class BaseError < StandardError
13
+
14
+ def initialize(h)
15
+ @h = h
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,101 @@
1
+ # RESAS (Regional Economy Society Analyzing System) に関する機能を格納する名前空間
2
+ # @see {https://resas.go.jp/}
3
+ module Resas
4
+
5
+ # RESAS API を扱うための Gem - トップの名前空間
6
+ # @see {https://opendata.resas-portal.go.jp/}
7
+ # @see {https://opendata.resas-portal.go.jp/docs/api/v1-rc.1/index.html}
8
+ module Api
9
+
10
+ module Response
11
+
12
+ class Body
13
+
14
+ def initialize( h, client, path, options )
15
+ @h = h
16
+
17
+ @client = client
18
+ @path = path
19
+ @options = options
20
+
21
+ raise_error_if_needed
22
+ end
23
+
24
+ # @return [URI::HTTPS or URI::Generic]
25
+ def path( full: true )
26
+ options = @options.dup.merge( full: full )
27
+ @client.path( @path, options )
28
+ end
29
+
30
+ # @return [Array or nil]
31
+ def result
32
+ @h[ 'result' ]
33
+ end
34
+
35
+ # @return [String or nil]
36
+ def message
37
+ @h[ 'message' ]
38
+ end
39
+
40
+ # @return [Integer or nil]
41
+ def status_code
42
+ _status_code = @h[ 'statusCode' ]
43
+ _status_code.present? ? _status_code.to_i : nil
44
+ end
45
+
46
+ # @return [String or nil]
47
+ def description
48
+ @h[ 'description' ]
49
+ end
50
+
51
+ # @return [Boolean]
52
+ def has_message?
53
+ message.present?
54
+ end
55
+
56
+ # @return [Boolean]
57
+ def has_description?
58
+ description.present?
59
+ end
60
+
61
+ def method_missing( method_name, *args, &block )
62
+ return super unless @h.respond_to?( method_name )
63
+ @h.send( method, *args, &block )
64
+ rescue NoMethodError => e
65
+ e.message = <<-MSG
66
+ Resas::Api::Response::Body#method_missing
67
+ #{ e.message }
68
+ method_name: #{ method_name }, @h.class: #{ @h.class }
69
+ MSG
70
+ # binding.pry
71
+ raise e
72
+ end
73
+
74
+ def respond_to?( method, include_all = true )
75
+ return @h.respond_to?( method, include_all ) || super
76
+ end
77
+
78
+ private
79
+
80
+ def raise_error_if_needed
81
+ raise error_class.new( @h ) unless result.present?
82
+ end
83
+
84
+ def error_class
85
+ case status_code
86
+ when 403
87
+ Resas::Api::Response::Forbidden
88
+ when 404
89
+ Resas::Api::Response::NotFound
90
+ when 429
91
+ Resas::Api::Response::TooManyRequests
92
+ else
93
+ Resas::Api::Response::BaseError
94
+ end
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,17 @@
1
+ # RESAS (Regional Economy Society Analyzing System) に関する機能を格納する名前空間
2
+ # @see {https://resas.go.jp/}
3
+ module Resas
4
+
5
+ # RESAS API を扱うための Gem - トップの名前空間
6
+ # @see {https://opendata.resas-portal.go.jp/}
7
+ # @see {https://opendata.resas-portal.go.jp/docs/api/v1-rc.1/index.html}
8
+ module Api
9
+
10
+ module Response
11
+
12
+ class Forbidden < BaseError
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # RESAS (Regional Economy Society Analyzing System) に関する機能を格納する名前空間
2
+ # @see {https://resas.go.jp/}
3
+ module Resas
4
+
5
+ # RESAS API を扱うための Gem - トップの名前空間
6
+ # @see {https://opendata.resas-portal.go.jp/}
7
+ # @see {https://opendata.resas-portal.go.jp/docs/api/v1-rc.1/index.html}
8
+ module Api
9
+
10
+ module Response
11
+
12
+ class NotFound < BaseError
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # RESAS (Regional Economy Society Analyzing System) に関する機能を格納する名前空間
2
+ # @see {https://resas.go.jp/}
3
+ module Resas
4
+
5
+ # RESAS API を扱うための Gem - トップの名前空間
6
+ # @see {https://opendata.resas-portal.go.jp/}
7
+ # @see {https://opendata.resas-portal.go.jp/docs/api/v1-rc.1/index.html}
8
+ module Api
9
+
10
+ module Response
11
+
12
+ class TooManyRequests < BaseError
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,144 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext'
3
+
4
+ namespace :make_methods do
5
+
6
+ desc 'Make methods of Resas::Api::Endpoint'
7
+ task :endpoints do
8
+ texts = []
9
+
10
+ # 共通
11
+
12
+ texts << <<-TXT
13
+ 都道府県一覧 api/v1-rc.1/prefectures
14
+ 市区町村一覧 api/v1-rc.1/cities
15
+ 旧市区町村一覧 api/v1-rc.1/oldCities
16
+ TXT
17
+
18
+ texts << <<-TXT
19
+ 産業大分類 api/v1-rc.1/industries/broad
20
+ 産業中分類 api/v1-rc.1/industries/middle
21
+ 産業小分類 api/v1-rc.1/industries/narrow
22
+ 職業大分類 api/v1-rc.1/jobs/broad
23
+ 職業中分類 api/v1-rc.1/jobs/middle
24
+ TXT
25
+
26
+ texts << <<-TXT
27
+ 特許.技術分野 api/v1-rc.1/patents/broad
28
+ 特許.技術テーマ api/v1-rc.1/patents/middle
29
+ 税関 api/v1-rc.1/customs
30
+ 輸出入花火図.取引国_地域 api/v1-rc.1/regions/broad
31
+ 輸出入花火図.取引国_国 api/v1-rc.1/regions/middle
32
+ 農業部門 api/v1-rc.1/regions/agricultureDepartments
33
+ 特許権者の所在地 api/v1-rc.1/patents/locations
34
+ 輸出入花火図.品目_大分類 api/v1-rc.1/tradeInfoItemTypes/broad
35
+ 輸出入花火図.品目_中分類 api/v1-rc.1/tradeInfoItemTypes/middle
36
+ 輸出入花火図.品目_小分類 api/v1-rc.1/tradeInfoItemTypes/narrow
37
+ TXT
38
+
39
+ # 産業
40
+
41
+ texts << <<-TXT
42
+ 特許一覧 api/v1-rc.1/industry/patent/list
43
+ 国、税関別輸出入 api/v1-rc.1/industry/export/fromTo
44
+ 海外への企業進出動向 api/v1-rc.1/industry/globalmarket/perPref
45
+ 産業別特化係数 api/v1-rc.1/industry/power/forIndustry
46
+ 地域別特化係数 api/v1-rc.1/industry/power/forArea
47
+ 製造業事業所単位分析_継続・参入・退出事業所別の推移 api/v1-rc.1/industry/power/forManufacturerEstablishments
48
+ TXT
49
+
50
+ # 農林水産業
51
+
52
+ texts << <<-TXT
53
+ 農産物の出荷先別販売金額構成 api/v1-rc.1/agriculture/sales/shipValue
54
+ 農産物の出荷先別経営体数割合 api/v1-rc.1/agriculture/sales/shipRatio
55
+ 経営耕地面積 api/v1-rc.1/agriculture/land/forStacked
56
+ 経営耕地面積規模別の経営体割合 api/v1-rc.1/agriculture/land/Ratio
57
+ 農地流動化率 api/v1-rc.1/agriculture/land/forMobility
58
+ 耕作放棄地率 api/v1-rc.1/agriculture/land/forAbandonment
59
+ 農業部門別販売金額 api/v1-rc.1/agriculture/all/forStacked
60
+ 年間延べ農作業日数 api/v1-rc.1/agriculture/crops/workingDays
61
+ 農産物販売金額(人日あたり) api/v1-rc.1/agriculture/crops/sales
62
+ 農業経営者・農業就業人口の年齢構成 api/v1-rc.1/agriculture/crops/farmersAgeStructure
63
+ 農業経営者・農業就業人口の平均年齢 api/v1-rc.1/agriculture/crops/farmersAverageAge
64
+ 農業生産関連事業の実施状況(経営体数) api/v1-rc.1/agriculture/crops/relatedBusiness
65
+ 農業経営体の法人化率 api/v1-rc.1/agriculture/crops/averageOfCorporate
66
+ TXT
67
+
68
+ texts << <<-TXT
69
+ 林業総収入(総額) api/v1-rc.1/forestry/income/forStacked
70
+ 林産物販売金額(経営体あたり) api/v1-rc.1/forestry/income/forSales
71
+ 林作業請負収入(経営体あたり) api/v1-rc.1/forestry/income/forContractRevenue
72
+ 林産物販売金額帯別の経営体割合 api/v1-rc.1/forestry/income/forSalesRatio
73
+ 林作業請負収入金額帯別の経営体割合 api/v1-rc.1/forestry/income/forContractRevenueRatio
74
+ 主要林業部門別延べ経営体数 api/v1-rc.1/forestry/income/allPortfolio
75
+ 主要林業部門別延べ経営体数構成 api/v1-rc.1/forestry/income/allForStacked
76
+ TXT
77
+
78
+ texts << <<-TXT
79
+ 主要海面漁業種類別延べ経営体数構成 api/v1-rc.1/fishery/sea/staple
80
+ 海面漁獲物等販売金額(総額) api/v1-rc.1/fishery/sea/totalSales
81
+ 海面漁獲物等販売金額(経営体あたり) api/v1-rc.1/fishery/sea/managementUnitSales
82
+ 海面漁獲物等販売金額帯別の経営体割合 api/v1-rc.1/fishery/sea/sales
83
+ 海面漁獲物等出荷先別販売金額構成 api/v1-rc.1/fishery/sea/shipValue
84
+ 海面漁獲物等出荷先別経営体数割合 api/v1-rc.1/fishery/sea/shipRatio
85
+ 海面養殖販売金額(総額) api/v1-rc.1/fishery/sea/aquacultureTotalSales
86
+ 海面養殖販売金額(経営体あたり) api/v1-rc.1/fishery/sea/aquacultureManagementUnitSales
87
+ 海面養殖販売金額帯別の経営体割合 api/v1-rc.1/fishery/sea/aquacultureSales
88
+ TXT
89
+
90
+ # 観光
91
+
92
+ texts << <<-TXT
93
+ 指定地域への国籍別訪問者数 api/v1-rc.1/tourism/foreigners/forFrom
94
+ 指定国籍からの訪問者数 api/v1-rc.1/tourism/foreigners/forTo
95
+ TXT
96
+
97
+ # 人口
98
+
99
+ texts << <<-TXT
100
+ 人口の自然増減 api/v1-rc.1/population/nature
101
+ 人口構成 api/v1-rc.1/population/composition/perYear
102
+ 人口ピラミッド api/v1-rc.1/population/composition/pyramid
103
+ 人口増減率 api/v1-rc.1/population/sum/perYear
104
+ 出生数・死亡数/転入数・転出数 api/v1-rc.1/population/sum/estimate
105
+ 将来人口推計 api/v1-rc.1/population/future/cities
106
+ TXT
107
+
108
+ # 自治体比較
109
+
110
+ texts << <<-TXT
111
+ 企業数 api/v1-rc.1/municipality/company/perYear
112
+ 事業所数 api/v1-rc.1/municipality/plant/perYear
113
+ 創業比率 api/v1-rc.1/municipality/foundation/perYear
114
+ 一人当たり地方税 api/v1-rc.1/municipality/taxes/perYear
115
+ 有効求人倍率 api/v1-rc.1/municipality/job/perYear
116
+ 製造品出荷額 api/v1-rc.1/municipality/manufacture/perYear
117
+ 従業者数(事業所単位) api/v1-rc.1/municipality/employee/perYear
118
+ 付加価値額(企業単位) api/v1-rc.1/municipality/value/perYear
119
+ 労働生産性(企業単位) api/v1-rc.1/municipality/labor/perYear
120
+ 黒字赤字企業比率 api/v1-rc.1/municipality/surplus/perYear
121
+ 一人当たり賃金 api/v1-rc.1/municipality/wages/perYear
122
+ 年間商品販売額 api/v1-rc.1/municipality/sales/perYear
123
+ TXT
124
+
125
+
126
+ texts.each do | text |
127
+ rows = text.split( /\r\n|\r|\n/ )
128
+ rows.each do | row |
129
+ /\A\s*(.+)\s+api\/v1-rc\.1\/([a-zA-Z\/]+)/ =~ row
130
+ type, dirs = $1, $2
131
+ method_name = dirs.split( '/' ).map( &:underscore ).join( '_' )
132
+ puts <<-METHOD
133
+ \# #{ type } (#{ dirs })
134
+ def #{ method_name }( params = {} )
135
+ get( '#{ dirs }', params )
136
+ end
137
+ METHOD
138
+
139
+ puts ''
140
+ end
141
+ end
142
+ end
143
+
144
+ end
@@ -1,7 +1,15 @@
1
+ # RESAS (Regional Economy Society Analyzing System) に関する機能を格納する名前空間
2
+ # @see {https://resas.go.jp/}
1
3
  module Resas
4
+
5
+ # RESAS API を扱うための Gem - トップの名前空間
6
+ # @see {https://opendata.resas-portal.go.jp/}
7
+ # @see {https://opendata.resas-portal.go.jp/docs/api/v1-rc.1/index.html}
2
8
  module Api
3
9
 
4
- VERSION = '0.1.6'
10
+ # この Gem のバージョン
11
+ # @return [String]
12
+ VERSION = '0.2.0'.freeze
5
13
 
6
14
  end
7
15
  end
data/resas-api.gemspec CHANGED
@@ -4,15 +4,15 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'resas/api/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "resas-api"
8
- spec.version = Resas::Api::VERSION
9
- spec.authors = ["Shu Fujita"]
10
- spec.email = ["s.fujita@nard.tech"]
7
+ spec.name = 'resas-api'
8
+ spec.version = Resas::Api::VERSION
9
+ spec.authors = [ 'Shu Fujita' ]
10
+ spec.email = [ 's.fujita@nard.tech' ]
11
11
 
12
- spec.summary = 'API for RESAS'
13
- spec.description = 'RESAS (Regional Economy Society Analyzing System) is developed by Japanese government. (https://resas.go.jp/) This gem enables you to access to API more easily.'
14
- spec.homepage = 'https://github.com/nard-tech/resas-api'
15
- spec.license = "MIT"
12
+ spec.summary = 'Ruby Gem for RESAS (Regional Economy Society Analyzing System) API by Japanese government.'
13
+ spec.description = 'RESAS (Regional Economy Society Analyzing System) is developed by Japanese government. (https://resas.go.jp/) This gem enables you to access to API more easily.'
14
+ spec.homepage = 'https://github.com/nard-tech/resas-api'
15
+ spec.license = 'MIT'
16
16
 
17
17
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
18
  # to allow pushing to a single host or delete this section to allow pushing to any host.
@@ -23,15 +23,20 @@ Gem::Specification.new do |spec|
23
23
  "public gem pushes."
24
24
  end
25
25
 
26
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
27
  f.match(%r{^(test|spec|features)/})
28
28
  end
29
- spec.bindir = "exe"
30
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
- spec.require_paths = ["lib"]
29
+ spec.bindir = 'exe'
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = [ 'lib' ]
32
32
 
33
- spec.add_development_dependency "bundler", "~> 1.13"
34
- spec.add_development_dependency "rake", "~> 10.0"
35
- spec.add_development_dependency "rspec", "~> 3.0"
36
- spec.add_development_dependency 'activesupport', '~> 5.0', '>= 5.0.0.1'
33
+ spec.add_runtime_dependency 'activesupport', '~> 5.0', '>= 5.0.0.1'
34
+
35
+ spec.add_runtime_dependency 'nard-appi', '>= 0.1.0'
36
+ spec.add_runtime_dependency 'faraday', '= 0.10'
37
+ spec.add_runtime_dependency 'faraday_middleware', '= 0.10.1'
38
+
39
+ spec.add_development_dependency 'bundler', '~> 1.13'
40
+ spec.add_development_dependency 'rake', '~> 10.0'
41
+ spec.add_development_dependency 'rspec', '~> 3.0'
37
42
  end