Skip to content

Commit

Permalink
Allow rss to override link URL options
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Dec 15, 2021
1 parent 7c93bf0 commit 2ebed9a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hebcal/rest-api",
"version": "3.10.1",
"version": "3.10.2",
"author": "Michael J. Radwin (https://github.com/mjradwin)",
"keywords": [
"hebcal"
Expand Down
3 changes: 2 additions & 1 deletion rest-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ declare module '@hebcal/rest-api' {
*/
export function eventsToRss(events: Event[], location: Location, mainUrl: string, selfUrl: string, lang?: string, evPubDate?: boolean): string;
export function eventsToRss2(events: Event[], options: HebrewCalendar.Options): string;
export function eventToRssItem(ev: Event, evPubDate: boolean, lastBuildDate: string, dayFormat: Intl.DateTimeFormat, location: Location, baseUrl: string): string;
export function eventToRssItem(ev: Event, evPubDate: boolean, lastBuildDate: string, dayFormat: Intl.DateTimeFormat, location: Location, baseUrl: string, options?: HebrewCalendar.Options): string;
export function eventToRssItem2(ev: Event, options: HebrewCalendar.Options): string;
export function getDownloadFilename(options: HebrewCalendar.Options): string;
export function pad2(number: number): string;
export function pad4(number: number): string;
Expand Down
44 changes: 33 additions & 11 deletions src/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import holidayDescription from './holidays.json';
* @param {boolean} il
* @param {string} tzid
* @param {string} mainUrl
* @param {string} utmSource
* @param {string} utmMedium
* @return {string[]}
*/
function getLinkAndGuid(ev, il, tzid, mainUrl) {
function getLinkAndGuid(ev, il, tzid, mainUrl, utmSource, utmMedium) {
let link;
let guid;
const dt = ev.eventTime || ev.getDate().greg();
Expand All @@ -22,11 +24,11 @@ function getLinkAndGuid(ev, il, tzid, mainUrl) {
const anchor = `${dtAnchor}-${descAnchor}`;
const url0 = ev.url();
if (url0) {
link = appendIsraelAndTracking(url0, il, 'shabbat1c', 'rss').replace(/&/g, '&');
link = appendIsraelAndTracking(url0, il, utmSource, utmMedium).replace(/&/g, '&');
guid = `${url0}#${anchor}`;
} else {
const url1 = `${mainUrl}&dt=${dtStr}`;
const url = appendIsraelAndTracking(url1, il, 'shabbat1c', 'rss').replace(/&/g, '&');
const url = appendIsraelAndTracking(url1, il, utmSource, utmMedium).replace(/&/g, '&');
guid = url1.replace(/&/g, '&') + `#${anchor}`;
link = `${url}#${anchor}`;
}
Expand Down Expand Up @@ -73,23 +75,24 @@ const localeToLg = {
* @return {string}
*/
export function eventsToRss2(events, options) {
const dayFormat = new Intl.DateTimeFormat('en-US', {
options.dayFormat = new Intl.DateTimeFormat('en-US', {
weekday: 'long',
day: '2-digit',
month: 'long',
year: 'numeric',
});
const location = options.location;
const mainUrl = options.mainUrl;
const evPubDate = options.evPubDate;
const buildDate = options.buildDate || new Date();
const buildDate = options.buildDate = options.buildDate || new Date();
const thisYear = buildDate.getFullYear();
const lastBuildDate = buildDate.toUTCString();
const lastBuildDate = options.lastBuildDate = buildDate.toUTCString();
const title = options.title || getCalendarTitle(events, options);
const description = options.description || title;
const utmSource = options.utmSource || 'shabbat1c';
const utmMedium = options.utmMedium || 'rss';
const mainUrlEsc = appendIsraelAndTracking(mainUrl,
location && location.getIsrael(),
options.utmSource, options.utmMedium, options.utmCampaign).replace(/&/g, '&');
utmSource, utmMedium, options.utmCampaign).replace(/&/g, '&');
const selfUrlEsc = options.selfUrl.replace(/&/g, '&');
const lang = options.lang || localeToLg[options.locale] || options.locale || 'en-US';
let str = `<?xml version="1.0" encoding="UTF-8"?>
Expand All @@ -104,7 +107,7 @@ export function eventsToRss2(events, options) {
<lastBuildDate>${lastBuildDate}</lastBuildDate>
`;
events.forEach((ev) => {
str += eventToRssItem(ev, evPubDate, lastBuildDate, dayFormat, location, mainUrl);
str += eventToRssItem2(ev, options);
});
str += '</channel>\n</rss>\n';
return str;
Expand All @@ -129,22 +132,41 @@ function getPubDate(ev, evPubDate, evDate, lastBuildDate) {
return lastBuildDate;
}

/**
* @param {Event} ev
* @param {HebrewCalendar.Options} options
* @return {string}
*/
export function eventToRssItem2(ev, options) {
return eventToRssItem(
ev,
options.evPubDate,
options.lastBuildDate,
options.dayFormat,
options.location,
options.mainUrl,
options);
}

/**
* @param {Event} ev
* @param {boolean} evPubDate
* @param {string} lastBuildDate
* @param {Intl.DateTimeFormat} dayFormat
* @param {Location} location
* @param {string} mainUrl
* @param {HebrewCalendar.Options} [options]
* @return {string}
*/
export function eventToRssItem(ev, evPubDate, lastBuildDate, dayFormat, location, mainUrl) {
export function eventToRssItem(ev, evPubDate, lastBuildDate, dayFormat, location, mainUrl, options) {
let subj = ev.render();
const evDate = ev.getDate().greg();
const pubDate = getPubDate(ev, evPubDate, evDate, lastBuildDate);
const il = location ? location.getIsrael() : false;
const tzid = location ? location.getTzid() : 'UTC';
const linkGuid = getLinkAndGuid(ev, il, tzid, mainUrl);
const utmSource = (options && options.utmSource) || 'shabbat1c';
const utmMedium = (options && options.utmMedium) || 'rss';
const linkGuid = getLinkAndGuid(ev, il, tzid, mainUrl, utmSource, utmMedium);
const link = linkGuid[0];
const guid = linkGuid[1];
const categories = getEventCategories(ev);
Expand Down
8 changes: 5 additions & 3 deletions src/rss.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,24 @@ test('eventsToRss2', (t) => {
options.mainUrl = 'https://www.hebcal.com/shabbat?geonameid=4887398&m=50&lg=s';
options.selfUrl = 'https://www.hebcal.com/shabbat?cfg=r&geonameid=4887398&m=50&lg=s&pubDate=1';
options.buildDate = new Date(2021, 11, 15, 12, 34, 56);
options.utmSource = 'foobar';
options.utmMedium = 'quux';
options.description = 'The quick brown fox';
const rss = eventsToRss2(events, options).split('\n');
const expected = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<rss version="2.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:atom="http://www.w3.org/2005/Atom">',
'<channel>',
'<title>Hebcal Chicago April 1990</title>',
'<link>https://www.hebcal.com/shabbat?geonameid=4887398&amp;m=50&amp;lg=s&amp;utm_source=undefined&amp;utm_medium=undefined</link>',
'<link>https://www.hebcal.com/shabbat?geonameid=4887398&amp;m=50&amp;lg=s&amp;utm_source=foobar&amp;utm_medium=quux</link>',
'<atom:link href="https://www.hebcal.com/shabbat?cfg=r&amp;geonameid=4887398&amp;m=50&amp;lg=s&amp;pubDate=1" rel="self" type="application/rss+xml" />',
'<description>The quick brown fox</description>',
'<language>en-US</language>',
'<copyright>Copyright (c) 2021 Michael J. Radwin. All rights reserved.</copyright>',
'<lastBuildDate>Wed, 15 Dec 2021 20:34:56 GMT</lastBuildDate>',
'<item>',
'<title>Candle lighting: 7:03pm</title>',
'<link>https://www.hebcal.com/shabbat?geonameid=4887398&amp;m=50&amp;lg=s&amp;dt=1990-04-06&amp;utm_source=shabbat1c&amp;utm_medium=rss#19900406-candle-lighting</link>',
'<link>https://www.hebcal.com/shabbat?geonameid=4887398&amp;m=50&amp;lg=s&amp;dt=1990-04-06&amp;utm_source=foobar&amp;utm_medium=quux#19900406-candle-lighting</link>',
'<guid isPermaLink="false">https://www.hebcal.com/shabbat?geonameid=4887398&amp;m=50&amp;lg=s&amp;dt=1990-04-06#19900406-candle-lighting</guid>',
'<description>Friday, April 06, 1990</description>',
'<category>candles</category>',
Expand All @@ -76,7 +78,7 @@ test('eventsToRss2', (t) => {
'</item>',
'<item>',
'<title>Havdalah (50 min): 8:13pm</title>',
'<link>https://www.hebcal.com/shabbat?geonameid=4887398&amp;m=50&amp;lg=s&amp;dt=1990-04-07&amp;utm_source=shabbat1c&amp;utm_medium=rss#19900407-havdalah</link>',
'<link>https://www.hebcal.com/shabbat?geonameid=4887398&amp;m=50&amp;lg=s&amp;dt=1990-04-07&amp;utm_source=foobar&amp;utm_medium=quux#19900407-havdalah</link>',
'<guid isPermaLink="false">https://www.hebcal.com/shabbat?geonameid=4887398&amp;m=50&amp;lg=s&amp;dt=1990-04-07#19900407-havdalah</guid>',
'<description>Saturday, April 07, 1990</description>',
'<category>havdalah</category>',
Expand Down

0 comments on commit 2ebed9a

Please sign in to comment.