Skip to content

Commit b6c7318

Browse files
fetch active flights
1 parent 016df1e commit b6c7318

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<title>habitat api test </title>
66
<script type="text/javascript" language="javascript" src="js/jquery-1.8.3-min.js"></script>
77
<script type="text/javascript" language="javascript" src="js/jquery.couch.js"></script>
8+
<script type="text/javascript" language="javascript" src="js/habitat.js"></script>
9+
<script type="text/javascript" language="javascript" src="js/habitat.tracker.js"></script>
10+
<script type="text/javascript" language="javascript" src="js/habitat.tracker.db.js"></script>
811
</head>
912
<body>
1013
</body>

js/habitat.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This file is part of Habitat Mobile Tracker
2+
3+
// root object
4+
var habitat = {
5+
// initialize
6+
db: $.couch.db("habitat")
7+
};
8+
9+
// temporary fix for CORS
10+
habitat.db.uri = "http://habitat.habhub.org/habitat/";

js/habitat.tracker.db.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
habitat.tracker.db = {
2+
3+
// pulls latest flights from habitat
4+
get_flights: function(callback, selector) {
5+
var ts = Math.floor(new Date().getTime() / 1000);
6+
7+
habitat.db.view("flight/end_start_including_payloads", {
8+
startkey: [ts],
9+
include_docs: true,
10+
success: function(data) {
11+
var last = null, list = [], payloads;
12+
13+
// merge payload_configs into flight docs
14+
for(k in data.rows) {
15+
var doc = data.rows[k].doc;
16+
17+
if(doc.type == "flight" && doc.approved == true) {
18+
last = doc;
19+
payloads = doc.payloads;
20+
doc.payloads = [];
21+
list.push(data.rows[k]);
22+
} else if(payloads.indexOf(doc._id) != -1) {
23+
last.payloads.push(doc);
24+
}
25+
}
26+
27+
// return results
28+
callback(list);
29+
},
30+
});
31+
}
32+
};

js/habitat.tracker.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
habitat.tracker = {
2+
flights: { active: [], upcoming: [] },
3+
4+
5+
init: function() {
6+
7+
// pull flight list
8+
this.db.get_flights(function(data) {
9+
var ts = Math.floor(new Date().getTime() / 1000);
10+
11+
for(k in data) {
12+
if(data[k].key[1] < ts) habitat.tracker.flights.active.push(data[k]);
13+
else habitat.tracker.flights.upcoming.push(data[k]);
14+
}
15+
});
16+
},
17+
18+
test: function() {
19+
var out = $('body');
20+
21+
out.append("Upcoming flights<br><br>");
22+
this.append(this.flights.upcoming);
23+
out.append("<br>Active flights<br><br>");
24+
this.append(this.flights.active);
25+
},
26+
27+
append: function(ref) {
28+
var out = $('body');
29+
30+
for(k in ref) { out.append(new Date(ref[k].doc.start).toUTCString() + " " + ref[k].doc.name + "<br />"); }
31+
}
32+
}

0 commit comments

Comments
 (0)