Skip to content

Commit

Permalink
Add price providers
Browse files Browse the repository at this point in the history
  • Loading branch information
srabuini committed Jan 4, 2015
1 parent 0aab8e5 commit b55df5d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 23 deletions.
4 changes: 2 additions & 2 deletions appinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"configurable"
],
"companyName": "WasabitLabs",
"longName": "Bitstamp BTC Price",
"longName": "BTC Price from different providers",
"projectType": "native",
"resources": {
"media": [
Expand All @@ -22,7 +22,7 @@
"shortName": "Btc Simple",
"uuid": "cea47b89-6378-47ca-b9f4-c3bb9de3286e",
"versionCode": 3,
"versionLabel": "1.2",
"versionLabel": "1.3",
"watchapp": {
"watchface": false
}
Expand Down
101 changes: 80 additions & 21 deletions src/js/pebble-js-app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var DEFAULT_CONFIGURATION = {"provider": "bitstamp"};

function fetchData() {
var response;
var req = new XMLHttpRequest();

var provider = new Bitstamp();
var provider = getProvider();
console.log(provider.url);
req.open('GET', provider.url);

Expand Down Expand Up @@ -35,44 +37,101 @@ function fetchData() {
req.send(null);
}

function BtcE(){
function BtcE() {
this.url = "https://btc-e.com/api/3/ticker/btc_usd";
this.name = "BTC-e";

this.data = function(response){
this.data = function(response) {
return {price: response.btc_usd.last, timestamp: response.btc_usd.updated};
};
}

function Bitstamp(){
function Bitstamp() {
this.url = "https://www.bitstamp.net/api/ticker/";
this.name = "Bitstamp";

this.data = function(response) {
return {price: response.last, timestamp: response.timestamp};
};
}

function Coindesk() {
this.url = "http://api.coindesk.com/v1/bpi/currentprice.json";
this.name = "Coindesk";
this.data = function(response) {
return {price: response.bpi.USD.rate_float, timestamp: Date.parse(response.time.updatedISO) / 1000};
};
}

function Bitfinex() {
this.url = "https://api.bitfinex.com/v1/pubticker/btcusd";
this.name = "Bitfinex";

this.data = function(response) {
return {price: response.last_price, timestamp: response.timestamp};
};
}

Pebble.addEventListener("ready",
function(e) {
console.log("connect! " + e.ready);
fetchData();
console.log(e.type);
});

Pebble.addEventListener("appmessage",
function(e) {
fetchData();
console.log(e.type);
console.log(e.payload);
console.log("message!");
});
function getConfiguration() {
var configuration = localStorage.getItem("configuration");
if (configuration) {
try {
return JSON.parse(configuration);
} catch(e) {
console.log("WARN: error parsing configuration from localstorage: " + e);
return DEFAULT_CONFIGURATION;
}
} else {
return DEFAULT_CONFIGURATION;
}
}

function getProvider() {
var configuration = getConfiguration();
var provider = configuration.provider;
var objProvider;

console.log("Getting provider...");
console.log(getConfiguration());
console.log(provider);

switch (provider) {
case "bitstamp":
objProvider = new Bitstamp();
break;
case "btc_e":
objProvider = new BtcE();
break;
case "coindesk":
objProvider = new Coindesk();
break;
case "bitfinex":
objProvider = new Bitfinex();
break;
default:
objProvider = new Bitstamp();
}

return objProvider;
}

Pebble.addEventListener("ready", function(e) {
console.log("connect! " + e.ready);
fetchData();
console.log(e.type);
});

Pebble.addEventListener("appmessage", function(e) {
fetchData();
console.log(e.type);
console.log(e.payload);
console.log("message!");
});

Pebble.addEventListener("showConfiguration", function(e) {
Pebble.openURL("http://wasabitlabs.com/en/btc-simple");
});

Pebble.addEventListener("webviewclosed", function(e) {
var configuration = JSON.parse(decodeURIComponent(e.response));
localStorage.setItem("configuration", JSON.stringify(configuration));
console.log("Configuration window returned: " + JSON.stringify(configuration));
fetchData();
});

0 comments on commit b55df5d

Please sign in to comment.