summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-06-30 01:18:33 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2012-06-30 01:18:33 +0200
commit4af799c68089d2a56f69fc67c4c0cd9c82d176b8 (patch)
tree3aa05e2caae87e5c8e0b46901577fd19039565ec
parentcc70ec9cb09172167e7ed334b2ab1def391c6d59 (diff)
downloadcollection-json-explorer-4af799c68089d2a56f69fc67c4c0cd9c82d176b8.tar.gz
collection-json-explorer-4af799c68089d2a56f69fc67c4c0cd9c82d176b8.tar.bz2
collection-json-explorer-4af799c68089d2a56f69fc67c4c0cd9c82d176b8.tar.xz
collection-json-explorer-4af799c68089d2a56f69fc67c4c0cd9c82d176b8.zip
o Prettier and better error handling.
-rw-r--r--.gitignore1
-rw-r--r--routes/index.js26
-rw-r--r--views/data.jade101
-rw-r--r--views/index.jade12
4 files changed, 96 insertions, 44 deletions
diff --git a/.gitignore b/.gitignore
index 3c3629e..93f1361 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
node_modules
+npm-debug.log
diff --git a/routes/index.js b/routes/index.js
index 6d57023..bceafc3 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -2,6 +2,14 @@ var http = require('http')
, url = require('url')
, collection_json = require('collection_json');
+function urlgenerator(req) {
+ var host = req.headers.host;
+ return {
+ render: function(u) {
+ return 'http://' + host + '/render?url=' + encodeURIComponent(u)}
+ };
+}
+
exports.index = function(req, res){
res.render('index', {
title: 'Collection+JSON Explorer',
@@ -17,14 +25,23 @@ exports.index = function(req, res){
});
};
-exports.render = function(req, res){
+exports.render = function(req, res) {
+ function sendErr(err) {
+ res.render('data', {
+ url: req.query.url,
+ err: err
+ });
+ }
fetchCollection(req.query.url, function(err, headers, body) {
if(err) {
- res.render('index', { title: 'Failed to render ' + req.query.url });
+ sendErr(err);
}
else {
- var parsedBody = JSON.parse(body);
- var doc = collection_json.fromObject(parsedBody);
+ var parsedBody;
+ try {
+ parsedBody = JSON.parse(body);
+ } catch(e) { sendErr('Unable to parse JSON: ' + e); }
+ var doc = collection_json.fromObject(parsedBody).collection;
var isUrl = function(u) {
try {
var x = url.parse(u);
@@ -36,6 +53,7 @@ exports.render = function(req, res){
};
res.render('data', {
isUrl: isUrl,
+ urlgenerator: urlgenerator(req),
url: req.query.url,
doc: doc,
headers: headers,
diff --git a/views/data.jade b/views/data.jade
index 15189e9..0bd5afd 100644
--- a/views/data.jade
+++ b/views/data.jade
@@ -1,12 +1,23 @@
extends layout
+block href
+ if typeof href != 'string'
+ | &lt;no href>
+ else
+ span
+ a(href=href) #{href}
+ |
+ a(href=urlgenerator.render(href)) Explore
+
block link
- var name = typeof link.name == 'string' ? link.name : '<no name attribute>'
- var prompt = typeof link.prompt == 'string' ? link.prompt : '<no prompt attribute>'
+ - var href = link.href
table
tr
td href
- td: a(href=link.href, name=link.name) #{link.href}
+ td
+ block href
tr
td rel
td
@@ -25,14 +36,18 @@ block link
td
td: img(src=link.href, alt=link.name, title=name)
-block content
- h1 Collection+JSON Explorer
+block err
+ p Error rendering #{url}
+
+ p= err
+
+block data
p Viewing #{url}
form(action="/render")
p
label URL:
- input(name="url", value=url)
+ input(name="url", size=100, value=url)
input(type="submit", value="Render")
form(action="http://redbot.org/?")
@@ -52,47 +67,53 @@ block content
li
a(href="#body") Raw body
- a(id="meta")
- h1
- a(href="#meta") Meta
+ a(id="meta"): h1: a(href="#meta") Meta
- table
- tr
- td Version
- td #{doc.version}
- tr
- td URI
- td
- if typeof doc.uri == 'string'
- a(href=doc.uri) #{doc.uri}
- else
- span No URI
+ - var href=doc.href
+ table
+ tr
+ td version
+ td #{doc.version}
+ tr
+ td href
+ td: block href
- h2 Collection Links
+ h2 Collection Links
+ if doc.links.length == 0
+ p Collection has no links.
+ else
each link, i in doc.links
- h3 Collection Link ##{i}
+ - var title = link.prompt || link.name
+ - title = title ? ": " + title : ""
+ h3 Collection Link ##{i}#{title}
block link
a(id="items"): h1: a(href="#items") Items
- each item, i in doc.items
- a(id="item-#{i+1}")
- h2: a(href="#item-#{i+1}") Item ##{i+1}
+ if doc.items.length == 0
+ p Collection has to items.
+ else
+ p The feed has #{doc.items.length} items.
+ each item, i in doc.items
+ a(id="item-#{i+1}"): h2: a(href="#item-#{i+1}") Item ##{i+1}
- if typeof item.href == 'string'
- | href
- a(href=item.href) #{item.href}
+ - var href=item.href
+ p href:
+ block href
- h2 Item Links
- each link, i in item.links
- h3 Item Link ##{i}
- block link
+ h3 Item Links
+ if item.links.length == 0
+ p Item has no links.
+ else
+ each link, i in item.links
+ h4 Item Link ##{i}
+ block link
- h3 Data
- table
- each data in item.data
- tr
- td #{data.name}
- td #{data.value}
+ h3 Data
+ table
+ each data in item.data
+ tr
+ td #{data.name}
+ td #{data.value}
a(id="headers"): h1: a(href="#headers") Headers
pre
@@ -107,3 +128,11 @@ block content
a(id="body"): h1: a(href="#body") Raw body
pre= raw
+
+block content
+ h1 Collection+JSON Explorer
+
+ if typeof err == 'undefined'
+ block data
+ else
+ block err
diff --git a/views/index.jade b/views/index.jade
index 5625546..cd12e4a 100644
--- a/views/index.jade
+++ b/views/index.jade
@@ -3,19 +3,23 @@ extends layout
block content
h1 Collection+JSON Explorer
+ div This is a simple/dumb explorer for the
+ a(href='http://amundsen.com/media-types/collection/') Collection+JSON Hypermedia
+ | .
+
h2 Explore your own collection
form(action='/render')
p
label URL:
- input(name='url', value='http://p2k12.bitraf.no/checkins-trygvis.php')
+ input(name='url', size='100', value='http://example-collection-json-db.herokuapp.com/')
input(type='submit', value='Render')
h2 Examples
-
- p Taken from:
- a(href='http://amundsen.com/media-types/collection/examples/') Mike Amundsen's examples
+ p Taken from:
+ a(href='http://amundsen.com/media-types/collection/examples/') Mike Amundsen's examples
ul
+ - var examples = examples || []
each example in examples
li
- var u='http://' + host + '/render?url=' + encodeURIComponent('http://' + host + '/examples/from-spec/' + example + '.collection+json')