Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
upgrade api mock to v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
weilu committed Feb 5, 2014
1 parent 7ba1db6 commit c32c736
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions assets/javascripts/hiveapp-api-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Licensed under the MIT License.
*
* v1.0.0
* v1.0.1
*/

var bitcoin = bitcoin || mockBitcoin()
Expand All @@ -25,6 +25,50 @@ function mockBitcoin() {
var locale = navigator.language;

function async(fn) { setTimeout(fn, 0) }
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
throw new Error('CORS not supported');
}
return xhr;
}

function ajax(url, options) {
options = options || {}
options.type = options.type || "GET"
options.dataType = options.dataType || "json"
options.success = options.success || function(){}
options.failure = options.failure || function(){}
options.complete = options.complete || function(){}

var xhr = createCORSRequest(options.type, url)
xhr.responseType = options.dataType

xhr.onload = function(event) {
options.success(xhr.response, xhr.status)
};

xhr.onerror = function(event) {
options.error(xhr.response, xhr.status, xhr.statusText)
};

xhr.onloadend = function(event) {
options.complete(xhr.response, xhr.status, xhr.statusText)
};

var data = new FormData();
if(options.data) {
for(var key in options.data) {
data.append(key, options.data[key])
}
}
xhr.send(data);
}

return {
BTC_IN_SATOSHI: _BTC_IN_SATOSHI,
Expand Down Expand Up @@ -115,8 +159,8 @@ function mockBitcoin() {
},

makeRequest: function(endpoint, args){
args['url'] = endpoint.replace(/http[s]?:\/\//gi, "http://www.corsproxy.com/");
$.ajax(args)
var url = endpoint.replace(/http[s]?:\/\//gi, "http://www.corsproxy.com/");
ajax(url, args)
},

userStringForSatoshi: function(satoshiAmount) {
Expand All @@ -142,3 +186,4 @@ function mockBitcoin() {
};
}


0 comments on commit c32c736

Please sign in to comment.