-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Star model and its migration & test * Add spreadsheet configuration file * Add SpreadsheetService API with managers * Add spreadsheet credentials file path * Add data import service for Star model * Add star table with name, name_en, seating_order * Refactor ImportService::Base class and IndexesThe commit refactors the ImportService::Base class and removes name_en index from the schema * Update database.yml for development and test environments * Add default value for SPREADSHEET_CREDENTIALS_FILEPATH
- Loading branch information
1 parent
77b55fb
commit bc154d8
Showing
20 changed files
with
271 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class Star < ApplicationRecord | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module FetchDataTableService | ||
class Base | ||
def initialize(*args) | ||
# TODO: 書く | ||
end | ||
|
||
def headers | ||
raise NotImplementedError | ||
end | ||
|
||
def rows | ||
raise NotImplementedError | ||
end | ||
|
||
def header_to_rows | ||
raise NotImplementedError | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
module FetchDataTableService | ||
class FromSpreadsheet < Base | ||
def initialize(spreadsheet_id, worksheet_name) | ||
super | ||
|
||
sheets_api = SpreadsheetService::SheetsApi.create | ||
spreadsheet_manager = spreadsheet_manager(sheets_api, spreadsheet_id) | ||
spreadsheet = spreadsheet_manager.spreadsheet | ||
|
||
@worksheet_manager = worksheet_manager(sheets_api, spreadsheet, worksheet_name) | ||
end | ||
|
||
def headers | ||
@worksheet_manager.headers | ||
end | ||
|
||
def rows | ||
@worksheet_manager.rows | ||
end | ||
|
||
def header_to_rows | ||
{}.tap do |hash| | ||
headers.each do |header| | ||
rows.each do |row| | ||
hash[header.to_sym] ||= [] | ||
hash[header.to_sym] << row[headers.index(header)] | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def spreadsheet_manager(sheets_api, spreadsheet_id) | ||
SpreadsheetService::SpreadsheetManager.new( | ||
sheets_api, | ||
spreadsheet_id | ||
) | ||
end | ||
|
||
def worksheet_manager(sheets_api, spreadsheet, worksheet_name) | ||
SpreadsheetService::WorksheetManager.new( | ||
sheets_api, | ||
spreadsheet, | ||
worksheet_name | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module ImportService | ||
class Base | ||
class << self | ||
def execute | ||
raise NotImplementedError | ||
end | ||
|
||
def columns | ||
raise NotImplementedError | ||
end | ||
|
||
def values | ||
raise NotImplementedError | ||
end | ||
|
||
def data_table | ||
raise NotImplementedError | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module ImportService | ||
class Star < Base | ||
class << self | ||
def execute | ||
ActiveRecord::Base.transaction do | ||
::Star.import(columns, values, validate: true) | ||
end | ||
end | ||
|
||
def columns | ||
data_table.headers.map(&:to_sym) | ||
end | ||
|
||
def values | ||
data_table.rows | ||
end | ||
|
||
def data_table | ||
spreadsheet_title = 'basic_attributes' | ||
|
||
spreadsheet_id = SpreadsheetService::SheetId.retrieve(spreadsheet_title) | ||
worksheet_name = 'stars' | ||
|
||
FetchDataTableService::FromSpreadsheet.new(spreadsheet_id, worksheet_name) | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module SpreadsheetService | ||
class SheetId | ||
class << self | ||
def retrieve(spreadsheet_title) | ||
title_to_id[spreadsheet_title] | ||
end | ||
|
||
def title_to_id | ||
{ | ||
'basic_attributes' => ENV.fetch('SPREADSHEET_ID_BASIC_ATTRIBUTES'), | ||
'products' => ENV.fetch('SPREADSHEET_ID_PRODUCTS') | ||
} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'google/apis/sheets_v4' | ||
|
||
module SpreadsheetService | ||
class SheetsApi | ||
CREDS_JSON_FILEPATH = ENV.fetch('SPREADSHEET_CREDENTIALS_FILEPATH', nil) | ||
|
||
class << self | ||
def create | ||
authorizer = Google::Auth::ServiceAccountCredentials.make_creds( | ||
json_key_io: File.open(CREDS_JSON_FILEPATH), | ||
|
||
# drive と drive.file も必要になることには注意する | ||
scope: %w[ | ||
https://www.googleapis.com/auth/drive | ||
https://www.googleapis.com/auth/drive.file | ||
https://www.googleapis.com/auth/spreadsheets | ||
] | ||
) | ||
authorizer.fetch_access_token! | ||
|
||
api = Google::Apis::SheetsV4::SheetsService.new | ||
api.authorization = authorizer | ||
|
||
api | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module SpreadsheetService | ||
class SpreadsheetManager | ||
attr_reader :spreadsheet, :worksheet_names | ||
|
||
def initialize(sheets_api, spreadsheet_id) | ||
@spreadsheet = sheets_api.get_spreadsheet(spreadsheet_id) | ||
@worksheet_names = @spreadsheet.sheets.map(&:properties).map(&:title) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module SpreadsheetService | ||
class WorksheetManager | ||
attr_reader :worksheet, :headers, :rows | ||
|
||
def initialize(sheets_api, spreadsheet, worksheet_name, range: 'A1:Z') | ||
@sheets_api = sheets_api | ||
@spreadsheet = spreadsheet | ||
|
||
@worksheet = @spreadsheet.sheets.find do |sheet| | ||
sheet.properties.title == worksheet_name | ||
end | ||
|
||
@all_cells = all_cells(@worksheet, range:) | ||
@headers = @all_cells.first | ||
@rows = @all_cells.drop(1) | ||
|
||
raise 'Some cells are invalid.' unless valid_all_cells? | ||
end | ||
|
||
def all_cells(worksheet, range: 'A1:Z') | ||
# 戻り値からは nil が取り除かれる | ||
@sheets_api.get_spreadsheet_values( | ||
@spreadsheet.spreadsheet_id, | ||
"#{worksheet.properties.title}!#{range}" | ||
).values | ||
end | ||
|
||
def valid_all_cells? | ||
return false if @headers.blank? || @rows.blank? | ||
|
||
@rows.each { |row| return false if row.size != @headers.size } | ||
|
||
true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class CreateStars < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :stars do |t| | ||
t.string :name, null: false, comment: '宿星の名前(日本語)' | ||
t.string :name_en, null: false, comment: '宿星の名前(英語)' | ||
t.integer :seating_order, null: false, comment: '席次' | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :stars, :name, unique: true | ||
add_index :stars, :seating_order, unique: true | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FactoryBot.define do | ||
factory :star do | ||
|
||
end | ||
end |
Oops, something went wrong.