Permalink
Browse files

[mongodb] agrego el mongodb y lo uso directamente. Muestro la tendenc…

…ia respecto a la ultima consulta
  • Loading branch information...
cronopio
cronopio committed Jun 13, 2011
1 parent 3b07cd8 commit ab2f5fa6042b5e0d1b72f2c25450940265855015
Showing with 62 additions and 18 deletions.
  1. +1 −0 package.json
  2. +47 −8 server.js
  3. +14 −10 views/index.jade
View
@@ -6,5 +6,6 @@
"express": "= 2.3.11"
, "jsonreq": "= 0.1.0"
, "jade": "= 0.12.1"
+ , "mongodb": "= 0.9.4-5"
}
}
View
@@ -6,6 +6,8 @@
var express = require('express');
var jsonreq = require('jsonreq');
var Facebook = require('./facebook');
+var mongo = require('mongodb');
+var server = new mongo.Server("127.0.0.1", 27017, {});
var app = module.exports = express.createServer();
@@ -28,13 +30,16 @@ app.configure(function(){
});
app.configure('development', function(){
- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+ app.set('db-name', 'btcfb-devel');
});
app.configure('production', function(){
app.use(express.errorHandler());
+ app.set('db-name', 'btcfb-production');
});
+var db = new mongo.Db(app.set('db-name'), server, {});
var checkUser = function(req, res, next){
if (req.session.fbInfo){
@@ -50,14 +55,48 @@ var checkUser = function(req, res, next){
// Routes
app.get('/', checkUser, function(req, res){
+ var locals = {};
+ // Traemos los valores de bitcoincharts.com
jsonreq.get('http://bitcoincharts.com/t/weighted_prices.json', function(err, data) {
- res.render('index', {
- title:'Bitcoin Price on Facebook',
- locals: {
- dolares: data.USD['24h']
- , euros: data.EUR['24h']
- , libras: data.GBP['24h']
- }
+ // Inicializamos los locales a pasar a la vista
+ locals.dolares = {valor: data.USD['24h']};
+ locals.euros = {valor: data.EUR['24h']};
+ locals.libras = {valor: data.GBP['24h']};
+
+ db.open(function(err,client){
+ if (err) throw err;
+ // Iniciamos la guardada de la informacion.
+ db.collection('precios', function(err, precios){
+ var timestamp = new Date();
+ precios.insert({time:timestamp, precios:data}, {safe:true},
+ function(err,objs){if (err) console.warn(err.message)});
+ });
+
+ // Revisamos la tendencia con el nuevo precio
+ db.collection('precios', function (err, precios) {
+ var ultimos = precios.find({}, {sort:{time:-1}});
+ var nuevo = {};
+ ultimos.nextObject(function(err,doc){
+ nuevo = doc;
+ });
+ ultimos.nextObject(function(err,doc){
+ var checkTendencia = function (nuevo, viejo) {
+ var ret;
+ if (nuevo === viejo) ret = '=';
+ if (nuevo > viejo) ret = '';
+ if (nuevo < viejo) ret = '';
+ return ret;
+ }
+ locals.dolares.tendencia = checkTendencia(nuevo.precios.USD['24h'], doc.precios.USD['24h']);
+ locals.euros.tendencia = checkTendencia(nuevo.precios.EUR['24h'], doc.precios.EUR['24h']);
+ locals.libras.tendencia = checkTendencia(nuevo.precios.GBP['24h'], doc.precios.GBP['24h']);
+ res.render('index', {
+ title:'Bitcoin Price on Facebook',
+ locals: locals
+ });
+ });
+ });
+ db.close();
});
});
});
View
@@ -3,12 +3,16 @@
| !{fbLike}
p(class='fbbox', style='position:absolute;top:155px;left:140px;')
img(src='/images/iconBtc.png', width='16px', height:'16px', style='float:left;')
- span(style='margin-left:5px;') Today's price is: USD
- b $#{dolares}
- span(style='margin-left:5px;') EUR
- b €#{euros}
- span(style='margin-left:5px;') GBP
- b £#{libras}
+ span(style='margin-left:5px;') Today's price is:
+ b #{dolares.tendencia}
+ b $#{dolares.valor}USD
+ span(style='margin-left:5px;') |
+ b #{euros.tendencia}
+ b €#{euros.valor}EUR
+ span(style='margin-left:5px;') |
+ b #{libras.tendencia}
+ b £#{libras.valor}GBP
+
span(style='margin-left:10px;')
a(href='javascript:void(0)', class='fblink', id='btn_share_this') Share This
p(class='fbbox', style='position:absolute;top:5px;left:250px;', id='userLine')
@@ -20,9 +24,9 @@
label(style='margin-left:3px') BTC equal to
input(id='calc_dest', size='8', disabled)
select(id='calc_list')
- option(value='#{dolares}') USD
- option(value='#{euros}') EUR
- option(value='#{libras}') GBP
+ option(value='#{dolares.valor}') USD
+ option(value='#{euros.valor}') EUR
+ option(value='#{libras.valor}') GBP
script
(function(){
@@ -55,7 +59,7 @@ script
link: 'http://apps.facebook.com/bitcoin_price/',
picture: 'http://s1.bitelia.com/files/2011/06/Bitcoin-100x100.png',
caption: 'Weighted prices in major markets',
- description: 'Right now the Bitcoin price are USD #{dolares} - #{euros} - £ #{libras}',
+ description: 'Right now the Bitcoin price are USD #{dolares.valor} - #{euros.valor} - £ #{libras.valor}',
message: "I Just found out the today's price of Bitcoin. Thanks to Bitcoin Price of Day"
},
function(response) {

0 comments on commit ab2f5fa

Please sign in to comment.