Skip to content

Commit 5b46058

Browse files
committed
Merge pull request #67 from hbeeken/swarm-updating-location-info
Pulling out checkin location information into msg properties
2 parents f71b19a + 03d2c12 commit 5b46058

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

foursquare/swarm.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ module.exports = function(RED) {
117117
var latest = result.response.checkins.items[0];
118118
msg.payload = {};
119119
msg.payload = latest;
120+
msg.location = {};
121+
122+
if (latest.location && (latest.location !== 'undefined')) {
123+
msg.location.lat = latest.location.lat;
124+
msg.location.lon = latest.location.lng;
125+
msg.location.name = latest.location.name;
126+
msg.name = latest.location.name;
127+
}
128+
if (latest.venue && (latest.venue !== 'undefined')) {
129+
// this is ok to overwrite anything set before because the location property
130+
// may or may not be there, and the same with the venue property
131+
msg.location.lat = latest.venue.location.lat;
132+
msg.location.lon = latest.venue.location.lng;
133+
msg.location.city = latest.venue.location.city;
134+
msg.location.country = latest.venue.location.country;
135+
msg.location.name = latest.venue.name;
136+
msg.name = latest.venue.name;
137+
}
120138
callback(msg);
121139
} else {
122140
if(afterTimestamp === null) { // if query node, always return something, when no check-ins, return empty payload

test/foursquare/swarm_spec.js

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,26 @@ describe('swarm nodes', function() {
5353
var scope = nock('https://api.foursquare.com:443')
5454
.filteringPath(/afterTimestamp=[^&]*/g, 'afterTimestamp=foo')
5555
.get('/v2/users/self/checkins?oauth_token=abcd1234&sort=newestfirst&m=swarm&v=20141016')
56-
.reply(200, {"meta":{"code":200},"response":{"checkins":{"count":1, "items":[{"id":"b695edf5ewc2","createdAt":1412861751,"type":"checkin","timeZoneOffset":60,"venue":{"id":"49a8b774","name":"Bobs House"}}]}}});
56+
.reply(200, {"meta":{"code":200},"response":{"checkins":{"count":1,
57+
"items":[{"id":"b695edf5ewc2","createdAt":1412861751,"type":"checkin","timeZoneOffset":60,
58+
"venue":{"id":"49a8b774","name":"Bobs House",
59+
"location":{"lat":"1234", "lng":"-1234","name":"Bobs House", "city":"Winchester", "country":"England"}}
60+
}]}}});
5761

5862
var n1 = helper.getNode("n1");
5963
var n2 = helper.getNode("n2");
6064
var n3 = helper.getNode("n3");
6165
n2.should.have.property('id','n2');
6266
n1.send({payload:"foo"});
6367
n3.on('input', function(msg){
64-
var venue = msg.payload.venue;
65-
venue.should.have.property('name', "Bobs House");
68+
msg.payload.venue.should.have.property('name', "Bobs House");
69+
msg.should.have.property('name', "Bobs House");
70+
msg.should.have.property('location');
71+
msg.location.should.have.property('lat', "1234");
72+
msg.location.should.have.property('lon', "-1234");
73+
msg.location.should.have.property('city', "Winchester");
74+
msg.location.should.have.property('country', "England");
75+
msg.location.should.have.property('name', "Bobs House");
6676
done();
6777
});
6878
});
@@ -102,6 +112,39 @@ describe('swarm nodes', function() {
102112
n1.send({payload:"foo"});
103113
});
104114
});
115+
116+
it('passes on null msg.payload if no results from query', function(done) {
117+
helper.load([foursquareNode, swarmNode],
118+
[ {id:"n1", type:"helper", wires:[["n2"]]},
119+
{id:"n4", type:"foursquare-credentials"},
120+
{id:"n2", type:"swarm", foursquare: "n4", wires:[["n3"]]},
121+
{id:"n3", type:"helper"}],
122+
{
123+
"n4": {
124+
displayname : "John",
125+
clientid: "987654321",
126+
clientsecret:"123456789",
127+
accesstoken:"abcd1234",
128+
},
129+
},
130+
function() {
131+
var scope = nock('https://api.foursquare.com:443')
132+
.filteringPath(/afterTimestamp=[^&]*/g, 'afterTimestamp=foo')
133+
.get('/v2/users/self/checkins?oauth_token=abcd1234&sort=newestfirst&m=swarm&v=20141016')
134+
.reply(200, {"meta":{"code":200},"response":{"checkins":{"count":1,
135+
"items":[]}}});
136+
137+
var n1 = helper.getNode("n1");
138+
var n2 = helper.getNode("n2");
139+
var n3 = helper.getNode("n3");
140+
n2.should.have.property('id','n2');
141+
n1.send({payload:"foo"});
142+
n3.on('input', function(msg){
143+
msg.should.have.property('payload', null);
144+
done();
145+
});
146+
});
147+
});
105148

106149
}}
107150
);
@@ -129,16 +172,23 @@ describe('swarm nodes', function() {
129172
var scope = nock('https://api.foursquare.com:443')
130173
.filteringPath(/afterTimestamp=[^&]*/g, 'afterTimestamp=foo')
131174
.get('/v2/users/self/checkins?oauth_token=abcd1234&sort=newestfirst&m=swarm&v=20141016&afterTimestamp=foo')
132-
.reply(200, {"meta":{"code":200},"response":{"checkins":{"count":1, "items":[{"id":"b695edf5ewc2","createdAt":1412861751,"type":"checkin","timeZoneOffset":60,"venue":{"id":"49a8b774","name":"Bobs House"}}]}}});
175+
.reply(200, {"meta":{"code":200},"response":{"checkins":{"count":1,
176+
"items":[{"id":"b695edf5ewc2","createdAt":1412861751,"type":"checkin","timeZoneOffset":60,
177+
"location":{"lat":"1234", "lng":"-1234","name":"Bobs House"}}]}}});
133178

134179
var n1 = helper.getNode("n1");
135180
var n2 = helper.getNode("n2");
136181
var n3 = helper.getNode("n3");
137182
n2.should.have.property('id','n2');
138183
n2.emit("input", {});
139184
n3.on('input', function(msg){
140-
var venue = msg.payload.venue;
141-
venue.should.have.property('name', "Bobs House");
185+
msg.should.have.property('name', "Bobs House");
186+
msg.should.have.property('location');
187+
msg.location.should.have.property('lat', "1234");
188+
msg.location.should.have.property('lon', "-1234");
189+
msg.location.should.not.have.property('city');
190+
msg.location.should.not.have.property('country');
191+
msg.location.should.have.property('name', "Bobs House");
142192
done();
143193
});
144194
});

0 commit comments

Comments
 (0)