Skip to content

Commit

Permalink
Updated RadioTAG support to match Draft 6
Browse files Browse the repository at this point in the history
  • Loading branch information
andybee committed Jun 3, 2015
1 parent 3519c62 commit 73ac7b9
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 49 deletions.
6 changes: 5 additions & 1 deletion models/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ module.exports = function(sequelize, DataTypes) {
primaryKey: true,
allowNull: false
},
station: {
bearer: {
type: DataTypes.STRING,
validate: {
notEmpty: true
}
},
time: {
type: DataTypes.DATE
},
time_source: {
type: DataTypes.STRING,
allowNull: true
}
}, {
underscored: true,
Expand Down
28 changes: 15 additions & 13 deletions routes/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ module.exports = function(app) {
};

var getTagTitle = function(tag) {
return "Tag: " + tag.station + " at " + formatAtomDate(tag.time);
return "Tag: " + tag.bearer + " at " + formatAtomDate(tag.time);
};

var getTagDescription = function(tag) {
return "Description of tag: " + tag.station + " at " + formatAtomDate(tag.time);
return "Description of tag: " + tag.bearer + " at " + formatAtomDate(tag.time);
};

var tagToAtomEntry = function(tag) {
// TODO: replace dummy values
var entry = {
title: getTagTitle(tag),
serviceId: tag.station,
serviceName: tag.station,
serviceUri: tag.bearer,
serviceName: tag.bearer,
imageUrl: "http://example.com/image.png",
canonicalUrl: "http://example.com/" + tag.station + "/" + dateToSeconds(tag.time),
canonicalUrl: "http://example.com/" + tag.bearer + "/" + dateToSeconds(tag.time),
uniqueId: "urn:uuid:" + tag.id,
dateUpdated: formatAtomDate(tag.time),
datePublished: formatAtomDate(tag.time),
Expand Down Expand Up @@ -159,13 +159,13 @@ module.exports = function(app) {
* RadioTag tag creation endpoint
*
* Parameters:
* - station: radio station identifier
* - bearer: radio service TX params
* - time: timestamp of tag (seconds since the Unix epoch)
*/

var postTagHandler = function(req, res, next) {
if (!req.body.station) {
res.json(400, { error: 'missing/invalid station parameter' });
if (!req.body.bearer) {
res.json(400, { error: 'missing/invalid bearer parameter' });
return;
}

Expand All @@ -186,10 +186,11 @@ module.exports = function(app) {

db.Tag
.create({
id: uuid.v4(),
station: req.body.station,
time: time,
client_id: req.device.id
id: uuid.v4(),
bearer: req.body.bearer,
time: time,
time_source: req.body.time_source,
client_id: req.device.id
})
.complete(function(err, tag) {
if (err) {
Expand Down Expand Up @@ -223,7 +224,8 @@ module.exports = function(app) {
client: tag.client_id,
user: tag.client.user_id,
time: formatAtomDate(tag.time),
station: tag.station,
time_source: tag.time_source,
bearer: tag.bearer,
title: getTagTitle(tag),
description: getTagDescription(tag)
};
Expand Down
97 changes: 66 additions & 31 deletions test/lib/tag-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,29 @@ var initDatabase = function(done) {
})
.then(function() {
return db.Tag.create({
id: 'cc5355c3-93f1-4616-9a54-9093a0c656fc',
client_id: 11,
station: 'radio1',
time: new Date(1391017811000)
id: 'cc5355c3-93f1-4616-9a54-9093a0c656fc',
client_id: 11,
bearer: 'radio1',
time: new Date(1391017811000),
time_source: 'broadcast'
});
})
.then(function() {
return db.Tag.create({
id: '4575bf06-9919-4237-9eb7-8c4f42885774',
client_id: 11,
station: 'radio2',
time: new Date(1391017812000)
id: '4575bf06-9919-4237-9eb7-8c4f42885774',
client_id: 11,
bearer: 'radio2',
time: new Date(1391017812000),
time_source: 'broadcast'
});
})
.then(function() {
return db.Tag.create({
id: 'e39ec3f6-09c9-4b58-82c1-70782aa8ad7c',
client_id: 11,
station: 'radio3',
time: new Date(1391017810000)
id: 'e39ec3f6-09c9-4b58-82c1-70782aa8ad7c',
client_id: 11,
bearer: 'radio3',
time: new Date(1391017810000),
time_source: 'broadcast'
});
})
.then(function() {
Expand Down Expand Up @@ -137,16 +140,16 @@ describe("GET /tags", function() {
});
});

describe("the radiotag:sid field", function() {
it("should equal the tag's station identifier", function() {
verifyXPathNodeValue(this.doc, "/feed/entry[1]/*[local-name(.)='sid']", 'radio2');
verifyXPathNodeValue(this.doc, "/feed/entry[2]/*[local-name(.)='sid']", 'radio1');
verifyXPathNodeValue(this.doc, "/feed/entry[3]/*[local-name(.)='sid']", 'radio3');
describe("the radiotag:service field uri attribute", function() {
it("should equal the tag's radio service bearer URI", function() {
verifyXPathAttribute(this.doc, "/feed/entry[1]/*[local-name(.)='service']/@uri", 'radio2');
verifyXPathAttribute(this.doc, "/feed/entry[2]/*[local-name(.)='service']/@uri", 'radio1');
verifyXPathAttribute(this.doc, "/feed/entry[3]/*[local-name(.)='service']/@uri", 'radio3');
});
});

describe("the radiotag:service field", function() {
it("should equal the tag's station name", function() {
it("should equal the tag's radio service name", function() {
// TODO: should be station display local-name
verifyXPathNodeValue(this.doc, "/feed/entry[1]/*[local-name(.)='service']", 'radio2');
verifyXPathNodeValue(this.doc, "/feed/entry[2]/*[local-name(.)='service']", 'radio1');
Expand Down Expand Up @@ -243,8 +246,9 @@ describe("POST /tag", function() {
.reply(200, { user_id: 11, client_id: 12 });

var data = {
station: 'radio1',
time: 1390981693
bearer: 'radio1',
time: 1390981693,
time_source: 'broadcast'
};

requestHelper.sendRequest(this, '/tag', {
Expand Down Expand Up @@ -308,9 +312,9 @@ describe("POST /tag", function() {
});
});

describe("the radiotag:sid field", function() {
it("should equal the tag's station identifier", function() {
verifyXPathNodeValue(this.doc, "/feed/entry/*[local-name(.)='sid']", 'radio1');
describe("the radiotag:service uri attribute", function() {
it("should equal the tag's service bearer URI", function() {
verifyXPathAttribute(this.doc, "/feed/entry/*[local-name(.)='service']/@uri", 'radio1');
});
});

Expand Down Expand Up @@ -389,7 +393,7 @@ describe("POST /tag", function() {
});

it("should have the correct station identifier", function() {
expect(this.tag.station).to.equal('radio1');
expect(this.tag.bearer).to.equal('radio1');
});

it("should be associated with the correct client id", function() {
Expand All @@ -410,8 +414,9 @@ describe("POST /tag", function() {
.reply(200, { user_id: 11, client_id: 12 });

var data = {
station: 'radio1'
// time: 1390981693
bearer: 'radio1',
// time: 1390981693,
time_source: 'broadcast'
};

requestHelper.sendRequest(this, '/tag', {
Expand All @@ -437,8 +442,9 @@ describe("POST /tag", function() {
.reply(200, { user_id: 11, client_id: 12 });

var data = {
station: 'radio1',
time: 'invalid'
bearer: 'radio1',
time: 'invalid',
time_source: 'broadcast'
};

requestHelper.sendRequest(this, '/tag', {
Expand All @@ -453,7 +459,7 @@ describe("POST /tag", function() {
});
});

context("with missing station identifier", function() {
context("with missing service bearer", function() {
before(dbHelper.clearDatabase);

before(function(done) {
Expand All @@ -464,8 +470,9 @@ describe("POST /tag", function() {
.reply(200, { user_id: 11, client_id: 12 });

var data = {
// station: 'radio1',
time: 1390981693
// bearer: 'radio1',
time: 1390981693,
time_source: 'broadcast'
};

requestHelper.sendRequest(this, '/tag', {
Expand All @@ -479,6 +486,34 @@ describe("POST /tag", function() {
expect(this.res.statusCode).to.equal(400);
});
});

context("with missing time source", function() {
before(dbHelper.clearDatabase);

before(function(done) {
var config = app.get('config');

nock(config.authorization_provider.base_uri)
.post('/authorized')
.reply(200, { user_id: 11, client_id: 12 });

var data = {
bearer: 'radio1',
time: 1390981693,
// time_source: 'broadcast'
};

requestHelper.sendRequest(this, '/tag', {
method: 'post',
data: data,
accessToken: '123abc'
}, done);
});

it("should return status 201", function() {
expect(this.res.statusCode).to.equal(201);
});
});
});

describe("GET /tags/all", function() {
Expand Down
3 changes: 1 addition & 2 deletions views/tags-atom.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
<% entries.forEach(function(entry) { %>
<entry>
<title><%= entry.title %></title>
<radiotag:sid><%= entry.serviceId %></radiotag:sid>
<radiotag:service><%= entry.serviceName %></radiotag:service>
<radiotag:service uri="<%= entry.serviceUri %>"><%= entry.serviceName %></radiotag:service>
<link rel="image" href="<%= entry.imageUrl %>"/>
<link rel="canonical" href="<%= entry.canonicalUrl %>"/>
<id><%= entry.uniqueId %></id>
Expand Down
6 changes: 4 additions & 2 deletions views/tags.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
<td>Client</td>
<td>User</td>
<td>Time</td>
<td>Station</td>
<td>Time Source</td>
<td>Bearer</td>
<td>Title</td>
<td>Description</td>
</tr>
Expand All @@ -19,7 +20,8 @@
<td><%= tag.client %></td>
<td><%= tag.user %></td>
<td><%= tag.time %></td>
<td><%= tag.station %></td>
<td><%= tag.time_source %></td>
<td><%= tag.bearer %></td>
<td><%= tag.title %></td>
<td><%= tag.description %></td>
</tr>
Expand Down

0 comments on commit 73ac7b9

Please sign in to comment.