-
-
Save gpittau/3982154 to your computer and use it in GitHub Desktop.
Experimentando una API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Andando | |
module("sru/fixtures/store2" | |
, { | |
setup: | |
function() | |
{ | |
var line_separator | |
= '\n' | |
, field_separator | |
= ',' | |
, unquote | |
= function(item) | |
{ | |
return item.replace(/^"|"$/g,"") | |
} | |
, toCsv | |
= function(raw_text) | |
{ | |
var lines | |
= raw_text | |
.split(line_separator) | |
, headers | |
= lines[0] | |
//.shift() | |
.split(field_separator) | |
.map(unquote) | |
return lines | |
.map( | |
function(line) | |
{ | |
var result ={} | |
, fields | |
return line | |
.split(field_separator) | |
.map(unquote) | |
} | |
) | |
} | |
, toJson | |
= function(csv_data) | |
{ | |
var lines | |
= csv_data | |
, headers | |
= lines | |
.shift() | |
return lines | |
.map( | |
function(line) | |
{ | |
var result ={} | |
, fields | |
= line | |
.map(unquote) | |
can | |
.each( | |
headers | |
, function(header,index) | |
{ | |
result[header]=fields[index] | |
} | |
) | |
return result | |
} | |
) | |
} | |
can.ajaxPrefilter( | |
function(options) | |
{ | |
return (/\.csv/.test(options.url)) | |
?'csv-json' | |
:options.dataType | |
} | |
) | |
can.ajaxSetup( | |
{ | |
accepts: | |
{ | |
'csv': "text/csv" | |
, 'csv-json': "text/csv" | |
} | |
, contents: | |
{ | |
'csv': /csv/ | |
, 'csv-json': /csv-json/ | |
} | |
, converters: | |
{ | |
"text csv":toCsv | |
, "csv csv-json":toJson | |
} | |
} | |
) | |
function fix_pipe(data) | |
{ | |
return data | |
} | |
can.Construct( | |
'SRU.fixtures.store2' | |
, { | |
union: | |
function() | |
{ | |
var args | |
= arguments | |
return can.when | |
.apply( | |
null | |
, can.map( | |
args | |
, this._get_one | |
) | |
).pipe( | |
function() | |
{ | |
return Array | |
.prototype | |
.concat | |
.apply([],arguments) | |
} | |
) | |
} | |
, join: | |
function(left_dfd,right_url,pred) | |
{ | |
var self | |
= this | |
left_dfd | |
.pipe( | |
function(left) | |
{ | |
var right_dfd | |
= self.get(right_url) | |
.pipe( | |
function(right) | |
{ | |
left | |
.map( | |
function(lobj) | |
{ | |
var prop='joined' | |
lobj[prop] | |
= can | |
.grep( | |
right | |
, function(rval) | |
{ | |
return pred(lobj,rval) | |
} | |
) | |
} | |
) | |
} | |
) | |
} | |
) | |
return left_dfd | |
} | |
, get: | |
function(url) | |
{ | |
return this._get_one(url) | |
} | |
, _get_one: | |
function(what) | |
{ | |
return can | |
.ajax(what) | |
//.pipe(toJson) | |
} | |
} | |
, { | |
} | |
) | |
} | |
} | |
) | |
test("csv2json ajax convert" | |
, function() | |
{ | |
stop(); | |
var store= can.ajax( | |
"assets/instituciones_ot.csv" | |
) | |
.then( | |
function(items) | |
{ | |
ok(items,'OK') | |
start() | |
return items | |
} | |
) | |
} | |
) | |
test("Store2-Deferreds-get" | |
, function() | |
{ | |
stop(); | |
var store | |
= SRU.fixtures | |
.store2 | |
.get( | |
"assets/instituciones_ot.csv" | |
).then( | |
function(items) | |
{ | |
ok(items,'OK') | |
start() | |
return items | |
} | |
) | |
} | |
) | |
test("Store2-Deferreds-union" | |
, function() | |
{ | |
stop(); | |
var store | |
= SRU.fixtures | |
.store2 | |
.union( | |
"assets/instituciones_ot.csv" | |
, "assets/instituciones_univ.csv" | |
).then( | |
function(items) | |
{ | |
ok(items,'OK') | |
start() | |
return items | |
} | |
) | |
} | |
) | |
test("Store2-Deferreds-join" | |
, function() | |
{ | |
stop(); | |
var join | |
= SRU.fixtures | |
.store2 | |
.join( | |
SRU.fixtures | |
.store2 | |
.get( | |
"assets/provincias.csv" | |
) | |
, "assets/instituciones_univ.csv" | |
, function(provincia,institucion) | |
{ | |
return institucion.PROVINCIA==provincia.ID_PROVINCIA | |
} | |
).pipe( | |
function(joined_data) | |
{ | |
return can | |
.map( | |
joined_data | |
, function(item) | |
{ | |
item.instituciones=item.joined | |
delete item.joined | |
return item | |
} | |
) | |
} | |
).then( | |
function(items) | |
{ | |
ok(items[0].joined==undefined,'OK') | |
ok(items[0].instituciones,'OK') | |
start() | |
} | |
) | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment