summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-06-29 10:36:01 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-06-29 10:36:01 +0200
commit4ed7271b1eca560fbad60c240cad23e85ac286f8 (patch)
tree0fc255912ec36735afe17c8870040d4ad768593c
parentc04cb305f45e39b71a7bdd51f91e8076eae86481 (diff)
downloadcollection-json-explorer-4ed7271b1eca560fbad60c240cad23e85ac286f8.tar.gz
collection-json-explorer-4ed7271b1eca560fbad60c240cad23e85ac286f8.tar.bz2
collection-json-explorer-4ed7271b1eca560fbad60c240cad23e85ac286f8.tar.xz
collection-json-explorer-4ed7271b1eca560fbad60c240cad23e85ac286f8.zip
o Adding examples from the specification.
-rw-r--r--app.js4
-rw-r--r--public/examples/from-spec/collection.collection+json66
-rw-r--r--public/examples/from-spec/error.collection+json12
-rw-r--r--public/examples/from-spec/item.collection+json26
-rw-r--r--public/examples/from-spec/minimal.collection+json7
-rw-r--r--public/examples/from-spec/queries.collection+json14
-rw-r--r--public/examples/from-spec/template.collection+json15
-rw-r--r--routes/index.js57
-rw-r--r--views/index.jade17
9 files changed, 196 insertions, 22 deletions
diff --git a/app.js b/app.js
index 509fcef..788679d 100644
--- a/app.js
+++ b/app.js
@@ -2,6 +2,10 @@ var express = require('express')
, routes = require('./routes')
, http = require('http');
+express.mime.define({
+ 'application/vnd.collection+json': ['collection+json']
+});
+
var app = express();
app.configure(function(){
diff --git a/public/examples/from-spec/collection.collection+json b/public/examples/from-spec/collection.collection+json
new file mode 100644
index 0000000..db57931
--- /dev/null
+++ b/public/examples/from-spec/collection.collection+json
@@ -0,0 +1,66 @@
+{ "collection" :
+ {
+ "version" : "1.0",
+ "href" : "http://example.org/friends/",
+
+ "links" : [
+ {"rel" : "feed", "href" : "http://example.org/friends/rss"}
+ ],
+
+ "items" : [
+ {
+ "href" : "http://example.org/friends/jdoe",
+ "data" : [
+ {"name" : "full-name", "value" : "J. Doe", "prompt" : "Full Name"},
+ {"name" : "email", "value" : "jdoe@example.org", "prompt" : "Email"}
+ ],
+ "links" : [
+ {"rel" : "blog", "href" : "http://examples.org/blogs/jdoe", "prompt" : "Blog"},
+ {"rel" : "avatar", "href" : "http://examples.org/images/jdoe", "prompt" : "Avatar", "render" : "image"}
+ ]
+ },
+
+ {
+ "href" : "http://example.org/friends/msmith",
+ "data" : [
+ {"name" : "full-name", "value" : "M. Smith", "prompt" : "Full Name"},
+ {"name" : "email", "value" : "msmith@example.org", "prompt" : "Email"}
+ ],
+ "links" : [
+ {"rel" : "blog", "href" : "http://examples.org/blogs/msmith", "prompt" : "Blog"},
+ {"rel" : "avatar", "href" : "http://examples.org/images/msmith", "prompt" : "Avatar", "render" : "image"}
+ ]
+ },
+
+ {
+ "href" : "http://example.org/friends/rwilliams",
+ "data" : [
+ {"name" : "full-name", "value" : "R. Williams", "prompt" : "Full Name"},
+ {"name" : "email", "value" : "rwilliams@example.org", "prompt" : "Email"}
+ ],
+ "links" : [
+ {"rel" : "blog", "href" : "http://examples.org/blogs/rwilliams", "prompt" : "Blog"},
+ {"rel" : "avatar", "href" : "http://examples.org/images/rwilliams", "prompt" : "Avatar", "render" : "image"}
+ ]
+ }
+ ],
+
+ "queries" : [
+ {"rel" : "search", "href" : "http://example.org/friends/search", "prompt" : "Search",
+ "data" : [
+ {"name" : "search", "value" : ""}
+ ]
+ }
+ ],
+
+ "template" : {
+ "data" : [
+ {"name" : "full-name", "value" : "", "prompt" : "Full Name"},
+ {"name" : "email", "value" : "", "prompt" : "Email"},
+ {"name" : "blog", "value" : "", "prompt" : "Blog"},
+ {"name" : "avatar", "value" : "", "prompt" : "Avatar"}
+
+ ]
+ }
+ }
+}
diff --git a/public/examples/from-spec/error.collection+json b/public/examples/from-spec/error.collection+json
new file mode 100644
index 0000000..bd67e3e
--- /dev/null
+++ b/public/examples/from-spec/error.collection+json
@@ -0,0 +1,12 @@
+{ "collection" :
+ {
+ "version" : "1.0",
+ "href" : "http://example.org/friends/",
+
+ "error" : {
+ "title" : "Server Error",
+ "code" : "X1C2",
+ "message" : "The server have encountered an error, please wait and try again."
+ }
+ }
+}
diff --git a/public/examples/from-spec/item.collection+json b/public/examples/from-spec/item.collection+json
new file mode 100644
index 0000000..8f93af6
--- /dev/null
+++ b/public/examples/from-spec/item.collection+json
@@ -0,0 +1,26 @@
+{ "collection" :
+ {
+ "version" : "1.0",
+ "href" : "http://example.org/friends/",
+
+ "links" : [
+ {"rel" : "feed", "href" : "http://example.org/friends/rss"},
+ {"rel" : "queries", "href" : "http://example.org/friends/?queries"},
+ {"rel" : "template", "href" : "http://example.org/friends/?template"}
+ ],
+
+ "items" : [
+ {
+ "href" : "http://example.org/friends/jdoe",
+ "data" : [
+ {"name" : "full-name", "value" : "J. Doe", "prompt" : "Full Name"},
+ {"name" : "email", "value" : "jdoe@example.org", "prompt" : "Email"}
+ ],
+ "links" : [
+ {"rel" : "blog", "href" : "http://examples.org/blogs/jdoe", "prompt" : "Blog"},
+ {"rel" : "avatar", "href" : "http://examples.org/images/jdoe", "prompt" : "Avatar", "render" : "image"}
+ ]
+ }
+ ]
+ }
+}
diff --git a/public/examples/from-spec/minimal.collection+json b/public/examples/from-spec/minimal.collection+json
new file mode 100644
index 0000000..2e2e65d
--- /dev/null
+++ b/public/examples/from-spec/minimal.collection+json
@@ -0,0 +1,7 @@
+{ "collection" :
+ {
+ "version" : "1.0",
+
+ "href" : "http://example.org/friends/"
+ }
+}
diff --git a/public/examples/from-spec/queries.collection+json b/public/examples/from-spec/queries.collection+json
new file mode 100644
index 0000000..1a3227d
--- /dev/null
+++ b/public/examples/from-spec/queries.collection+json
@@ -0,0 +1,14 @@
+{ "collection" :
+ {
+ "version" : "1.0",
+ "href" : "http://example.org/friends/",
+
+ "queries" : [
+ {"rel" : "search", "href" : "http://example.org/friends/search", "prompt" : "Search"
+ "data" : [
+ {"name" : "search", "value" : ""}
+ ]
+ }
+ ]
+ }
+}
diff --git a/public/examples/from-spec/template.collection+json b/public/examples/from-spec/template.collection+json
new file mode 100644
index 0000000..df16225
--- /dev/null
+++ b/public/examples/from-spec/template.collection+json
@@ -0,0 +1,15 @@
+{ "collection" :
+ {
+ "version" : "1.0",
+ "href" : "http://example.org/friends/",
+
+ "template" : {
+ "data" : [
+ {"name" : "full-name", "value" : "", "prompt" : "Full Name"},
+ {"name" : "email", "value" : "", "prompt" : "Email"},
+ {"name" : "blog", "value" : "", "prompt" : "Blog"},
+ {"name" : "avatar", "value" : "", "prompt" : "Avatar"}
+ ]
+ }
+ }
+}
diff --git a/routes/index.js b/routes/index.js
index 1a05e39..0bd432d 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -3,34 +3,53 @@ var http = require('http')
, collection_json = require('collection_json');
exports.index = function(req, res){
- res.render('index', { title: 'Express' });
+ res.render('index', {
+ title: 'Collection+JSON Explorer',
+ host: req.headers.host,
+ examples: [
+ "minimal",
+ "collection",
+ "item",
+ "queries",
+ "template",
+ "error"
+ ]
+ });
};
exports.render = function(req, res){
- var options = url.parse(req.query.url);
- options.method = 'GET';
- options.headers = {
- accept: 'application/vnd.collection+json'
- };
- var clientReq = http.request(options, function(clientRes) {
- clientRes.setEncoding('utf8');
- var body = '';
- clientRes.on('data', function (chunk) {
- body += chunk;
- });
- clientRes.on('end', function (chunk) {
+ fetchCollection(req.query.url, function(err, headers, body) {
+ if(err) {
+ res.render('index', { title: 'Failed to render ' + req.query.url });
+ }
+ else {
var parsedBody = JSON.parse(body);
var doc = collection_json.fromObject(parsedBody);
res.render('data', {
url: req.query.url,
doc: doc,
- headers: clientRes.headers,
+ headers: headers,
raw: body,
formattedRaw: JSON.stringify(parsedBody, null, ' ') });
- });
- });
- clientReq.on('error', function() {
- res.render('index', { title: 'Failed to render ' + req.query.url });
+ }
});
- clientReq.end();
};
+
+function fetchCollection(u, cb) {
+ var options = url.parse(u);
+ options.headers = {
+ accept: 'application/vnd.collection+json'
+ };
+ var req = http.get(options, function(res) {
+ res.setEncoding('utf8');
+ var body = '';
+ res.on('data', function (chunk) {
+ body += chunk;
+ });
+ res.on('end', function (chunk) {
+ cb(undefined, res.headers, body);
+ });
+ }).on('error', function() {
+ cb('Unable to fetch ' + u);
+ });
+}
diff --git a/views/index.jade b/views/index.jade
index af774d4..86cfffa 100644
--- a/views/index.jade
+++ b/views/index.jade
@@ -3,8 +3,19 @@ extends layout
block content
h1 Collection+JSON Explorer
- form(action="/render")
+ h2 Explore your own collection
+ form(action='/render')
p
label URL:
- input(name="url", value="http://p2k12.bitraf.no/checkins-trygvis.php")
- input(type="submit", value="Render")
+ input(name='url', value='http://p2k12.bitraf.no/checkins-trygvis.php')
+ input(type='submit', value='Render')
+
+ h2 Examples
+
+ p Taken from
+ a(url='http://amundsen.com/media-types/collection/examples/') Mike Admundsen's examples
+
+ each example in examples
+ form(action='/render')
+ input(name='url', type='hidden', value='http://#{host}/examples/from-spec/#{example}.collection+json')
+ input(type='submit', value='#{example}')