a.rb
ruby
1FILE_NAME = 'data.txt'
2START_ITEM = 1
3IN_ITEM = 2
4SEP_ITEM = 3
5END_ITEM = 0
6
7def parse_file(file_name)
8 lines = File.readlines(file_name)
9 parse_lines(lines)
10end
11
12def parse_lines(lines)
13 items = []
14 status = nil
15 item_block = []
16 lines.each do |line|
17 status = stat_machine(line.chomp, status)
18 if status == SEP_ITEM || status == END_ITEM
19 items << parse_block(item_block)
20 item_block = []
21 elsif status == IN_ITEM
22 item_block << line
23 end
24 end
25 items
26end
27
28def stat_machine(line, status)
29 if status.nil?
30 return START_ITEM if line == '[商品]'
31 elsif status == START_ITEM || status == IN_ITEM || status == SEP_ITEM
32 return SEP_ITEM if line == '----------'
33 return END_ITEM if line == '*********************************************************************'
34 return IN_ITEM
35 end
36 nil
37end
38
39def parse_block(item_block)
40 headers = item_block[0].match(/(.*)\((.*)\)/)
41 size = color = option = price = count = point = nil
42 item_block.shift
43 item_block.each do |line|
44 size = line.match(/サイズ:(.*)/)[1] if line.start_with?('サイズ:')
45 color = line.match(/カラー:(.*)/)[1] if line.start_with?('カラー:')
46 option = line.match(/※オプション:(.*)/)[1] if line.start_with?('※オプション:')
47 price, count = line.gsub(',', '').match(/価格 (\d+)\(円\) x (\d+)\(個\) = (\d+)\(円\)/).captures if line.start_with?('価格')
48 point = line.match(/獲得ポイント(\d*)/)[1] if line.start_with?('獲得ポイント')
49 end
50
51 {
52 name: headers[1],
53 code: headers[2],
54 size: size,
55 color: color,
56 price: price.to_i,
57 count: count.to_i,
58 option: option,
59 point: point.to_i
60 }
61end
62
63items = parse_file(FILE_NAME)
64items.each { |item| puts item }
data.txt
(省略)
---------------------------------------------------------------------
[受注番号] 000000-20170205-0000000
[日時] 2017-02-05 00:00:00
[注文者] 名前 太郎 (ナマエ タロウ) 様
〒123-0000 東京都
(TEL) 090-0000-4444
[支払方法] クレジット
[ポイント利用] 1000
[配送方法] 郵便
[配送日時指定]
[備考]
●備考
---------------------------------------------------------------------
[ショップ名] ***********
==========
[送付先] 名前 太郎 (ナマエ タロウ) 様
〒123-0000 東京都
(TEL) 090-0000-4444
[商品]
商品A(000-1234)
サイズ:XL
カラー:ブルー
※オプション:無し
価格 6,0000(円) x 1(個) = 6,0000(円) (税込、送料込)
獲得ポイント660
----------
商品A(001-1234)
サイズ:XL
カラー:レッド
※オプション:無し
価格 6,0000(円) x 1(個) = 6,0000(円) (税込、送料込)
獲得ポイント660
----------
商品A(002-1234)
サイズ:XL
カラー:グリーン
※オプション:無し
価格 6,0000(円) x 1(個) = 6,0000(円) (税込、送料込)
獲得ポイント660
*********************************************************************
小計 18,0000(円)
送料 0(円) 離島・一部地域では別途料金が必要な場合があります
---------------------------------------------------
合計 18,0000(円)
---------------------------------------------------------------------
今回のお買い物で獲得するポイント 660
---------------------------------------------------------------------
*消費税について
消費税率 8%
消費税計算順序 1商品毎に消費税計算
1円未満消費税端数 切り捨て
---------------------------------------------------------------------
(省略)
実行結果
$ ruby a.rb
{:name=>"商品A", :code=>"000-1234", :size=>"XL", :color=>"ブルー", :price=>60000, :count=>1, :option=>"無し", :point=>660}
{:name=>"商品A", :code=>"001-1234", :size=>"XL", :color=>"レッド", :price=>60000, :count=>1, :option=>"無し", :point=>660}
{:name=>"商品A", :code=>"002-1234", :size=>"XL", :color=>"グリーン", :price=>60000, :count=>1, :option=>"無し", :point=>660}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。