jma 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 68878c8d8158aedb476dcb7a1a32c882a6c2266b9446c9cf3e9ae649a68cc3ab
4
+ data.tar.gz: fadbe66ea597c00942a79f4fbeada1794bd07fd37ed0f5119bf301ae102487dd
5
+ SHA512:
6
+ metadata.gz: ed3f4c5688c8bb9f98b6e4ccb94481bf30c422e5870f472de02400bda1683ef857ae4331df65c0e50124fa3dfbf9772d681f1fef6ee263b353a040d91408e370
7
+ data.tar.gz: c307b363f26c562c1827b3f02b8c8a0bbfa38f5c7b6b05bd3a676f5d28e1ae79b15c5359bcbb4690c5ccc19b1f22b99cbc47f54a21d691420e94a658bfa02af4
data/.gitignore ADDED
@@ -0,0 +1,204 @@
1
+
2
+ # Created by https://www.toptal.com/developers/gitignore/api/macos,ruby,rubymine
3
+ # Edit at https://www.toptal.com/developers/gitignore?templates=macos,ruby,rubymine
4
+
5
+ ### macOS ###
6
+ # General
7
+ .DS_Store
8
+ .AppleDouble
9
+ .LSOverride
10
+
11
+ # Icon must end with two \r
12
+ Icon
13
+
14
+
15
+ # Thumbnails
16
+ ._*
17
+
18
+ # Files that might appear in the root of a volume
19
+ .DocumentRevisions-V100
20
+ .fseventsd
21
+ .Spotlight-V100
22
+ .TemporaryItems
23
+ .Trashes
24
+ .VolumeIcon.icns
25
+ .com.apple.timemachine.donotpresent
26
+
27
+ # Directories potentially created on remote AFP share
28
+ .AppleDB
29
+ .AppleDesktop
30
+ Network Trash Folder
31
+ Temporary Items
32
+ .apdisk
33
+
34
+ ### Ruby ###
35
+ *.gem
36
+ *.rbc
37
+ /.config
38
+ /coverage/
39
+ /InstalledFiles
40
+ /pkg/
41
+ /spec/reports/
42
+ /spec/examples.txt
43
+ /test/tmp/
44
+ /test/version_tmp/
45
+
46
+ # Used by dotenv library to load environment variables.
47
+ # .env
48
+
49
+ # Ignore Byebug command history file.
50
+ .byebug_history
51
+
52
+ ## Specific to RubyMotion:
53
+ .dat*
54
+ .repl_history
55
+ build/
56
+ *.bridgesupport
57
+ build-iPhoneOS/
58
+ build-iPhoneSimulator/
59
+
60
+ ## Specific to RubyMotion (use of CocoaPods):
61
+ #
62
+ # We recommend against adding the Pods directory to your .gitignore. However
63
+ # you should judge for yourself, the pros and cons are mentioned at:
64
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
65
+ # vendor/Pods/
66
+
67
+ ## Documentation cache and generated files:
68
+ /.yardoc/
69
+ /_yardoc/
70
+ /doc/
71
+ /rdoc/
72
+
73
+ ## Environment normalization:
74
+ /.bundle/
75
+ /vendor/bundle
76
+ /lib/bundler/man/
77
+
78
+ # for a library or gem, you might want to ignore these files since the code is
79
+ # intended to run in multiple environments; otherwise, check them in:
80
+ # Gemfile.lock
81
+ # .ruby-version
82
+ # .ruby-gemset
83
+
84
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
85
+ .rvmrc
86
+
87
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
88
+ # .rubocop-https?--*
89
+
90
+ ### RubyMine ###
91
+ # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
92
+ # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
93
+
94
+ # User-specific stuff
95
+ .idea/**/workspace.xml
96
+ .idea/**/tasks.xml
97
+ .idea/**/usage.statistics.xml
98
+ .idea/**/dictionaries
99
+ .idea/**/shelf
100
+
101
+ # AWS User-specific
102
+ .idea/**/aws.xml
103
+
104
+ # Generated files
105
+ .idea/**/contentModel.xml
106
+
107
+ # Sensitive or high-churn files
108
+ .idea/**/dataSources/
109
+ .idea/**/dataSources.ids
110
+ .idea/**/dataSources.local.xml
111
+ .idea/**/sqlDataSources.xml
112
+ .idea/**/dynamic.xml
113
+ .idea/**/uiDesigner.xml
114
+ .idea/**/dbnavigator.xml
115
+
116
+ # Gradle
117
+ .idea/**/gradle.xml
118
+ .idea/**/libraries
119
+
120
+ # Gradle and Maven with auto-import
121
+ # When using Gradle or Maven with auto-import, you should exclude module files,
122
+ # since they will be recreated, and may cause churn. Uncomment if using
123
+ # auto-import.
124
+ # .idea/artifacts
125
+ # .idea/compiler.xml
126
+ # .idea/jarRepositories.xml
127
+ # .idea/modules.xml
128
+ # .idea/*.iml
129
+ # .idea/modules
130
+ # *.iml
131
+ # *.ipr
132
+
133
+ # CMake
134
+ cmake-build-*/
135
+
136
+ # Mongo Explorer plugin
137
+ .idea/**/mongoSettings.xml
138
+
139
+ # File-based project format
140
+ *.iws
141
+
142
+ # IntelliJ
143
+ out/
144
+
145
+ # mpeltonen/sbt-idea plugin
146
+ .idea_modules/
147
+
148
+ # JIRA plugin
149
+ atlassian-ide-plugin.xml
150
+
151
+ # Cursive Clojure plugin
152
+ .idea/replstate.xml
153
+
154
+ # Crashlytics plugin (for Android Studio and IntelliJ)
155
+ com_crashlytics_export_strings.xml
156
+ crashlytics.properties
157
+ crashlytics-build.properties
158
+ fabric.properties
159
+
160
+ # Editor-based Rest Client
161
+ .idea/httpRequests
162
+
163
+ # Android studio 3.1+ serialized cache file
164
+ .idea/caches/build_file_checksums.ser
165
+
166
+ ### RubyMine Patch ###
167
+ # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
168
+
169
+ # *.iml
170
+ # modules.xml
171
+ # .idea/misc.xml
172
+ # *.ipr
173
+
174
+ # Sonarlint plugin
175
+ # https://plugins.jetbrains.com/plugin/7973-sonarlint
176
+ .idea/**/sonarlint/
177
+
178
+ # SonarQube Plugin
179
+ # https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
180
+ .idea/**/sonarIssues.xml
181
+
182
+ # Markdown Navigator plugin
183
+ # https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
184
+ .idea/**/markdown-navigator.xml
185
+ .idea/**/markdown-navigator-enh.xml
186
+ .idea/**/markdown-navigator/
187
+
188
+ # Cache file creation bug
189
+ # See https://youtrack.jetbrains.com/issue/JBR-2257
190
+ .idea/$CACHE_FILE$
191
+
192
+ # CodeStream plugin
193
+ # https://plugins.jetbrains.com/plugin/12206-codestream
194
+ .idea/codestream.xml
195
+
196
+ # End of https://www.toptal.com/developers/gitignore/api/macos,ruby,rubymine
197
+
198
+ routeflagsinc-mvno-aos-tmp-6a87a25fed26
199
+ ods_jp-master
200
+ .idea
201
+
202
+ .rspec_status
203
+
204
+ Gemfile.lock
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.0
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.13.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at info@routeflags.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ gem 'activesupport'
8
+ gem 'faraday'
9
+ gem 'faraday-http-cache'
10
+ gem 'faraday_middleware'
11
+ gem 'multi_xml'
12
+ gem 'rss'
13
+
14
+ group :development do
15
+ gem 'rspec'
16
+ gem 'rubocop'
17
+ gem 'vcr'
18
+ gem 'webmock'
19
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Routeflags,.inc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,188 @@
1
+ # JMA - UNOFFICIAL Japan Meteorological Agency (気象庁) API client
2
+
3
+ This repository contains a gem for the Japan Meteorological Agency (気象庁) for searching new information simplifies. To use the API contains information, see the terms and conditions. ([気象庁ホームページについて](https://www.jma.go.jp/jma/kishou/info/coment.html))
4
+
5
+ ## Features
6
+
7
+ - Overall retrieves information from the three API Feed, Response XML and Response JSON.
8
+ - Favor requests a server with `If-Modified-Since`.
9
+ - Appreciatively displays the entire connection log.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'jma'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ ```shell
22
+ bundle install
23
+ ```
24
+
25
+ Or install it yourself as:
26
+
27
+ ```shell
28
+ gem install jma
29
+ ```
30
+
31
+ ## Configuration
32
+
33
+ To construct a client, available to configure a :response_type, :logger, :temporary_directory and :cache.
34
+
35
+ ```ruby
36
+ client = JMA::Client.new.tap do |config|
37
+ config.response_type = :raw
38
+ config.logger = Logger.new($stdout)
39
+ config.cache = ActiveSupport::Cache.lookup_store(:file_store, 'tmp')
40
+ end
41
+ ```
42
+
43
+ ### Constructor Details
44
+
45
+ Options Hash:
46
+ - :response_type(Symbol) -- The API response type, by default: response_type does not convert the response. You may specify below options..
47
+ - :raw
48
+ - :object
49
+ - :logger(Instance) -- The instance to logging, a default :logger will print log to display.
50
+ - :temporary_directory(String) -- The locate directory to store the cache, a default :temporary_directory is 'tmp' directory at run a gem.
51
+ - :cache(Instance) -- The cache instance that stores the time of the reply date from the API, a default value: cache will use `ActiveSupport::Cache`.
52
+
53
+ ## Usage
54
+
55
+ ### QQTW (Quickest Quick-start in The West)¶
56
+
57
+ ```ruby
58
+ feed = JMA::Client.new.tap do |config|
59
+ config.response_type = :object
60
+ end.request_extra
61
+
62
+ puts <<~EOF
63
+ 発表時刻: #{feed.updated.content.strftime('%Y-%m-%d %H:%M:%S')} 「#{feed.title.content} - #{feed.subtitle.content}」
64
+ #{feed.items.first.content.content}
65
+ #{feed.items.first.id.content}
66
+ #{feed.items.first.author.name.content}
67
+ EOF
68
+
69
+ #=> 発表時刻: 2022-01-07 19:53:07 「高頻度(随時) - JMAXML publishing feed」
70
+ #=> 【石狩・空知・後志地方気象警報・注意報】後志地方では、8日までなだれに注意してください。
71
+ #=> http://www.data.jma.go.jp/developer/xml/data/20220107105234_0_VPWW53_016000.xml
72
+ #=> 札幌管区気象台
73
+ ```
74
+
75
+ ### Client Operations
76
+
77
+ #### Feeds
78
+ When you specify `:object` at `:response_type`, Obtain an `RSS::Atom::Entry` object.
79
+
80
+ #### Regular (定時)
81
+ 気象に関する情報のうち、天気概況など定時に発表されるもの。毎分更新し、直近少なくとも10分入電を掲載しています。*1
82
+
83
+ * `#request_regular()`
84
+ * 高頻度
85
+ * `#request_regular_long()`
86
+ * 長期
87
+
88
+ #### Extra (随時)
89
+ 気象に関する情報のうち、警報・注意報など随時発表されるもの。毎分更新し、直近少なくとも10分入電を掲載しています。*1
90
+
91
+ * `#request_extra()`
92
+ * 高頻度
93
+ * `#request_extra_long()`
94
+ * 長期
95
+
96
+ #### Earthquake and volcano (地震火山)
97
+ 地震、火山に関する情報。毎分更新し、直近少なくとも10分入電を掲載しています。*1
98
+
99
+ * `request_eqvol()`
100
+ * 高頻度
101
+ * `request_eqvol_long()`
102
+ * 長期
103
+
104
+ #### Other (その他)
105
+ その他の情報。毎分更新し、直近少なくとも10分入電を掲載しています。*1
106
+
107
+ * `request_other()`
108
+ * 高頻度
109
+ * `request_other_long()`
110
+ * 長期
111
+
112
+ #### APIs
113
+ When you specify `:object` at `:response_type`, Obtain a `Faraday::Response` object.
114
+
115
+ - `#request_area_codes()`
116
+ - エリア一覧
117
+ - `#request_amedas_latest_time()`
118
+ - アメダス 最新時刻
119
+ - `#request_amedas_map()`
120
+ - アメダス(全国地図表示用)
121
+ - `#request_amedas_point()`
122
+ - アメダス(地点系)
123
+ - `#request_amedas_point_date()`
124
+ - アメダス(地点系)
125
+ - `#request_amedas_table()`
126
+ - アメダス観測所一覧
127
+ - `#request_amedas_selector_information()`
128
+ - アメダス配信データ 各要素の説明
129
+ - `#request_overview_forecast(n)`
130
+ - 天気概況(当日/翌日)
131
+ - `#request_forecast(n)`
132
+ - 天気予報
133
+ - `#request_warning()`
134
+ - エリア毎の注意報/警報
135
+ - `#request_himawari_fd()`
136
+ - ひまわり(フルディスク画像)の配信時刻
137
+ - `#request_himawari_jp()`
138
+ - ひまわり(日本付近画像)の配信時刻
139
+ - `#request_jmatile_n1()`
140
+ - レーダー実況の配信時刻
141
+ - `#request_jmatile_n2()`
142
+ - 降水ナウキャストの配信時刻
143
+
144
+
145
+ ##### Retrieve the Feed details
146
+ - `#request_xml(url)`
147
+
148
+ ```ruby
149
+ jma_client = JMA::Client.new.tap do |config|
150
+ config.response_type = :object
151
+ end
152
+ jma_client.request_xml('http://www.data.jma.go.jp/developer/xml/data/20211229170106_0_VFVO52_400000.xml')
153
+ ```
154
+
155
+
156
+ ## Development
157
+
158
+ ### Test
159
+
160
+ ```bash
161
+ rspec spec
162
+ ```
163
+
164
+ ## Contributing
165
+
166
+ Bug reports and pull requests are welcome on Bitbucket at https://bitbucket.org/routeflagsinc/jma. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://bitbucket.org/routeflagsinc/jma/src/master/CODE_OF_CONDUCT.md).
167
+
168
+ ## License
169
+
170
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
171
+
172
+ ## Inspiration
173
+ - [octokit/octokit.rb: Ruby toolkit for the GitHub API](https://github.com/octokit/octokit.rb)
174
+
175
+ ## Copyrights
176
+ *1 出典:気象庁ホームページ [気象庁防災情報XML - DATA GO JP](https://www.data.go.jp/data/dataset/mlit_20170902_0034)
177
+
178
+ ## References
179
+ - [気象庁ホームページについて](https://www.jma.go.jp/jma/kishou/info/coment.html)
180
+ - [気象庁防災情報XMLフォーマット 情報提供ページ](http://xml.kishou.go.jp/)
181
+ - [気象庁 | 気象庁防災情報XMLフォーマット形式電文(PULL型)](http://xml.kishou.go.jp/xmlpull.html)
182
+ - [気象庁防災情報XML - DATA GO JP](https://www.data.go.jp/data/dataset/mlit_20170902_0034)
183
+ - [【Colab / Python】気象庁API - 気象データの収集](https://qiita.com/T_Ryota/items/ef96d6575404a0fd46dd)
184
+ - [気象庁防災XMLのPull型提供(Atom Feed)の品質 - Qiita](https://qiita.com/e_toyoda/items/2942903d406ed96c6bed)
185
+ - [気象庁防災XMLのPull型提供(Atom Feed)について - Qiita](https://qiita.com/e_toyoda/items/185c1dea055e230918b9)
186
+ - [気象庁ホームページ防災気象情報のURL構造 - Qiita](https://qiita.com/e_toyoda/items/7a293313a725c4d306c0)
187
+ - [ピュアRubyでAtomも対応したRSS Parserを作ってみたMEMO ](https://madogiwa0124.hatenablog.com/entry/2019/03/26/214228)
188
+ - [現在出ている気象警報・注意報を取得する](https://qiita.com/conqueror/items/f72cfdedd01149c35313)
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/jma.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'jma/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'jma'
9
+ spec.version = JMA::VERSION
10
+ spec.authors = ['smapira']
11
+ spec.email = ['info@routeflags.com']
12
+
13
+ spec.summary = 'UNOFFICIAL Japan Meteorological Agency (気象庁) API client'
14
+ spec.description = 'This repository contains a gem for the Japan Meteorological Agency (気象庁) for searching new information simplifies.'
15
+ spec.homepage = 'https://bitbucket.org/routeflagsinc/jma/'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'activesupport'
26
+ spec.add_dependency 'faraday'
27
+ spec.add_dependency 'faraday-http-cache'
28
+ spec.add_dependency 'faraday_middleware'
29
+ spec.add_dependency 'multi_xml'
30
+ spec.add_dependency 'rss'
31
+ spec.add_development_dependency 'bundler'
32
+ spec.add_development_dependency 'rake'
33
+ spec.add_development_dependency 'rspec'
34
+ spec.add_development_dependency 'rubocop'
35
+ end
data/lib/jma/client.rb ADDED
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jma/configurable'
4
+ require 'jma/error'
5
+ require 'jma/raise_error'
6
+ require 'faraday'
7
+ require 'faraday-http-cache'
8
+ require 'faraday_middleware'
9
+ require 'net/http'
10
+ require 'rss'
11
+
12
+ module JMA
13
+ class Client
14
+ include JMA::Configurable
15
+
16
+ ENDPOINTS.merge(FEED_ENDPOINTS).each do |method_name, path|
17
+ define_method method_name do |*arguments|
18
+ if ENDPOINTS.keys.include? method_name
19
+ request_api(path, *arguments)
20
+ else
21
+ request_feed path
22
+ end
23
+ end
24
+ end
25
+
26
+ # The root path that Faraday is being loaded from.
27
+ #
28
+ # This is the root from where the libraries are auto-loaded.
29
+ #
30
+ # @return [Faraday::Response]
31
+ # @param url [String]
32
+ def request_xml(url)
33
+ connection = build_connection url
34
+ connection.headers = connection_options[:headers]
35
+ connection.get url
36
+ end
37
+
38
+ private
39
+
40
+ def switch_parser(path)
41
+ return nil if response_type == RESPONSE_TYPE_RAW || path == 'bosai/amedas/data/latest_time.txt'
42
+ return :json if ENDPOINTS.values.include? path
43
+
44
+ :xml
45
+ end
46
+
47
+ def build_connection(path)
48
+ type = switch_parser(path)
49
+ Faraday.new do |f|
50
+ f.use Faraday::HttpCache, store: cache, serializer: Marshal
51
+ f.use JMA::Response::RaiseError
52
+ f.response :follow_redirects
53
+ f.response :logger, logger
54
+ f.response type unless type.nil?
55
+ f.request :retry
56
+ f.adapter Faraday.default_adapter
57
+ end
58
+ end
59
+
60
+ def build_api_endpoint(path, arguments)
61
+ if arguments.size == 3
62
+ return [API_BASE_URL,
63
+ path, arguments[0], "#{arguments[1]}_#{arguments[2]}.json"].join('/')
64
+ end
65
+ return [API_BASE_URL, path, "#{arguments.first}.json"].join('/') if arguments.size == 1
66
+
67
+ [API_BASE_URL, path].join('/')
68
+ end
69
+
70
+ def request_api(path, *arguments)
71
+ endpoint = build_api_endpoint path, arguments
72
+ connection = build_connection path
73
+ connection.headers = connection_options[:headers]
74
+ connection.get endpoint
75
+ end
76
+
77
+ def fetch_atom_feed(endpoint)
78
+ client = Net::HTTP.new(endpoint.hostname, endpoint.port)
79
+ client.use_ssl = true
80
+ client.verify_mode = OpenSSL::SSL::VERIFY_NONE
81
+ client.set_debug_output logger
82
+ last_modified_at = cache.read endpoint.path.split(%r{/}).join('_').to_sym
83
+ request = Net::HTTP::Get.new(endpoint.path,
84
+ { 'If-Modified-Since' => last_modified_at }
85
+ .merge(connection_options[:headers]))
86
+ client.request request
87
+ end
88
+
89
+ def request_feed(path)
90
+ endpoint = URI("#{FEED_BASE_URL}/#{path}")
91
+ response = fetch_atom_feed endpoint
92
+ error = Error.from_response response
93
+ raise error if error
94
+
95
+ if response.is_a?(Net::HTTPSuccess)
96
+ cache.write(endpoint.path.split(%r{/}).join('_').to_sym,
97
+ response['last-modified'])
98
+ end
99
+
100
+ if response_type == RESPONSE_TYPE_RAW
101
+ response
102
+ elsif response.is_a?(Net::HTTPSuccess)
103
+ RSS::Parser.parse(response.body)
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+ require 'active_support'
5
+
6
+ module JMA
7
+ module Configurable
8
+ attr_writer :response_type, :logger, :temporary_directory, :cache
9
+
10
+ API_BASE_URL = 'https://www.jma.go.jp'
11
+ FEED_BASE_URL = 'https://www.data.jma.go.jp'
12
+
13
+ ENDPOINTS = {
14
+ request_area_codes: 'bosai/common/const/area.json',
15
+ request_amedas_table: 'bosai/amedas/const/amedastable.json',
16
+ request_amedas_selector_information: 'bosai/const/selectorinfos/amedas.json',
17
+ request_forecast: 'bosai/forecast/data/forecast',
18
+ request_overview_forecast: 'bosai/forecast/data/overview_forecast',
19
+ request_amedas_map: 'bosai/amedas/data/map',
20
+ request_amedas_point: 'bosai/amedas/data/point',
21
+ request_amedas_point_date: 'bosai/amedas/data/point',
22
+ request_amedas_latest_time: 'bosai/amedas/data/latest_time.txt',
23
+ request_jmatile_n1: 'bosai/jmatile/data/nowc/targetTimes_N1.json',
24
+ request_jmatile_n2: 'bosai/jmatile/data/nowc/targetTimes_N2.json',
25
+ request_himawari_jp: 'bosai/himawari/data/satimg/targetTimes_jp.json',
26
+ request_himawari_fd: 'bosai/himawari/data/satimg/targetTimes_fd.json',
27
+ request_warning: 'bosai/warning/data/warning'
28
+ }.freeze
29
+
30
+ FEED_ENDPOINTS = {
31
+ request_regular: 'developer/xml/feed/regular.xml',
32
+ request_extra: 'developer/xml/feed/extra.xml',
33
+ request_eqvol: 'developer/xml/feed/eqvol.xml',
34
+ request_other: 'developer/xml/feed/other.xml',
35
+ request_regular_long: 'developer/xml/feed/regular_l.xml',
36
+ request_extra_long: 'developer/xml/feed/extra_l.xml',
37
+ request_eqvol_long: 'developer/xml/feed/eqvol_l.xml',
38
+ request_other_long: 'developer/xml/feed/other_l.xml'
39
+ }.freeze
40
+
41
+ USER_AGENT = "JMA Ruby Gem #{JMA::VERSION}".freeze
42
+
43
+ TEMPORARY = 'tmp'
44
+
45
+ RESPONSE_TYPE_RAW = :raw
46
+
47
+ WARNING_INFORMATION = {
48
+ '02' => '暴風雪警報',
49
+ '03' => '大雨警報',
50
+ '04' => '洪水警報',
51
+ '05' => '暴風警報',
52
+ '06' => '大雪警報',
53
+ '07' => '波浪警報',
54
+ '08' => '高潮警報',
55
+ '10' => '大雨注意報',
56
+ '12' => '大雪警報',
57
+ '13' => '風雪注意報',
58
+ '14' => '雷注意報',
59
+ '15' => '強風注意報',
60
+ '16' => '波浪注意報',
61
+ '17' => '融雪注意報',
62
+ '18' => '洪水注意報',
63
+ '19' => '高潮注意報',
64
+ '20' => '濃霧注意報',
65
+ '21' => '乾燥注意報',
66
+ '22' => 'なだれ注意報',
67
+ '23' => '低温注意報',
68
+ '24' => '霜注意報',
69
+ '25' => '着氷注意報',
70
+ '26' => '着雪注意報',
71
+ '32' => '暴風雪特別警報',
72
+ '33' => '大雨特別警報',
73
+ '35' => '暴風特別警報',
74
+ '36' => '大雪特別警報',
75
+ '37' => '波浪特別警報'
76
+ }.freeze
77
+
78
+ # Default options for Faraday::Connection
79
+ # @return [Hash]
80
+ def connection_options
81
+ { headers: { user_agent: user_agent } }
82
+ end
83
+
84
+ # Default User-Agent header string from ENV or {USER_AGENT}
85
+ # @return [String]
86
+ def user_agent
87
+ ENV['JMA_USER_AGENT'] || USER_AGENT
88
+ end
89
+
90
+ # Default Logger object from argument or Logger.new(STDOUT)
91
+ # @return [String]
92
+ def logger
93
+ @logger || Logger.new($stdout)
94
+ end
95
+
96
+ # Default path to temporary directory location from argument or {TEMPORARY}
97
+ # @return [String]
98
+ def temporary_directory
99
+ @temporary_directory || TEMPORARY
100
+ end
101
+
102
+ # Default path to temporary directory location from argument or {TEMPORARY}
103
+ # @return [String]
104
+ def response_type
105
+ @response_type || RESPONSE_TYPE_RAW
106
+ end
107
+
108
+ def cache
109
+ @cache || ActiveSupport::Cache.lookup_store(:file_store, temporary_directory)
110
+ end
111
+ end
112
+ end
data/lib/jma/error.rb ADDED
@@ -0,0 +1,235 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JMA
4
+ # Custom error class for rescuing from all API errors
5
+ # Inspired by https://github.com/octokit/octokit.rb
6
+ class Error < StandardError
7
+ # Returns the appropriate JMA::Error subclass based
8
+ # on status and response message
9
+ #
10
+ # @param [Net::HTTPResponse] response HTTP response
11
+ # @return [JMA::Error]
12
+ def self.from_response(response)
13
+ if response.methods.include? :code
14
+ return unless (klass = match_status response.code.to_i, response.body.to_s)
15
+ else
16
+ return unless (klass = match_status response[:status].to_i,
17
+ response[:body].to_s)
18
+
19
+ end
20
+ klass.new(response)
21
+ end
22
+
23
+ def self.match_status(status, body)
24
+ case status
25
+ when 400
26
+ JMA::BadRequest
27
+ when 401
28
+ error_for_401
29
+ when 403
30
+ error_for_403 body
31
+ when 404
32
+ error_for_404 body
33
+ when 405
34
+ JMA::MethodNotAllowed
35
+ when 406
36
+ JMA::NotAcceptable
37
+ when 409
38
+ JMA::Conflict
39
+ when 400..499
40
+ match_status_410_500 status
41
+ when 500..599
42
+ match_status_over_500 status
43
+ end
44
+ end
45
+
46
+ def self.match_status_410_500(status)
47
+ case status
48
+ when 415
49
+ JMA::UnsupportedMediaType
50
+ when 422
51
+ JMA::UnprocessableEntity
52
+ when 451
53
+ JMA::UnavailableForLegalReasons
54
+ when 400..499
55
+ JMA::ClientError
56
+ else
57
+ JMA::ClientError
58
+ end
59
+ end
60
+
61
+ def self.match_status_over_500(status)
62
+ case status
63
+ when 500
64
+ JMA::InternalServerError
65
+ when 501
66
+ JMA::NotImplemented
67
+ when 502
68
+ JMA::BadGateway
69
+ when 503
70
+ JMA::ServiceUnavailable
71
+ when 500..599
72
+ JMA::ServerError
73
+ else
74
+ JMA::ServerError
75
+ end
76
+ end
77
+
78
+ def initialize(response = nil)
79
+ @response = response
80
+ super(build_error_message)
81
+ end
82
+
83
+ # Returns most appropriate error for 401 HTTP status code
84
+ # @private
85
+ def self.error_for_401
86
+ JMA::Unauthorized
87
+ end
88
+
89
+ # Returns most appropriate error for 403 HTTP status code
90
+ # @private
91
+ def self.error_for_403(body)
92
+ case body
93
+ when /rate limit exceeded/i
94
+ JMA::TooManyRequests
95
+ when /login attempts exceeded/i
96
+ JMA::TooManyLoginAttempts
97
+ when /abuse/i
98
+ JMA::AbuseDetected
99
+ else
100
+ JMA::Forbidden
101
+ end
102
+ end
103
+
104
+ # Return most appropriate error for 404 HTTP status code
105
+ # @private
106
+ def self.error_for_404
107
+ JMA::NotFound
108
+ end
109
+
110
+ def build_error_message
111
+ return nil if @response.nil?
112
+
113
+ if @response.methods.include? :code
114
+ "#{@response.code} - #{@response.message}"
115
+ else
116
+ message = "#{@response[:method].to_s.upcase} ".dup
117
+ message << "#{@response[:url]}: "
118
+ message << (@response[:status]).to_s
119
+ message
120
+ end
121
+ end
122
+ end
123
+
124
+ # Raised on errors in the 400-499 range
125
+ class ClientError < Error
126
+ end
127
+
128
+ # Raised when API returns a 400 HTTP status code
129
+ class BadRequest < ClientError
130
+ end
131
+
132
+ # Raised when API returns a 401 HTTP status code
133
+ class Unauthorized < ClientError
134
+ end
135
+
136
+ # Raised when API returns a 403 HTTP status code
137
+ class Forbidden < ClientError
138
+ end
139
+
140
+ # Raised when API returns a 403 HTTP status code
141
+ # and body matches 'rate limit exceeded'
142
+ class TooManyRequests < Forbidden
143
+ end
144
+
145
+ # Raised when API returns a 403 HTTP status code
146
+ # and body matches 'login attempts exceeded'
147
+ class TooManyLoginAttempts < Forbidden
148
+ end
149
+
150
+ # Raised when API returns a 403 HTTP status code
151
+ # and body matches 'abuse'
152
+ class AbuseDetected < Forbidden
153
+ end
154
+
155
+ # Raised when API returns a 403 HTTP status code
156
+ # and body matches 'repository access blocked'
157
+ class RepositoryUnavailable < Forbidden
158
+ end
159
+
160
+ # Raised when API returns a 403 HTTP status code
161
+ # and body matches 'email address must be verified'
162
+ class UnverifiedEmail < Forbidden
163
+ end
164
+
165
+ # Raised when API returns a 403 HTTP status code
166
+ # and body matches 'account was suspended'
167
+ class AccountSuspended < Forbidden
168
+ end
169
+
170
+ # Raised when API returns a 404 HTTP status code
171
+ class NotFound < ClientError
172
+ end
173
+
174
+ # Raised when API returns a 404 HTTP status code
175
+ # and body matches 'Branch not protected'
176
+ class BranchNotProtected < ClientError
177
+ end
178
+
179
+ # Raised when API returns a 405 HTTP status code
180
+ class MethodNotAllowed < ClientError
181
+ end
182
+
183
+ # Raised when API returns a 406 HTTP status code
184
+ class NotAcceptable < ClientError
185
+ end
186
+
187
+ # Raised when API returns a 409 HTTP status code
188
+ class Conflict < ClientError
189
+ end
190
+
191
+ # Raised when API returns a 414 HTTP status code
192
+ class UnsupportedMediaType < ClientError
193
+ end
194
+
195
+ # Raised when API returns a 422 HTTP status code
196
+ class UnprocessableEntity < ClientError
197
+ end
198
+
199
+ # Raised when API returns a 451 HTTP status code
200
+ class UnavailableForLegalReasons < ClientError
201
+ end
202
+
203
+ # Raised on errors in the 500-599 range
204
+ class ServerError < Error
205
+ end
206
+
207
+ # Raised when API returns a 500 HTTP status code
208
+ class InternalServerError < ServerError
209
+ end
210
+
211
+ # Raised when API returns a 501 HTTP status code
212
+ class NotImplemented < ServerError
213
+ end
214
+
215
+ # Raised when API returns a 502 HTTP status code
216
+ class BadGateway < ServerError
217
+ end
218
+
219
+ # Raised when API returns a 503 HTTP status code
220
+ class ServiceUnavailable < ServerError
221
+ end
222
+
223
+ # Raised when client fails to provide valid Content-Type
224
+ class MissingContentType < ArgumentError
225
+ end
226
+
227
+ # Raised when a method requires an application client_id
228
+ # and secret but none is provided
229
+ class ApplicationCredentialsRequired < StandardError
230
+ end
231
+
232
+ # Raised when a repository is created with an invalid format
233
+ class InvalidRepository < ArgumentError
234
+ end
235
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+
5
+ module JMA
6
+ # Faraday response middleware
7
+ module Response
8
+ # This class raises an API-flavored exception based
9
+ # HTTP status codes returned by the API
10
+ class RaiseError < Faraday::Response::Middleware
11
+ def on_complete(response)
12
+ return unless (error = JMA::Error.from_response(response))
13
+
14
+ raise error
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JMA
4
+ VERSION = '0.1.0'
5
+ end
data/lib/jma.rb ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jma/version'
4
+ require 'jma/client'
data/tmp/.keep ADDED
File without changes
metadata ADDED
@@ -0,0 +1,200 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jma
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - smapira
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-01-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday-http-cache
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday_middleware
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: multi_xml
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rss
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: This repository contains a gem for the Japan Meteorological Agency (気象庁)
154
+ for searching new information simplifies.
155
+ email:
156
+ - info@routeflags.com
157
+ executables: []
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - ".gitignore"
162
+ - ".ruby-version"
163
+ - ".travis.yml"
164
+ - CODE_OF_CONDUCT.md
165
+ - Gemfile
166
+ - LICENSE.txt
167
+ - README.md
168
+ - Rakefile
169
+ - jma.gemspec
170
+ - lib/jma.rb
171
+ - lib/jma/client.rb
172
+ - lib/jma/configurable.rb
173
+ - lib/jma/error.rb
174
+ - lib/jma/raise_error.rb
175
+ - lib/jma/version.rb
176
+ - tmp/.keep
177
+ homepage: https://bitbucket.org/routeflagsinc/jma/
178
+ licenses:
179
+ - MIT
180
+ metadata: {}
181
+ post_install_message:
182
+ rdoc_options: []
183
+ require_paths:
184
+ - lib
185
+ required_ruby_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ required_rubygems_version: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ requirements: []
196
+ rubygems_version: 3.3.3
197
+ signing_key:
198
+ specification_version: 4
199
+ summary: UNOFFICIAL Japan Meteorological Agency (気象庁) API client
200
+ test_files: []