Commit fdf150df4730f1837e68eb0bef9c9773b317f37a

Authored by Adhidarma Hadiwinoto
1 parent f1fc4a3bf5
Exists in master

json dan plain

Showing 5 changed files with 100 additions and 67 deletions Side-by-side Diff

... ... @@ -3,6 +3,25 @@ var config = iniparser.parseSync('./config.ini');
3 3 var whiskers = require('whiskers');
4 4  
5 5 var numeral = require('numeral');
  6 +numeral.language('id', {
  7 + delimiters: {
  8 + thousands: '.',
  9 + decimal: ','
  10 + },
  11 + abbreviations: {
  12 + thousand: 'k',
  13 + million: 'm',
  14 + billion: 'b',
  15 + trillion: 't'
  16 + },
  17 + ordinal : function (number) {
  18 + return number === 1 ? 'er' : 'ème';
  19 + },
  20 + currency: {
  21 + symbol: 'Rp.'
  22 + }
  23 +});
  24 +numeral.language('id');
6 25  
7 26 var Sync = require('sync');
8 27 var redis = require('redis').createClient(config.globals.redis_port, config.globals.redis_host);
... ... @@ -13,9 +32,12 @@ var app = express();
13 32 var suppliers = config.globals.suppliers.split(',');
14 33  
15 34 app.engine('.html', whiskers.__express);
  35 +app.engine('.txt', whiskers.__express);
  36 +app.engine('.json', whiskers.__express);
  37 +
16 38 app.set('views', __dirname+'/views');
17 39  
18   -function getBalances(suppliers, callback) {
  40 +function getBalances(suppliers, thousands, callback) {
19 41  
20 42 var kw = [];
21 43 var count = suppliers.length;
... ... @@ -29,9 +51,17 @@ function getBalances(suppliers, callback) {
29 51 var balances = [];
30 52  
31 53 for (var i=0; i<count; i++) {
  54 + var value = res[i];
  55 + if (!value) {
  56 + value = 0;
  57 + }
  58 +
  59 + if (thousands) {
  60 + value = numeral(res[i]).format('0,0');
  61 + }
32 62 balances.push({
33 63 title: suppliers[i],
34   - balance: numeral(res[i]).format('0,0')
  64 + balance: value,
35 65 });
36 66 }
37 67  
... ... @@ -42,12 +72,27 @@ function getBalances(suppliers, callback) {
42 72 app.get('/', function(req, res) {
43 73  
44 74 Sync(function() {
  75 + var template_file;
  76 + var thousands = true;
  77 +
  78 + res.format({
  79 + text: function() {
  80 + template_file = 'index.txt';
  81 + },
  82 + json: function() {
  83 + thousands = false;
  84 + template_file = 'index.json';
  85 + },
  86 + 'default': function() {
  87 + template_file = 'index.html';
  88 + },
  89 +
  90 + });
45 91  
46   - var balances = getBalances.sync(null, suppliers);
  92 + var balances = getBalances.sync(null, suppliers, thousands);
47 93 //balances = getBalances(suppliers);
48 94  
49   - res.render('basic_template.html', {
50   - // logo, berdasar isu #1
  95 + res.render(template_file, {
51 96 logo: 'http://reload97.com/sites/default/files/web-reload97.png',
52 97 title: 'Supplier Balances',
53 98 balances: balances,
views/basic_template.html
... ... @@ -1,50 +0,0 @@
1   -<!DOCTYPE html>
2   -<html lang="en">
3   - <head>
4   - <meta charset="utf-8">
5   - <meta http-equiv="X-UA-Compatible" content="IE=edge">
6   - <meta name="viewport" content="width=device-width, initial-scale=1">
7   - <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
8   - <title>{title}</title>
9   -
10   - <!-- Latest compiled and minified CSS -->
11   - <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
12   -
13   - <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
14   - <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
15   - <!--[if lt IE 9]>
16   - <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
17   - <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
18   - <![endif]-->
19   - </head>
20   - <body>
21   - <div class="pull-left">
22   - <img src="{logo}">
23   - </div>
24   - <h1>
25   - <br/><br/>
26   - {title}
27   - </h1>
28   -
29   - <table class="table table-hover table-striped">
30   - {for balance in balances}
31   - <tr>
32   - <td>{balance.title}</td>
33   - <td>
34   - <div class="pull-right">
35   - {balance.balance}
36   - </div>
37   - </td>
38   - </tr>
39   - {/for}
40   - </table>
41   -
42   - Generated on {timestamp}
43   -
44   -
45   - <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
46   - <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
47   - <!-- Latest compiled and minified JavaScript -->
48   - <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
49   - </body>
50   -</html>
1   -<html>
2   -<head>
  1 +<!DOCTYPE html>
  2 +<html lang="en">
  3 + <head>
  4 + <meta charset="utf-8">
  5 + <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6 + <meta name="viewport" content="width=device-width, initial-scale=1">
  7 + <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
3 8 <title>{title}</title>
4   -</head>
5   -<body>
6   -<h1>{title}</h1>
7 9  
8   -<table>
  10 + <!-- Latest compiled and minified CSS -->
  11 + <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  12 +
  13 + <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
  14 + <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  15 + <!--[if lt IE 9]>
  16 + <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
  17 + <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  18 + <![endif]-->
  19 + </head>
  20 + <body>
  21 + <div class="pull-left">
  22 + <img src="{logo}">
  23 + </div>
  24 + <h1>
  25 + <br/><br/>
  26 + {title}
  27 + </h1>
  28 +
  29 + <table class="table table-hover table-striped">
9 30 {for balance in balances}
10   - <tr>
11   - <td>{balance.title}</td>
12   - <td class="pull-right">{balance.balance}</td>
13   - </tr>
  31 + <tr>
  32 + <td>{balance.title}</td>
  33 + <td>
  34 + <div class="pull-right">
  35 + {balance.balance}
  36 + </div>
  37 + </td>
  38 + </tr>
14 39 {/for}
15   -</table>
16   -</body>
  40 + </table>
  41 +
  42 + Generated on {timestamp}
  43 +
  44 +
  45 + <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
  46 + <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  47 + <!-- Latest compiled and minified JavaScript -->
  48 + <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  49 + </body>
17 50 </html>
... ... @@ -0,0 +1,3 @@
  1 +{
  2 +{for balance in balances}{balance.title}: {balance.balance},
  3 +{/for}}
... ... @@ -0,0 +1,2 @@
  1 +{for balance in balances}{balance.title} {balance.balance}
  2 +{/for}