Skip to content

Commit

Permalink
refactor: JSONデータを返却するだけのシンプルなシリアライザに変更する
Browse files Browse the repository at this point in the history
  • Loading branch information
kuroweb committed Nov 7, 2024
1 parent f2c9cd7 commit dd9b2f1
Show file tree
Hide file tree
Showing 21 changed files with 309 additions and 168 deletions.
4 changes: 2 additions & 2 deletions volumes/backend/app/controllers/api/v1/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module V1
class ProductsController < Api::ApplicationController
def index
products = ProductFinder.new(params: find_product_params).execute
render json: ProductsSerializer.new(products), status: :ok
render json: Api::Products::ProductsSerializer.new(products).render_json, status: :ok
end

def show
render json: ProductSerializer.new(product), status: :ok
render json: Api::Products::ProductSerializer.new(product).render_json, status: :ok
end

def create
Expand Down
20 changes: 20 additions & 0 deletions volumes/backend/app/serializers/api/products/base_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Api
module Products
class BaseSerializer
def render_json
normalize_json
end

private

# @override
def json
raise NotImplementedError, "#{self.class.name}##{__method__} is not defined."
end

def normalize_json
json.is_a?(Hash) ? json.with_indifferent_access : json
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Api
module Products
class CategorySerializer < BaseSerializer
def initialize(category)
@category = category
end

private

def json
@category.as_json
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Api
module Products
class IosysCrawlSettingSerializer < BaseSerializer
def initialize(iosys_crawl_setting)
@iosys_crawl_setting = iosys_crawl_setting
end

private

def json
iosys_crawl_setting_json.merge(
{
iosys_crawl_setting_exclude_keywords: iosys_crawl_setting_exclude_keywords_json,
iosys_crawl_setting_required_keywords: iosys_crawl_setting_required_keywords_json
}
)
end

def iosys_crawl_setting_json
@iosys_crawl_setting.as_json
end

def iosys_crawl_setting_exclude_keywords_json
@iosys_crawl_setting.iosys_crawl_setting_exclude_keywords.map(&:as_json)
end

def iosys_crawl_setting_required_keywords_json
@iosys_crawl_setting.iosys_crawl_setting_required_keywords.map(&:as_json)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Api
module Products
class JanparaCrawlSettingSerializer < BaseSerializer
def initialize(janpara_crawl_setting)
@janpara_crawl_setting = janpara_crawl_setting
end

private

def json
janpara_crawl_setting_json.merge(
{
janpara_crawl_setting_exclude_keywords: janpara_crawl_setting_exclude_keywords_json,
janpara_crawl_setting_required_keywords: janpara_crawl_setting_required_keywords_json
}
)
end

def janpara_crawl_setting_json
@janpara_crawl_setting.as_json
end

def janpara_crawl_setting_exclude_keywords_json
@janpara_crawl_setting.janpara_crawl_setting_exclude_keywords.map(&:as_json)
end

def janpara_crawl_setting_required_keywords_json
@janpara_crawl_setting.janpara_crawl_setting_required_keywords.map(&:as_json)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Api
module Products
class MercariCrawlSettingSerializer < BaseSerializer
def initialize(mercari_crawl_setting)
@mercari_crawl_setting = mercari_crawl_setting
end

private

def json
mercari_crawl_setting_json.merge(
{
mercari_crawl_setting_exclude_keywords: mercari_crawl_setting_exclude_keywords_json,
mercari_crawl_setting_required_keywords: mercari_crawl_setting_required_keywords_json
}
)
end

def mercari_crawl_setting_json
@mercari_crawl_setting.as_json
end

def mercari_crawl_setting_exclude_keywords_json
@mercari_crawl_setting.mercari_crawl_setting_exclude_keywords.map(&:as_json)
end

def mercari_crawl_setting_required_keywords_json
@mercari_crawl_setting.mercari_crawl_setting_required_keywords.map(&:as_json)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Api
module Products
class PcKoubouCrawlSettingSerializer < BaseSerializer
def initialize(pc_koubou_crawl_setting)
@pc_koubou_crawl_setting = pc_koubou_crawl_setting
end

private

def json
pc_koubou_crawl_setting_json.merge(
{
pc_koubou_crawl_setting_exclude_keywords: pc_koubou_crawl_setting_exclude_keywords_json,
pc_koubou_crawl_setting_required_keywords: pc_koubou_crawl_setting_required_keywords_json
}
)
end

def pc_koubou_crawl_setting_json
@pc_koubou_crawl_setting.as_json
end

def pc_koubou_crawl_setting_exclude_keywords_json
@pc_koubou_crawl_setting.pc_koubou_crawl_setting_exclude_keywords.map(&:as_json)
end

def pc_koubou_crawl_setting_required_keywords_json
@pc_koubou_crawl_setting.pc_koubou_crawl_setting_required_keywords.map(&:as_json)
end
end
end
end
57 changes: 57 additions & 0 deletions volumes/backend/app/serializers/api/products/product_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module Api
module Products
class ProductSerializer < BaseSerializer
def initialize(product)
@product = product
end

private

def json
product_json.merge(
{
yahoo_auction_crawl_setting: yahoo_auction_crawl_setting_json,
mercari_crawl_setting: mercari_crawl_setting_json,
janpara_crawl_setting: janpara_crawl_setting_json,
iosys_crawl_setting: iosys_crawl_setting_json,
pc_koubou_crawl_setting: pc_koubou_crawl_setting_json,
used_sofmap_crawl_setting: used_sofmap_crawl_setting_json,
category: category_json
}
)
end

def product_json
@product.as_json
end

def yahoo_auction_crawl_setting_json
YahooAuctionCrawlSettingSerializer.new(@product.yahoo_auction_crawl_setting).render_json
end

def mercari_crawl_setting_json
MercariCrawlSettingSerializer.new(@product.mercari_crawl_setting).render_json
end

def janpara_crawl_setting_json
JanparaCrawlSettingSerializer.new(@product.janpara_crawl_setting).render_json
end

def iosys_crawl_setting_json
IosysCrawlSettingSerializer.new(@product.iosys_crawl_setting).render_json
end

def pc_koubou_crawl_setting_json
PcKoubouCrawlSettingSerializer.new(@product.pc_koubou_crawl_setting).render_json
end

def used_sofmap_crawl_setting_json
UsedSofmapCrawlSettingSerializer.new(@product.used_sofmap_crawl_setting).render_json
end

def category_json
CategorySerializer.new(@product.category).render_json
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Api
module Products
class ProductsSerializer < BaseSerializer
def initialize(products)
@products = products
end

private

def json
{
products: products_json
}
end

def products_json
@products.map do |product|
ProductSerializer.new(product).render_json
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Api
module Products
class UsedSofmapCrawlSettingSerializer < BaseSerializer
def initialize(used_sofmap_crawl_setting)
@used_sofmap_crawl_setting = used_sofmap_crawl_setting
end

private

def json
used_sofmap_crawl_setting_json.merge(
{
used_sofmap_crawl_setting_exclude_keywords: used_sofmap_crawl_setting_exclude_keywords_json,
used_sofmap_crawl_setting_required_keywords: used_sofmap_crawl_setting_required_keywords_json
}
)
end

def used_sofmap_crawl_setting_json
@used_sofmap_crawl_setting.as_json
end

def used_sofmap_crawl_setting_exclude_keywords_json
@used_sofmap_crawl_setting.used_sofmap_crawl_setting_exclude_keywords.map(&:as_json)
end

def used_sofmap_crawl_setting_required_keywords_json
@used_sofmap_crawl_setting.used_sofmap_crawl_setting_required_keywords.map(&:as_json)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Api
module Products
class YahooAuctionCrawlSettingSerializer < BaseSerializer
def initialize(yahoo_auction_crawl_setting)
@yahoo_auction_crawl_setting = yahoo_auction_crawl_setting
end

private

def json
yahoo_auction_crawl_setting_json.merge(
{
yahoo_auction_crawl_setting_exclude_keywords: yahoo_auction_crawl_setting_exclude_keywords_json,
yahoo_auction_crawl_setting_required_keywords: yahoo_auction_crawl_setting_required_keywords_json
}
)
end

def yahoo_auction_crawl_setting_json
@yahoo_auction_crawl_setting.as_json
end

def yahoo_auction_crawl_setting_exclude_keywords_json
@yahoo_auction_crawl_setting.yahoo_auction_crawl_setting_exclude_keywords.map(&:as_json)
end

def yahoo_auction_crawl_setting_required_keywords_json
@yahoo_auction_crawl_setting.yahoo_auction_crawl_setting_required_keywords.map(&:as_json)
end
end
end
end
14 changes: 0 additions & 14 deletions volumes/backend/app/serializers/base_serializer.rb

This file was deleted.

15 changes: 0 additions & 15 deletions volumes/backend/app/serializers/category_serializer.rb

This file was deleted.

15 changes: 0 additions & 15 deletions volumes/backend/app/serializers/iosys_crawl_setting_serializer.rb

This file was deleted.

This file was deleted.

Loading

0 comments on commit dd9b2f1

Please sign in to comment.