Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed business app #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 32 additions & 31 deletions cloud/mortgage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,41 @@
/**
* Use this Web service to figure out your monthly mortgage payment.
*/
var request=require("request");
var mortgage = {
//SOAP API URL
SOAPUrl : "http://www.webservicex.net/mortgage.asmx",
/**
* Calc mortgaeg based on user input.
* Tutorial: How to wrap SOAP message and unwrap SOAP response.
*/
getMortgage : function(years, interest, loanAmount, tax, insurance, callback) {
/**
* Since SOAP calls are wrapped HTTP calls, in Javascript we have to wrap SOAP envelope manually or using a SOAP library in Javascript
*/
var xmlContent = '<?xml version="1.0" encoding="utf-8"?>' + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + ' <soap:Body>' + ' <GetMortgagePayment xmlns="http://www.webserviceX.NET/">' + ' <Years>' + years + '</Years>' + ' <Interest>' + interest + '</Interest>' + ' <LoanAmount>' + loanAmount + '</LoanAmount>' + ' <AnnualTax>' + tax + '</AnnualTax>' + ' <AnnualInsurance>' + insurance + '</AnnualInsurance>' + ' </GetMortgagePayment>' + ' </soap:Body>' + '</soap:Envelope>'
//SOAP API URL
SOAPUrl : "http://www.webservicex.net/mortgage.asmx",
/**
* Calc mortgaeg based on user input.
* Tutorial: How to wrap SOAP message and unwrap SOAP response.
*/
getMortgage : function(years, interest, loanAmount, tax, insurance, callback) {
/**
* Since SOAP calls are wrapped HTTP calls, in Javascript we have to wrap SOAP envelope manually or using a SOAP library in Javascript
*/
var xmlContent = '<?xml version="1.0" encoding="utf-8"?>' + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + ' <soap:Body>' + ' <GetMortgagePayment xmlns="http://www.webserviceX.NET/">' + ' <Years>' + years + '</Years>' + ' <Interest>' + interest + '</Interest>' + ' <LoanAmount>' + loanAmount + '</LoanAmount>' + ' <AnnualTax>' + tax + '</AnnualTax>' + ' <AnnualInsurance>' + insurance + '</AnnualInsurance>' + ' </GetMortgagePayment>' + ' </soap:Body>' + '</soap:Envelope>'

var url = mortgage.SOAPUrl;
//Webcall paramters.
var opt = {
url : url,
method : "POST",
charset : 'UTF-8',
contentType : 'text/xml',
body : xmlContent,
period : 3600
};
var url = mortgage.SOAPUrl;
//Webcall paramters.
var opt = {
url : url,
method : "POST",
body : xmlContent,
headers:{
"Content-Type":"text/xml"
}
};

//Feedhenry Web Call
$fh.web(opt, function(err, res) {
var xml2js = require("xml2js");
(new xml2js.Parser()).parseString(res.body, function(err, jsres) {
var mortgageRes=jsres["soap:Body"].GetMortgagePaymentResponse.GetMortgagePaymentResult;
callback(err, mortgageRes);
});
return;
});
}
//Feedhenry Web Call
request(opt, function(err, res,body) {
var xml2js = require("xml2js");
(new xml2js.Parser()).parseString(body, function(err, jsres) {
var mortgageRes=jsres['soap:Envelope']["soap:Body"][0].GetMortgagePaymentResponse[0].GetMortgagePaymentResult[0];
callback(err, mortgageRes);
});
return;
});
}
};

exports.getMortgage = mortgage.getMortgage;
4 changes: 2 additions & 2 deletions cloud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "business-app",
"version": "0.1.0",
"dependencies" : {
"libxmljs" : "*",
"xml2js":"*",
"request":"*"
"request":"*",
"soap":"*"
}
}
158 changes: 83 additions & 75 deletions cloud/stock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,89 @@
* Stock Info lookup: Using WebServiceX API . SOAP
*
*/
var util = require("./util");

var stock = {
//YAHOO finance api for looking up stock symbol with a company name. It is a JSONP service.
yahooApi : "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query={0}&callback=YAHOO.Finance.SymbolSuggest.ssCallback",
//WebServiceX API (Open API). It returns stock details with specific stock symbol.
webServiceXApi : "http://www.webservicex.net/stockquote.asmx",
/**
* The function will look for stock symbol based on "name" param, and return stock info from WebServiceX
*
* Return stock information.
*/
getStockInfo : function(name, callback) {
//Compose request url using user input.
var yahooApiUrl = stock.yahooApi.replace("{0}", name);
/*
* Perform Webcall
* Raw response from YAHOO JSONP api which contains stock symbol as well as other information we do not want.
*
*/
$fh.web({
url : yahooApiUrl,
method : "GET",
charset : "UTF-8",
period : 3600
}, function(err, symbolRes) {
//Clear up YAHOO response and only keep the information "stock symbol" we need.
var stockSymbol = stock.processSymbolRes(symbolRes);

// construct SOAP envelop. We could do this manually or just use a Javascript Library.
var soapEnvolope = '<?xml version="1.0" encoding="utf-8"?>' + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' + '<soap:Body>' + '<GetQuote xmlns="http://www.webserviceX.NET/">' + '<symbol>' + stockSymbol + '</symbol>' + '</GetQuote>' + '</soap:Body>' + '</soap:Envelope>';

//Retrieve SOAP url
var stockInfoUrl = stock.webServiceXApi;

//Prepare webcall parameters
var opt = {
url : stockInfoUrl,
method : "POST",
charset : "UTF-8",
contentType : "text/xml",
body : soapEnvolope,
period : 3600
}

//Perform webcall
$fh.web(opt, function(err, res) {
var xml2js=require ("xml2js");
//getSOAPElement will retrieve specific XML object within SOAP response
(new xml2js.Parser()).parseString(res.body, function(err, jsres) {
var quoteRes=jsres["soap:Body"]["GetQuoteResponse"]["GetQuoteResult"];
//mash up the data and return to client.
callback(err, {
stockSymbol : stockSymbol,
stockInfo : quoteRes
});
});
});
});
},
/**
* Process Response from YAHOO stock symbol api.
* It will clear up the response and only return stock symbol as string.
*/
processSymbolRes : function(res) {
var resBody = res.body;
var removedHeadRes = resBody.replace("YAHOO.Finance.SymbolSuggest.ssCallback(", "");
//remove jsonp callback header
var removedTailRes = removedHeadRes.substr(0, removedHeadRes.length - 1);
//remove jsonp callback bracket
var resObj = $fh.parse(removedTailRes);
//parse result to JSON object
return resObj.ResultSet.Result[0].symbol;
//return the first matched stock symbol
}
var util = require("./util");
var request=require("request");
var xml2js=require("xml2js");
var stock = {
//YAHOO finance api for looking up stock symbol with a company name. It is a JSONP service.
yahooApi : "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query={0}&callback=YAHOO.Finance.SymbolSuggest.ssCallback",
//WebServiceX API (Open API). It returns stock details with specific stock symbol.
webServiceXApi : "http://www.webservicex.net/stockquote.asmx",
/**
* The function will look for stock symbol based on "name" param, and return stock info from WebServiceX
*
* Return stock information.
*/
getStockInfo : function(name, callback) {
//Compose request url using user input.
var yahooApiUrl = stock.yahooApi.replace("{0}", name);
/*
* Perform Webcall
* Raw response from YAHOO JSONP api which contains stock symbol as well as other information we do not want.
*
*/
request(yahooApiUrl,function(err,response,body){
//Clear up YAHOO response and only keep the information "stock symbol" we need.
var stockSymbol = stock.processSymbolRes(body);
var soapBody=stock.getSOAPBody(stockSymbol);
var requestArgs={
"url":stock.webServiceXApi,
"method":"POST",
"body":soapBody,
"Content-type":"text/xml; charset=utf-8",
"jar":false,
"headers":{
"Content-Length":soapBody.length,
"Content-Type":"text/xml; charset=utf-8"
}
};
request(requestArgs,function(err,res,body){
if (err){
console.log(err);
callback(err,null);
}else{
stock.parseStockSoapBody(body,function(err,parseRes){
var quoteRes=parseRes['soap:Envelope']["soap:Body"][0]["GetQuoteResponse"][0]["GetQuoteResult"][0];
//mash up the data and return to client.
var rtn= {
stockSymbol : stockSymbol,
stockInfo : quoteRes
}
callback(err,rtn);
});
}
});
});
},
/**
* Process Response from YAHOO stock symbol api.
* It will clear up the response and only return stock symbol as string.
*/
processSymbolRes:function(res) {
var resBody = res;
var removedHeadRes = resBody.replace("YAHOO.Finance.SymbolSuggest.ssCallback(", "");
//remove jsonp callback header
var removedTailRes = removedHeadRes.substr(0, removedHeadRes.length - 1);
//remove jsonp callback bracket
var resObj = JSON.parse(removedTailRes);
//parse result to JSON object
return resObj.ResultSet.Result[0].symbol;
//return the first matched stock symbol
},
parseStockSoapBody:function(stockSoapRes,callback){
var xmlParser=new xml2js.Parser();
xmlParser.parseString(stockSoapRes,callback);
},
getSOAPBody:function(symbol){
return '<?xml version="1.0" encoding="utf-8"?>'+
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
'<soap:Body>'+
'<GetQuote xmlns="http://www.webserviceX.NET/">'+
'<symbol>'+symbol+'</symbol>'+
'</GetQuote>'+
'</soap:Body>'+
'</soap:Envelope>';
}
}

exports.getStockInfo = stock.getStockInfo;