From 07f24cb8f4b142f9e262e8c09cb8b61f21270f32 Mon Sep 17 00:00:00 2001 From: Osamu Takiya Date: Tue, 20 Jun 2023 02:12:20 +0900 Subject: [PATCH] =?UTF-8?q?CSV=E3=83=80=E3=82=A6=E3=83=B3=E3=83=AD?= =?UTF-8?q?=E3=83=BC=E3=83=89=E6=A9=9F=E8=83=BD=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/download_csv_service.rb | 23 +++++++++++++ .../from_spreadsheet.rb | 2 +- app/service/file_service.rb | 10 ++++-- app/service/import_service/base.rb | 4 +-- app/service/spreadsheet_service/sheet_id.rb | 8 ++--- spec/service/download_csv_service_spec.rb | 34 +++++++++++++++++++ spec/service/import_service/star_spec.rb | 7 ---- 7 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 app/service/download_csv_service.rb create mode 100644 spec/service/download_csv_service_spec.rb diff --git a/app/service/download_csv_service.rb b/app/service/download_csv_service.rb new file mode 100644 index 0000000..eeca6db --- /dev/null +++ b/app/service/download_csv_service.rb @@ -0,0 +1,23 @@ +require 'csv' + +class DownloadCsvService + # スプレッドシートからのダウンロードを大前提とする + def initialize(spreadsheet_id:, worksheet_name:) + @fetcher = FetchDataTableService::FromSpreadsheet.new( + spreadsheet_id:, + worksheet_name: + ) + end + + def execute(filepath) + csv_contents = CSV.generate do |csv| + csv << @fetcher.headers + @fetcher.rows.each do |row| + csv << row + end + end + + file_service = FileService.new(filepath) + file_service.write(csv_contents) + end +end diff --git a/app/service/fetch_data_table_service/from_spreadsheet.rb b/app/service/fetch_data_table_service/from_spreadsheet.rb index b59a302..448d4f3 100644 --- a/app/service/fetch_data_table_service/from_spreadsheet.rb +++ b/app/service/fetch_data_table_service/from_spreadsheet.rb @@ -1,6 +1,6 @@ module FetchDataTableService class FromSpreadsheet < Base - def initialize(spreadsheet_id, worksheet_name) + def initialize(spreadsheet_id:, worksheet_name:) super sheets_api = SpreadsheetService::SheetsApi.create diff --git a/app/service/file_service.rb b/app/service/file_service.rb index ec70260..7ea7d40 100644 --- a/app/service/file_service.rb +++ b/app/service/file_service.rb @@ -1,5 +1,11 @@ class FileService - def initialize - # TODO: 書く + def initialize(filepath) + @filepath = filepath + end + + def write(text) + File.open(@filepath, 'w') do |file| + file.puts text + end end end diff --git a/app/service/import_service/base.rb b/app/service/import_service/base.rb index 5eeef5d..973627f 100644 --- a/app/service/import_service/base.rb +++ b/app/service/import_service/base.rb @@ -36,9 +36,9 @@ def data_table_from_csv(csv_filepath:) end def data_table_from_spreadsheet(spreadsheet_title:, worksheet_name:) - spreadsheet_id = SpreadsheetService::SheetId.retrieve(spreadsheet_title) + spreadsheet_id = SpreadsheetService::SheetId.title_to_id(spreadsheet_title) - FetchDataTableService::FromSpreadsheet.new(spreadsheet_id, worksheet_name) + FetchDataTableService::FromSpreadsheet.new(spreadsheet_id:, worksheet_name:) end end end diff --git a/app/service/spreadsheet_service/sheet_id.rb b/app/service/spreadsheet_service/sheet_id.rb index f498be2..f300f2d 100644 --- a/app/service/spreadsheet_service/sheet_id.rb +++ b/app/service/spreadsheet_service/sheet_id.rb @@ -1,16 +1,12 @@ module SpreadsheetService class SheetId class << self - def retrieve(spreadsheet_title) - title_to_id[spreadsheet_title] - end - # 新規追加時はシートへの権限の付与も忘れないこと - def title_to_id + def title_to_id(title) { 'basic_attributes' => ENV.fetch('SPREADSHEET_ID_BASIC_ATTRIBUTES'), 'products' => ENV.fetch('SPREADSHEET_ID_PRODUCTS') - } + }.fetch(title) end end end diff --git a/spec/service/download_csv_service_spec.rb b/spec/service/download_csv_service_spec.rb new file mode 100644 index 0000000..19e772b --- /dev/null +++ b/spec/service/download_csv_service_spec.rb @@ -0,0 +1,34 @@ +require 'rails_helper' + +describe DownloadCsvService do + let(:service) do + DownloadCsvService.new( + spreadsheet_id: SpreadsheetService::SheetId.title_to_id('basic_attributes'), + worksheet_name: 'stars' + ) + end + + describe 'CSVファイル' do + it '期待どおりにダウンロード(=生成)されていること' do + service.execute('tmp/vault_test.csv') + + expect(File.exist?('tmp/vault_test.csv')).to be_truthy + end + + it '一行目(ヘッダ)の内容が期待どおりであること' do + service.execute('tmp/vault_test_check_first_row.csv') + + expect( + File.open('tmp/vault_test_check_first_row.csv').readline.chomp + ).to eq 'id,seating_order,name,name_en' + end + + it '二行目(内容の行の一行目)の内容が期待どおりであること' do + service.execute('tmp/vault_test_check_second_row.csv') + + expect( + File.open('tmp/vault_test_check_first_row.csv').readlines[1].chomp + ).to eq '1,1,天魁星,Leader Star' + end + end +end diff --git a/spec/service/import_service/star_spec.rb b/spec/service/import_service/star_spec.rb index 15639cf..45be850 100644 --- a/spec/service/import_service/star_spec.rb +++ b/spec/service/import_service/star_spec.rb @@ -1,13 +1,6 @@ require 'rails_helper' describe ImportService::Star do - let(:service) do - ImportService::Star.new( - spreadsheet_title: 'basic_attributes', - worksheet_name: 'stars' - ) - end - it 'ソースをもとにしたサービスのオブジェクトが生成されること' do expect( ImportService::Star.new(