Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSVダウンロード機能を実装 #45

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/service/download_csv_service.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/service/fetch_data_table_service/from_spreadsheet.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 8 additions & 2 deletions app/service/file_service.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions app/service/import_service/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 2 additions & 6 deletions app/service/spreadsheet_service/sheet_id.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
34 changes: 34 additions & 0 deletions spec/service/download_csv_service_spec.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 0 additions & 7 deletions spec/service/import_service/star_spec.rb
Original file line number Diff line number Diff line change
@@ -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(
Expand Down