Skip to content

Commit

Permalink
Add mapping for sy:updatePeriod and sy:updateFrequency (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
w3stling authored Sep 21, 2024
1 parent 89d3737 commit 696238e
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ protected void registerChannelTags() {
channelTags.putIfAbsent("dc:language", (channel, value) -> Mapper.mapIfEmpty(value, channel::getLanguage, channel::setLanguage));
channelTags.putIfAbsent("dc:rights", (channel, value) -> Mapper.mapIfEmpty(value, channel::getCopyright, channel::setCopyright));
channelTags.putIfAbsent("dc:title", (channel, value) -> Mapper.mapIfEmpty(value, channel::getTitle, channel::setTitle));
channelTags.putIfAbsent("dc:date", (channel, value) -> Mapper.mapIfEmpty(value, channel::getPubDate, channel::setPubDate));
channelTags.putIfAbsent("sy:updatePeriod", (channel, value) -> channel.syUpdatePeriod = value);
channelTags.putIfAbsent("sy:updateFrequency", (channel, value) -> mapInteger(value, number -> channel.syUpdateFrequency = number));
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/apptasticsoftware/rssreader/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package com.apptasticsoftware.rssreader;

import com.apptasticsoftware.rssreader.util.Util;

import java.time.ZonedDateTime;
import java.util.*;

Expand All @@ -46,6 +48,8 @@ public class Channel {
private String docs;
private String rating;
private Image image;
protected String syUpdatePeriod;
protected int syUpdateFrequency = 1;
private final DateTimeParser dateTimeParser;

/**
Expand Down Expand Up @@ -216,7 +220,11 @@ public void setGenerator(String generator) {
* @return time to live
*/
public Optional<String> getTtl() {
return Optional.ofNullable(ttl);
return Optional.ofNullable(ttl)
.or(() -> Optional.ofNullable(syUpdatePeriod)
.map(Util::toMinutes)
.map(minutes -> minutes / Math.max(syUpdateFrequency, 1))
.map(String::valueOf));
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/apptasticsoftware/rssreader/util/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.apptasticsoftware.rssreader.util;

import java.util.Locale;

/**
* Utility class for RSS reader.
*/
public class Util {

private Util() {

}

/**
* Convert a time period string to hours.
*
* @param period the time period string (e.g., "daily", "weekly", "monthly", "yearly", "hourly")
* @return the number of hours in the given time period, or 1 if the period is not recognized
*/
public static int toMinutes(String period) {
switch (period.toLowerCase(Locale.ENGLISH)) {
case "daily": return 1440;
case "weekly": return 10080;
case "monthly": return 43800;
case "yearly": return 525600;
case "hourly":
default: return 60;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,15 @@ void readFromAMixOfUrlAndString() throws IOException, URISyntaxException {
assertEquals(33, items.size());
}

@Test
void syUpdatePeriodAndSyUpdateFrequency() throws IOException, URISyntaxException {
var source = getFileUri("rss-feed.xml");

var items = new RssReader().read(source).collect(Collectors.toList());
assertEquals(20, items.size());
assertEquals("30", items.get(0).getChannel().getTtl().orElse(""));
}

private InputStream fromFile(String fileName) {
return getClass().getClassLoader().getResourceAsStream(fileName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ void enclosureEqualsTest() {

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(Channel.class).withIgnoredFields("dateTimeParser").withIgnoredFields("category").withNonnullFields("categories").verify();
EqualsVerifier.simple().forClass(Channel.class).withIgnoredFields("dateTimeParser").withIgnoredFields("category").withIgnoredFields("syUpdatePeriod").withIgnoredFields("syUpdateFrequency").withNonnullFields("categories").verify();
EqualsVerifier.simple().forClass(Item.class).withIgnoredFields("defaultComparator").withIgnoredFields("dateTimeParser").withIgnoredFields("category").withNonnullFields("categories").withIgnoredFields("enclosure").withNonnullFields("enclosures").verify();
EqualsVerifier.simple().forClass(Enclosure.class).verify();
EqualsVerifier.simple().forClass(Image.class).verify();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void readItunesPodcastFeed2() throws IOException {

@Test
void equalsContract() {
EqualsVerifier.simple().forClass(ItunesChannel.class).withIgnoredFields("dateTimeParser").withIgnoredFields("category").withNonnullFields("categories").withNonnullFields("itunesCategories").verify();
EqualsVerifier.simple().forClass(ItunesChannel.class).withIgnoredFields("dateTimeParser").withIgnoredFields("category").withNonnullFields("categories").withIgnoredFields("syUpdatePeriod").withIgnoredFields("syUpdateFrequency").withNonnullFields("itunesCategories").verify();
EqualsVerifier.simple().forClass(ItunesItem.class).withIgnoredFields("defaultComparator").withIgnoredFields("dateTimeParser").withIgnoredFields("category").withNonnullFields("categories").withIgnoredFields("enclosure").withNonnullFields("enclosures").verify();
EqualsVerifier.simple().forClass(ItunesOwner.class).verify();
}
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/com/apptasticsoftware/rssreader/util/UtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.apptasticsoftware.rssreader.util;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

class UtilTest {

@ParameterizedTest(name = "{0} is expected to output {1}")
@MethodSource("periodToHoursTestData")
void periodToHours(String period, int expectedHours) {
assertEquals(expectedHours, Util.toMinutes(period));
}

private static Stream<Arguments> periodToHoursTestData() {
return Stream.of(
Arguments.of("daily", 1440),
Arguments.of("weekly", 10080),
Arguments.of("monthly", 43800),
Arguments.of("yearly", 525600),
Arguments.of("hourly", 60),
Arguments.of("unknown", 60)
);
}
}
2 changes: 1 addition & 1 deletion src/test/resources/rss-feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<lastBuildDate>Fri, 23 Aug 2024 01:11:17 +0000</lastBuildDate>
<language>en-US</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<sy:updateFrequency>2</sy:updateFrequency>
<generator>https://wordpress.org/?v=6.5.5</generator>
<image>
<url>https://techcrunch.com/wp-content/uploads/2015/02/cropped-cropped-favicon-gradient.png?w=32</url>
Expand Down

0 comments on commit 696238e

Please sign in to comment.