Class: JMAFeed::Report
- Inherits:
-
Struct
- Object
- Struct
- JMAFeed::Report
- Defined in:
- lib/jma_feed/report.rb
Constant Summary collapse
- CSV_ROW_SEP =
"\r\n"- NUM_HEADER_ROWS =
3- HEADERS =
i( number category kind name title code compression_format size_average size_max memo code_old )
Instance Attribute Summary collapse
-
#category ⇒ Object
Returns the value of attribute category.
-
#code_represented ⇒ Object
Returns the value of attribute code_represented.
-
#codes ⇒ Object
Returns the value of attribute codes.
-
#kind ⇒ Object
Returns the value of attribute kind.
-
#name ⇒ Object
Returns the value of attribute name.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
- .build_by_csv_row(row) ⇒ Object
- .build_code_represented(raw_code) ⇒ Object
-
.build_codes(raw_code) ⇒ Object
build_code(‘VGSK50’) => ‘VGSK50’ build_code(‘VPTWii(ii=40-45)’) => [“VPTW40”, “VPTW41”, “VPTW42”, “VPTW43”, “VPTW44”, “VPTW45”] build_code(‘VXKO(ii=50-89)’) => [“VXKO50”, “VXKO51”, “VXKO52”, “VXKO53”, “VXKO54”, “VXKO55”, …].
- .get ⇒ Object
- .load(**args) ⇒ Object
- .load_csv(version: "20240216") ⇒ Object
- .parse_raw_code(raw_code) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#category ⇒ Object
Returns the value of attribute category
3 4 5 |
# File 'lib/jma_feed/report.rb', line 3 def category @category end |
#code_represented ⇒ Object
Returns the value of attribute code_represented
3 4 5 |
# File 'lib/jma_feed/report.rb', line 3 def code_represented @code_represented end |
#codes ⇒ Object
Returns the value of attribute codes
3 4 5 |
# File 'lib/jma_feed/report.rb', line 3 def codes @codes end |
#kind ⇒ Object
Returns the value of attribute kind
3 4 5 |
# File 'lib/jma_feed/report.rb', line 3 def kind @kind end |
#name ⇒ Object
Returns the value of attribute name
3 4 5 |
# File 'lib/jma_feed/report.rb', line 3 def name @name end |
#title ⇒ Object
Returns the value of attribute title
3 4 5 |
# File 'lib/jma_feed/report.rb', line 3 def title @title end |
Class Method Details
.build_by_csv_row(row) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/jma_feed/report.rb', line 43 def build_by_csv_row(row) codes = build_codes(row[:code]) code_represented = build_code_represented(row[:code]) new( name: row[:name], title: row[:title], kind: row[:kind].split('/'), category: row[:category], code_represented: code_represented, codes: codes, ) end |
.build_code_represented(raw_code) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/jma_feed/report.rb', line 60 def build_code_represented(raw_code) if m = parse_raw_code(raw_code) _, main, cond = m.to_a main else raw_code end end |
.build_codes(raw_code) ⇒ Object
build_code(‘VGSK50’)
=> 'VGSK50'
build_code(‘VPTWii(ii=40-45)’)
=> ["VPTW40", "VPTW41", "VPTW42", "VPTW43", "VPTW44", "VPTW45"]
build_code(‘VXKO(ii=50-89)’)
=> ["VXKO50", "VXKO51", "VXKO52", "VXKO53", "VXKO54", "VXKO55", ...]
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/jma_feed/report.rb', line 75 def build_codes(raw_code) if m = parse_raw_code(raw_code) _, main, cond = m.to_a cursor, range = cond.split('=') range_first, range_last = range.split('-').map(&:to_i) (range_first..range_last).map do |i| main.match?(cursor) ? main.sub(cursor, i.to_s) : main + i.to_s end else [raw_code] end end |
.get ⇒ Object
39 40 41 |
# File 'lib/jma_feed/report.rb', line 39 def get @data ||= load end |
.load(**args) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/jma_feed/report.rb', line 31 def load(**args) load_csv(**args) do |csv| csv.drop(NUM_HEADER_ROWS).map do |row| build_by_csv_row(row) end end end |
.load_csv(version: "20240216") ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/jma_feed/report.rb', line 23 def load_csv(version: "20240216") path = File.join(File.dirname(__FILE__), "../../data/jmaxml_#{version}_format_v1_3_hyo1.csv") File.open(path) do |f| csv = CSV.new(f, headers: HEADERS, row_sep: CSV_ROW_SEP) yield(csv) end end |
.parse_raw_code(raw_code) ⇒ Object
56 57 58 |
# File 'lib/jma_feed/report.rb', line 56 def parse_raw_code(raw_code) raw_code.match(/(.+)\(([^\)]+)\)/) end |
Instance Method Details
#has_code?(c) ⇒ Boolean
91 92 93 |
# File 'lib/jma_feed/report.rb', line 91 def has_code?(c) codes.include?(c) end |
#has_kind?(k) ⇒ Boolean
95 96 97 |
# File 'lib/jma_feed/report.rb', line 95 def has_kind?(k) Array(kind).include?(k) end |