Skip to content

Commit

Permalink
タイムゾーンに協定世界時が設定されている場合の対応
Browse files Browse the repository at this point in the history
タイムゾーンに協定世界時が設定されていたら、アプリデフォルトのタイムゾーンを
設定して登録するように修正。
  • Loading branch information
siso25 committed Oct 2, 2023
1 parent 3481e0c commit 40be2d9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
15 changes: 9 additions & 6 deletions app/models/external_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,41 @@ def parse_rss_feed(feed_url)
def save_rdf_feed(user, rdf_item, feed_updated)
return if ExternalEntry.find_by(url: rdf_item.link)

published_at = rdf_publish_date(rdf_item, feed_updated)
ExternalEntry.create(
title: rdf_item.title,
url: rdf_item.link,
summary: rdf_item.description,
thumbnail_image_url: nil,
published_at: rdf_publish_date(rdf_item, feed_updated),
published_at: published_at.utc? ? Time.zone.parse(published_at.to_s) : published_at,
user:
)
end

def save_rss_feed(user, rss_item, feed_updated)
return if ExternalEntry.find_by(url: rss_item.link)

published_at = rss_publish_date(rss_item, feed_updated)
ExternalEntry.create(
title: rss_item.title,
url: rss_item.link,
summary: rss_item.description,
thumbnail_image_url: rss_item.enclosure&.url,
published_at: rss_publish_date(rss_item, feed_updated),
published_at: published_at.utc? ? Time.zone.parse(published_at.to_s) : published_at,
user:
)
end

def save_atom_feed(user, atom_item, feed_updated)
return if ExternalEntry.find_by(url: atom_item.link&.href)

published_at = atom_publish_date(atom_item, feed_updated)
ExternalEntry.create(
title: atom_item.title&.content,
url: atom_item.link&.href,
summary: atom_item.content&.content,
thumbnail_image_url: atom_image_url(atom_item),
published_at: atom_publish_date(atom_item, feed_updated),
published_at: published_at.utc? ? Time.zone.parse(published_at.to_s) : published_at,
user:
)
end
Expand All @@ -90,14 +93,14 @@ def rdf_publish_date(rdf_item, feed_updated)
return rdf_item.dc_date if rdf_item.dc_date
return feed_updated if feed_updated

Time.zone.today
Time.zone.now
end

def rss_publish_date(rss_item, feed_updated)
return rss_item.pubDate if rss_item.pubDate
return feed_updated if feed_updated

Time.zone.today
Time.zone.now
end

def atom_image_url(atom_item)
Expand All @@ -109,7 +112,7 @@ def atom_publish_date(atom_item, feed_updated)
return atom_item.updated.content if atom_item.updated
return feed_updated if feed_updated

Time.zone.today
Time.zone.now
end
end
end
28 changes: 14 additions & 14 deletions test/models/external_entry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ class ExternalEntryTest < ActiveSupport::TestCase
rdf_item.expect(:description, nil)
rdf_item.expect(:dc_date, nil)

assert ExternalEntry.save_rdf_feed(user, rdf_item, nil)
assert ExternalEntry.save_rdf_feed(user, rdf_item, Time.zone.local(2023, 1, 1, 0, 0, 0).utc)
end

test '.save_rss_feed' do
user = users(:hatsuno)
rss_item = Minitest::Mock.new
rss_item.expect(:link, 'https://test.com/rss')
rss_item.expect(:pubDate, Time.zone.local(2022, 1, 1, 0, 0, 0))
rss_item.expect(:pubDate, Time.zone.local(2022, 1, 1, 0, 0, 0))
rss_item.expect(:title, 'test title')
rss_item.expect(:link, 'https://test.com/rss')
rss_item.expect(:description, 'article description')
rss_item.expect(:enclosure, rss_item)
rss_item.expect(:url, 'https://test.com/image.png')
rss_item.expect(:pubDate, Time.zone.local(2022, 1, 1, 0, 0, 0))
rss_item.expect(:pubDate, Time.zone.local(2022, 1, 1, 0, 0, 0))

assert ExternalEntry.save_rss_feed(user, rss_item, nil)
end
Expand All @@ -80,24 +80,24 @@ class ExternalEntryTest < ActiveSupport::TestCase
user = users(:hatsuno)
rss_item = Minitest::Mock.new
rss_item.expect(:link, 'https://test.com/rss')
rss_item.expect(:pubDate, nil)
rss_item.expect(:title, 'test title')
rss_item.expect(:link, 'https://test.com/rss')
rss_item.expect(:description, 'article description')
rss_item.expect(:enclosure, nil)
rss_item.expect(:pubDate, nil)

assert ExternalEntry.save_rss_feed(user, rss_item, Time.zone.local(2023, 1, 1, 0, 0, 0))
assert ExternalEntry.save_rss_feed(user, rss_item, Time.zone.local(2023, 1, 1, 0, 0, 0).utc)
end

test 'no exception even if all rss elements are nil' do
user = users(:hatsuno)
rss_item = Minitest::Mock.new
rss_item.expect(:link, nil)
rss_item.expect(:pubDate, nil)
rss_item.expect(:title, nil)
rss_item.expect(:link, nil)
rss_item.expect(:description, nil)
rss_item.expect(:enclosure, nil)
rss_item.expect(:pubDate, nil)

assert ExternalEntry.save_rss_feed(user, rss_item, nil)
end
Expand All @@ -107,6 +107,9 @@ class ExternalEntryTest < ActiveSupport::TestCase
atom_item = Minitest::Mock.new
atom_item.expect(:link, atom_item)
atom_item.expect(:href, 'https://test.com/feed')
atom_item.expect(:published, atom_item)
atom_item.expect(:published, atom_item)
atom_item.expect(:content, Time.zone.local(2022, 1, 1, 0, 0, 0))
atom_item.expect(:title, atom_item)
atom_item.expect(:content, 'test title')
atom_item.expect(:link, atom_item)
Expand All @@ -117,9 +120,6 @@ class ExternalEntryTest < ActiveSupport::TestCase
atom_item.expect(:find, atom_item)
atom_item.expect(:nil?, atom_item)
atom_item.expect(:href, 'https://test.com/image.png')
atom_item.expect(:published, atom_item)
atom_item.expect(:published, atom_item)
atom_item.expect(:content, Time.zone.local(2022, 1, 1, 0, 0, 0))

assert ExternalEntry.save_atom_feed(user, atom_item, nil)
end
Expand All @@ -129,6 +129,8 @@ class ExternalEntryTest < ActiveSupport::TestCase
atom_item = Minitest::Mock.new
atom_item.expect(:link, atom_item)
atom_item.expect(:href, 'https://test.com/feed')
atom_item.expect(:published, nil)
atom_item.expect(:updated, nil)
atom_item.expect(:title, atom_item)
atom_item.expect(:content, 'test title')
atom_item.expect(:link, atom_item)
Expand All @@ -139,23 +141,21 @@ class ExternalEntryTest < ActiveSupport::TestCase
atom_item.expect(:find, atom_item)
atom_item.expect(:nil?, atom_item)
atom_item.expect(:href, 'https://test.com/image.png')
atom_item.expect(:published, nil)
atom_item.expect(:updated, nil)

assert ExternalEntry.save_atom_feed(user, atom_item, Time.zone.local(2023, 1, 1, 0, 0, 0))
assert ExternalEntry.save_atom_feed(user, atom_item, Time.zone.local(2023, 1, 1, 0, 0, 0).utc)
end

test 'no exception even if all atom elements are nil' do
user = users(:hatsuno)
atom_item = Minitest::Mock.new
atom_item.expect(:link, nil)
atom_item.expect(:published, nil)
atom_item.expect(:updated, nil)
atom_item.expect(:title, nil)
atom_item.expect(:content, nil)
atom_item.expect(:link, nil)
atom_item.expect(:content, nil)
atom_item.expect(:links, nil)
atom_item.expect(:published, nil)
atom_item.expect(:updated, nil)

assert ExternalEntry.save_atom_feed(user, atom_item, nil)
end
Expand Down

0 comments on commit 40be2d9

Please sign in to comment.