Skip to content

Commit

Permalink
Implement a basic version of Event JSON-LD transform
Browse files Browse the repository at this point in the history
Refs #27
  • Loading branch information
despawnerer committed Mar 25, 2016
1 parent 942ebc1 commit 511ae5a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
41 changes: 40 additions & 1 deletion client/src/js/models/event.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import moment from 'moment';

import Model from '../base/model';
import {capfirst, range} from '../utils';
import {capfirst, range, makeAbsoluteURL} from '../utils';

import Place from './place';

Expand Down Expand Up @@ -51,6 +51,16 @@ export default class Event extends Model {
isExhibition() {
return this.data.categories.indexOf('exhibition') !== -1;
}

getItemType() {
if (this.isFestival()) {
return 'Festival';
} else if (this.isExhibition()) {
return 'ExhibitionEvent';
} else {
return 'TheaterEvent';
}
}
}


Expand Down Expand Up @@ -92,9 +102,38 @@ export class Date {
return moment.duration((this.endTs - this.startTs) * 1000);
}

hasKnownEnd() {
return this.endTs != this.startTs;
}

isActual() {
return this.endTs > moment().unix();
}

toJSONLD(app) {
const url = app.resolver.reverse('event', {id: this.event.data.id});
const result = {
'@context': 'http://schema.org',
'@type': this.event.getItemType(),
name: this.event.getShortTitle(),
url: makeAbsoluteURL(url),
}

const place = this.event.getPlace();
if (place) {
result.location = place.toJSONLD(app);
}

if (this.isDateBased) {
result.startDate = this.start.format('YYYY-MM-DD');
result.endDate = this.end.format('YYYY-MM-DD');
} else {
result.startDate = this.start.format('YYYY-MM-DD[T]HH:mm');
if (this.hasKnownEnd()) result.endDate = this.end.format('YYYY-MM-DD[T]HH:mm');
}

return result;
}
}


Expand Down
24 changes: 4 additions & 20 deletions client/src/templates/event.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@
<div class="item-day-of-week"><%- date.start.format('dddd') %></div>
</div>
<div class="item-time">
<% if (date.start.isSame(date.end, 'minute')) { %>
<%- date.start.format('LT') %>
<% } else { %>
<% if (date.hasKnownEnd()) { %>
<%- date.start.format('LT') %>&ndash;<%- date.end.format('LT') %>
<% } else { %>
<%- date.start.format('LT') %>
<% } %>
</div>
<% } %>
Expand Down Expand Up @@ -151,23 +151,7 @@
</div>

<% event.getDates().forEach(function(date) { %>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "<% if (event.isFestival()) { %>Festival<% } else { %>TheaterEvent<% } %>",
<% if (date.end && date.end !== date.start) { %>"endDate": "<%- moment.unix(date.endTs).tz(location.timezone).format() %>",<% } %>
<% if (place) { %>
"location": {
"@type": "Place",
<% if (!place.isStub()) { %>"url": "<%- url('place', {id: place.data.id}) %>",<% } %>
"name": "<%- place.getTitle() %>",
"address": "<%- place.data.address %>"
},
<% } %>
"name": "<%- event.getShortTitle() %>",
"startDate": "<%- moment.unix(date.startTs).tz(location.timezone).format() %>"
}
</script>
<script type="application/ld+json"><%= JSON.stringify(date.toJSONLD(app)) %></script>
<% }) %>

<script type="application/ld+json">
Expand Down

0 comments on commit 511ae5a

Please sign in to comment.