Skip to content

Commit

Permalink
npi.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasalmeida committed Jan 23, 2015
1 parent a17266a commit 0279c89
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
57 changes: 57 additions & 0 deletions jobs/npi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
console.log("npi.js job")

getNpiInfoJob = function(){
// it all happens here
if(document.getElementById('openHealthJob')){
getNpiInfoJob.buildUI('openHealthJob')
}

}

getNpiInfoJob.buildUI=function(div0){
if(typeof(div0)=='string'){div0=document.getElementById(div0)}
div0.innerHTML='NPI: <input id=inputNpi value="1548485139" style="color:blue"> <button id="inputNpiButton">enter</button> <a href="https://data.medicare.gov/resource/s63f-csi6" target=_blank>source</a><div id="getNpiInfoJobResults" style="color:blue"> ... click or press enter ... </div>';
var bt = document.getElementById('inputNpiButton')
bt.onclick=function(){
openHealth.npi(inputNpi.value,function(x){
var d = document.getElementById("getNpiInfoJobResults")
var n = x.length
if(n>0){
x=openHealth.docs2tab(x)
// sort attributes and remove
var xx={}
Object.getOwnPropertyNames(x).forEach(function(a){
console.log(a)
xx[a]={}; // to make sure it not be an Array
x[a].map(function(ai,i){
xx[a][i]=ai
})
})
x=xx;
d.innerHTML='<span style="color:green">'+n+' entries were found with NPI '+inputNpi.value+':</span>'
d.appendChild(openHealth.crossdoc2html(x,"NPI "+inputNpi.value+".csv",true))
} else {
d.innerHTML='<span style="color:red">no entries were found with NPI '+inputNpi.value+':</span>'
}

})
}



4
}


if(!window.openHealth){
var s = document.createElement('script')
s.src="https://mathbiol.github.io/openHealth/openHealth.js"
s.onload=function(){
getNpiInfoJob()
}
}else{
getNpiInfoJob()
}



30 changes: 26 additions & 4 deletions openHealth.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ this.sodas=function(urls,q,fun,xx){ // version of sodaAll with multiple urls, fo
}
}

this.object2query=function(q){
if(typeof(q)=="object"){
var qq=""
var F = Object.getOwnPropertyNames(q);
F.forEach(function(fi){
qq+=fi+'='+q[fi]+'&';
})
q=qq.slice(0,qq.length-1); // remove the last &
}
return q
}

this.txt2docs=function(txt){
if (txt[0]=='['){ // txt is json
var docs = JSON.parse(txt);
Expand Down Expand Up @@ -450,10 +462,11 @@ this.crossdoc2csv=function(d,title){
return csv
}

this.crossdoc2html=function(d,title){ // create table from cross-document
this.crossdoc2html=function(d,title,sort){ // create table from cross-document
if(!title){title=Date()+'.csv'}
var html = '<table border=1 style="color:navy">';
var rows = Object.getOwnPropertyNames(d);
if(sort){rows.sort()}
var cols = [""].concat(Object.getOwnPropertyNames(d[rows[0]]));
// header
html +='<tr>';
Expand Down Expand Up @@ -487,9 +500,6 @@ this.crossdoc2html=function(d,title){ // create table from cross-document
return div;
}




this.markdown=function(x){ // basic markdown2html
x = x.replace(/\[([^\]]+)\]\(([^\)]+)\)/g,'<a href="$2" target=_blank>$1</a>'); // links
x = x.replace(/\n/g,'<br>'); // new lines
Expand Down Expand Up @@ -543,6 +553,18 @@ this.endJobMsgURL=function(){ // post URL of job into the div.id="msg" if it exi
}
}

this.npi=function(q,fun){ // retrieve data on a National Provider Identifier from CMS
if(!q){q=1548485139} // default example
if(!fun){fun = function(x){console.log(x)}}
var url = 'https://data.medicare.gov/resource/s63f-csi6.json?';
// basic filtering
if(typeof(q)=="number"){q=""+q} // if number convert to string
q=openHealth.object2query(q) // if it is an object
if(q.match(/^[\d]+$/)){q='npi='+q} // if the query is an integer then assume it is the NPI
this.soda(url,q,fun)
return this
}



}
Expand Down

0 comments on commit 0279c89

Please sign in to comment.