Skip to content

Commit 41fc393

Browse files
committed
refactor: YAMLファイル構造とコード処理を最適化
1 parent 752d25d commit 41fc393

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

lib/tasks/news.rake

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ namespace :news do
1010

1111
logger.info('==== START news:fetch ====')
1212

13-
# 既存の news.yml を読み込み
14-
news_yaml_path = Rails.root.join('db', 'news.yml')
15-
existing_news = YAML.safe_load File.read(news_yaml_path)
16-
17-
# テスト/ステージング環境ではサンプルファイル、本番は実サイトのフィード
13+
# 本番環境では実サイトのフィード、それ以外(テスト環境など)ではテスト用フェード
1814
DOJO_NEWS_FEED = 'https://news.coderdojo.jp/feed/'
1915
TEST_NEWS_FEED = Rails.root.join('spec', 'fixtures', 'sample_news.rss')
2016
RSS_FEED_LIST = Rails.env.production? ?
@@ -32,16 +28,17 @@ namespace :news do
3228
}
3329
end
3430

35-
# 既存データをハッシュに変換(URL をキーに)
36-
existing_items_hash = existing_news.index_by { |item| item['url'] }
31+
# 既存データ (YAML) を読み込み、ハッシュに変換
32+
news_yaml_file = File.read Rails.root.join('db', 'news.yml')
33+
existing_news = YAML.safe_load(news_yaml_file).index_by { |item| item['url'] }
3734

3835
# 新しいアイテムと既存アイテムを分離
3936
truly_new_items = []
4037
updated_items = []
4138

4239
news_items.each do |new_item|
43-
if existing_items_hash.key?(new_item['url'])
44-
existing_item = existing_items_hash[new_item['url']]
40+
if existing_news.key?(new_item['url'])
41+
existing_item = existing_news[new_item['url']]
4542
# タイトルまたは公開日が変わった場合のみ更新
4643
if existing_item['title'] != new_item['title'] || existing_item['published_at'] != new_item['published_at']
4744
updated_items << existing_item.merge(new_item)

0 commit comments

Comments
 (0)