jma_feed 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e548f6f36c66d5fbf33b4827e9dc92ebb5a3db3e7afe5d65ab6e3438e7c2636
4
- data.tar.gz: 8624233d9b3ad4e95b31ee409103aed698df7be193f32a3dd1b092cb049bfd52
3
+ metadata.gz: 5c76dfa5b24cec264aa047de3a21871a1cce3fcf7bee5908a88ecd0029d8fda1
4
+ data.tar.gz: 10abdfb01733d13e2d733a6f2499e6a2a614f3fb181797852602db2f5b45441a
5
5
  SHA512:
6
- metadata.gz: 3e0ce670bfdeb22f6ff8a1bfeae084643acbe03706c73dce37eb5851720852bcd79c04da42fc2dcfebb7c04afa9af679e75b1fcecebe7a1c722fd576d6b0cbf9
7
- data.tar.gz: 48de3a2546694270418a5b4175e97187aedb65c876863b66bb58f90f899b0d034100cc11d4c04a7181d44a58347b49ad534961930a3c0e29701bc07e178b069f
6
+ metadata.gz: 31aa0529c9797ef626849ecf3392dbea29a449917751f142c765dc0818b4f73e35642d4e481d9f05fd92acf8ec23ef2d51a333f8c6d9cdc7af2a1b92ce2affc7
7
+ data.tar.gz: ca936a1a99345e281a278c8aa1d234397ca90c600c2a4e3efc500d2bcdb463276ee28faac9bd45ba5d5bbd0dc4fc67773c0148f620635023517b82784bcce3cf
@@ -0,0 +1,7 @@
1
+ class JMAFeed::Extra
2
+ include JMAFeed::Api
3
+
4
+ def feed_type
5
+ :extra
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class JMAFeed::Regular
2
+ include JMAFeed::Api
3
+
4
+ def feed_type
5
+ :regular
6
+ end
7
+ end
data/lib/jma_feed/api.rb CHANGED
@@ -1,20 +1,20 @@
1
1
  require "net/http"
2
2
 
3
- class JMAFeed::Api
4
- def initialize
5
- end
3
+ module JMAFeed::Api
6
4
 
7
- def get_feed_regular(if_modified_since: nil, &block)
8
- res = execute(:get, "https://www.data.jma.go.jp/developer/xml/feed/regular.xml", headers: {
9
- "If-Modified-Since" => if_modified_since ?
10
- if_modified_since.httpdate :
11
- nil
12
- }, &block)
13
- JMAFeed::Result.new(res)
5
+ def self.type(feed_type, **args)
6
+ feed_type = feed_type.to_sym
7
+ if feed_type == :regular
8
+ JMAFeed::Regular.new(**args)
9
+ elsif feed_type == :extra
10
+ JMAFeed::Extra.new(**args)
11
+ else
12
+ raise "feed_type=#{feed_type} not supported"
13
+ end
14
14
  end
15
15
 
16
- def get_feed_extra(if_modified_since: nil, &block)
17
- res = execute(:get, "https://www.data.jma.go.jp/developer/xml/feed/extra.xml", headers: {
16
+ def get(if_modified_since: nil, &block)
17
+ res = execute(:get, "https://www.data.jma.go.jp/developer/xml/feed/#{feed_type}.xml", headers: {
18
18
  "If-Modified-Since" => if_modified_since ?
19
19
  if_modified_since.httpdate :
20
20
  nil
@@ -22,6 +22,24 @@ class JMAFeed::Api
22
22
  JMAFeed::Result.new(res)
23
23
  end
24
24
 
25
+ # def get_feed_regular(if_modified_since: nil, &block)
26
+ # res = execute(:get, "https://www.data.jma.go.jp/developer/xml/feed/regular.xml", headers: {
27
+ # "If-Modified-Since" => if_modified_since ?
28
+ # if_modified_since.httpdate :
29
+ # nil
30
+ # }, &block)
31
+ # JMAFeed::Result.new(res)
32
+ # end
33
+
34
+ # def get_feed_extra(if_modified_since: nil, &block)
35
+ # res = execute(:get, "https://www.data.jma.go.jp/developer/xml/feed/extra.xml", headers: {
36
+ # "If-Modified-Since" => if_modified_since ?
37
+ # if_modified_since.httpdate :
38
+ # nil
39
+ # }, &block)
40
+ # JMAFeed::Result.new(res)
41
+ # end
42
+
25
43
  def execute(verb, uri, query: nil, body: nil, headers: {})
26
44
  uri = uri.is_a?(URI::HTTP) ? uri : URI.parse(uri)
27
45
  request = build_request(verb, uri, query: query, body: body)
@@ -1,4 +1,4 @@
1
- class JMAFeed::Alert < Struct.new(:code, :name, :risk_level, :cluster, keyword_init: true)
1
+ class JMAFeed::WeatherAlert < Struct.new(:code, :name, :risk_level, :cluster, keyword_init: true)
2
2
  # 気象警報・注意(H27)_解説資料.pdf > 別表1
3
3
  # https://www.jma.go.jp/jma/kishou/know/bosai/warning_kind.html
4
4
  LIST = {
@@ -161,12 +161,7 @@ class JMAFeed::Alert < Struct.new(:code, :name, :risk_level, :cluster, keyword_i
161
161
  },
162
162
  }
163
163
 
164
- # def self.clusters
165
- # LIST.map{|k,v| v[:cluster]}.uniq
166
- # end
167
-
168
164
  def self.clusters
169
- # LIST.map{|k,v| v[:cluster]}.uniq
170
165
  all.group_by(&:cluster)
171
166
  end
172
167
 
@@ -1,3 +1,5 @@
1
+ require "csv"
2
+
1
3
  class JMAFeed::Report < Struct.new(
2
4
  :name, :title, :kind, :category, :code_represented, :codes, keyword_init: true
3
5
  )
@@ -34,6 +36,10 @@ class JMAFeed::Report < Struct.new(
34
36
  end
35
37
  end
36
38
 
39
+ def get
40
+ @data ||= load
41
+ end
42
+
37
43
  def build_by_csv_row(row)
38
44
  codes = build_codes(row[:code])
39
45
  code_represented = build_code_represented(row[:code])
@@ -43,7 +49,7 @@ class JMAFeed::Report < Struct.new(
43
49
  kind: row[:kind].split('/'),
44
50
  category: row[:category],
45
51
  code_represented: code_represented,
46
- code: codes,
52
+ codes: codes,
47
53
  )
48
54
  end
49
55
 
@@ -27,14 +27,14 @@ class JMAFeed::VPWW54 < JMAFeed::ReportEntry
27
27
  text_node :note, collection: true
28
28
  end
29
29
 
30
- def alert
31
- @alert ||= JMAFeed::Alert.all.find{|a| a.code == code}
30
+ def weather_alert
31
+ @weather_alert ||= JMAFeed::WeatherAlert.all.find{|a| a.code == code}
32
32
  end
33
33
  end
34
34
  text_node :change_status
35
35
  xml_node :area, type: "JMAFeed::JMX::Area"
36
36
 
37
- JMAFeed::Alert.clusters.each do |cluster,alerts|
37
+ JMAFeed::WeatherAlert.clusters.each do |cluster,alerts|
38
38
  define_method cluster do
39
39
  kind.find{|k| alerts.map(&:code).include?(k.code)}
40
40
  end
@@ -13,52 +13,47 @@ class JMAFeed::Result
13
13
  http_result.body
14
14
  end
15
15
 
16
+ def modified?
17
+ http_result.kind_of?(Net::HTTPSuccess)
18
+ end
19
+
16
20
  def last_modified
17
21
  DateTime.parse(http_result["Last-Modified"])
18
22
  end
19
23
 
20
24
  def doc
21
- @doc ||= Nokogiri::XML(body)
25
+ @doc ||= JMAFeed::ResultDoc.new(body)
22
26
  end
23
27
 
24
- def namespace
25
- {atom: doc.root.namespace.href}
26
- end
28
+ # def namespace
29
+ # {atom: doc.root.namespace.href}
30
+ # end
27
31
 
28
- def text(name)
29
- doc.xpath("/atom:feed/atom:#{name}", namespace).text
30
- end
32
+ # def text(name)
33
+ # doc.xpath("/atom:feed/atom:#{name}", namespace).text
34
+ # end
31
35
 
32
36
  def title
33
- text('title')
37
+ doc.title
34
38
  end
35
39
 
36
40
  def subtitle
37
- text('subtitle')
41
+ doc.subtitle
38
42
  end
39
43
 
40
44
  def id
41
- text('id')
45
+ doc.id
42
46
  end
43
47
 
44
48
  def updated
45
- DateTime.parse(text('updated'))
49
+ doc.updated
46
50
  end
47
51
 
48
52
  def related
49
- text('link[@rel="related"]/@href')
53
+ doc.related
50
54
  end
51
55
 
52
56
  def entries
53
- @entries ||= doc.xpath('/atom:feed/atom:entry', namespace).map do |entry|
54
- JMAFeed::ResultEntry.new(
55
- title: entry.xpath('atom:title', namespace).text,
56
- link: entry.xpath('atom:link/@href', namespace).text,
57
- id: entry.xpath('atom:id', namespace).text,
58
- updated: entry.xpath('atom:updated', namespace).text,
59
- author: entry.xpath('atom:author/atom:name', namespace).text,
60
- content: entry.xpath('atom:content', namespace).text,
61
- )
62
- end
57
+ doc.entries
63
58
  end
64
59
  end
@@ -0,0 +1,49 @@
1
+ class JMAFeed::ResultDoc
2
+ attr_reader :doc
3
+
4
+ def initialize(body)
5
+ @body = body
6
+ @doc = Nokogiri::XML(body)
7
+ end
8
+
9
+ def namespace
10
+ {atom: doc.root.namespace.href}
11
+ end
12
+
13
+ def text(name)
14
+ doc.xpath("/atom:feed/atom:#{name}", namespace).text
15
+ end
16
+
17
+ def title
18
+ text('title')
19
+ end
20
+
21
+ def subtitle
22
+ text('subtitle')
23
+ end
24
+
25
+ def id
26
+ text('id')
27
+ end
28
+
29
+ def updated
30
+ DateTime.parse(text('updated'))
31
+ end
32
+
33
+ def related
34
+ text('link[@rel="related"]/@href')
35
+ end
36
+
37
+ def entries
38
+ @entries ||= doc.xpath('/atom:feed/atom:entry', namespace).map do |entry|
39
+ JMAFeed::ResultEntry.new(
40
+ title: entry.xpath('atom:title', namespace).text,
41
+ link: entry.xpath('atom:link/@href', namespace).text,
42
+ id: entry.xpath('atom:id', namespace).text,
43
+ updated: entry.xpath('atom:updated', namespace).text,
44
+ author: entry.xpath('atom:author/atom:name', namespace).text,
45
+ content: entry.xpath('atom:content', namespace).text,
46
+ )
47
+ end
48
+ end
49
+ end
@@ -1,3 +1,3 @@
1
1
  module JMAFeed
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/jma_feed.rb CHANGED
@@ -8,10 +8,13 @@ end
8
8
 
9
9
  require "jma_feed/version"
10
10
  require "jma_feed/report"
11
- require "jma_feed/result_entry"
12
11
  require "jma_feed/result"
12
+ require "jma_feed/result_doc"
13
+ require "jma_feed/result_entry"
13
14
  require "jma_feed/api"
14
- require "jma_feed/entity/alert"
15
+ require "jma_feed/api/extra"
16
+ require "jma_feed/api/regular"
17
+ require "jma_feed/entity/weather_alert"
15
18
  require "jma_feed/entity/risk_level"
16
19
 
17
20
  require "jma_feed/report_entry"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jma_feed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - metheglin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-05 00:00:00.000000000 Z
11
+ date: 2024-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.0.3
47
+ version: 0.0.4
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.0.3
54
+ version: 0.0.4
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: giri
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -76,8 +76,10 @@ files:
76
76
  - data/jmaxml_20240216_format_v1_3_hyo1.csv
77
77
  - lib/jma_feed.rb
78
78
  - lib/jma_feed/api.rb
79
- - lib/jma_feed/entity/alert.rb
79
+ - lib/jma_feed/api/extra.rb
80
+ - lib/jma_feed/api/regular.rb
80
81
  - lib/jma_feed/entity/risk_level.rb
82
+ - lib/jma_feed/entity/weather_alert.rb
81
83
  - lib/jma_feed/report.rb
82
84
  - lib/jma_feed/report_entry.rb
83
85
  - lib/jma_feed/report_entry/jmx/area.rb
@@ -91,6 +93,7 @@ files:
91
93
  - lib/jma_feed/report_entry/vpww54.rb
92
94
  - lib/jma_feed/report_entry/vxko.rb
93
95
  - lib/jma_feed/result.rb
96
+ - lib/jma_feed/result_doc.rb
94
97
  - lib/jma_feed/result_entry.rb
95
98
  - lib/jma_feed/version.rb
96
99
  homepage: https://rubygems.org/gems/jma_feed