[Startup Engineering] HW2 Quiz: SSJS Interactive Tutorial

Setup

First, execute the following commands to download and run the script.

/* 주어진 대로 단순히 실행해 봄. 근데 왜+어떻게+무엇을 한지는 모르겠다-_ -;*/

# Download and make executable
wget https://spark-public.s3.amazonaws.com/startup/code/market-research.js
wget https://spark-public.s3.amazonaws.com/startup/code/market-research-wrapper.js

[sourcecode code="bash"] ubuntu@ip-172-31-5-125:~$ wget https://spark-public.s3.amazonaws.com/startup/code/market-research.js
--2013-07-06 17:25:05-- https://spark-public.s3.amazonaws.com/startup/code/market-research.js
Resolving spark-public.s3.amazonaws.com (spark-public.s3.amazonaws.com)... 176.32.98.226
Connecting to spark-public.s3.amazonaws.com (spark-public.s3.amazonaws.com)|176.32.98.226|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3147 (3.1K) [text/javascript]
Saving to: `market-research.js'
100%[======================================>] 3,147 --.-K/s in 0s
2013-07-06 17:25:06 (665 MB/s) - `market-research.js' saved [3147/3147]
ubuntu@ip-172-31-5-125:~$ wget https://spark-public.s3.amazonaws.com/startup/code/market-research-wrapper.js
--2013-07-06 17:25:10-- https://spark-public.s3.amazonaws.com/startup/code/market-research-wrapper.js
Resolving spark-public.s3.amazonaws.com (spark-public.s3.amazonaws.com)... 176.32.98.226
Connecting to spark-public.s3.amazonaws.com (spark-public.s3.amazonaws.com)|176.32.98.226|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 150 [text/javascript]
Saving to: `market-research-wrapper.js'
100%[======================================>] 150 --.-K/s in 0s
2013-07-06 17:25:11 (33.6 MB/s) - `market-research-wrapper.js' saved [150/150]
[/sourcecode]
# Install npm dependencies. This will create a node_modules directory in
# the current working directory. Don't cd into other directories right
# now; later we'll show how to install modules globally.
npm install restler csv accounting

[sourcecode code="bash"] ubuntu@ip-172-31-5-125:~$ npm install restler csv accounting
The program 'npm' is currently not installed. You can install it by typing:
sudo apt-get install npm
ubuntu@ip-172-31-5-125:~$ sudo apt-get install npm
(...)
ubuntu@ip-172-31-5-125:~$ npm install restler csv accounting
npm http GET https://registry.npmjs.org/restler
npm http GET https://registry.npmjs.org/csv
npm http GET https://registry.npmjs.org/accounting
npm http 200 https://registry.npmjs.org/restler
npm http GET https://registry.npmjs.org/restler/-/restler-2.0.1.tgz
npm http 200 https://registry.npmjs.org/accounting
npm http GET https://registry.npmjs.org/accounting/-/accounting-0.3.2.tgz
npm http 200 https://registry.npmjs.org/csv
npm http GET https://registry.npmjs.org/csv/-/csv-0.3.3.tgz
npm http 200 https://registry.npmjs.org/restler/-/restler-2.0.1.tgz
npm http 200 https://registry.npmjs.org/accounting/-/accounting-0.3.2.tgz
npm http 200 https://registry.npmjs.org/csv/-/csv-0.3.3.tgz
[email protected] ./node_modules/restler
[email protected] ./node_modules/accounting
[email protected] ./node_modules/csv
[/sourcecode]

# As a script
node market-research.js
node market-research.js FB ORCL

[sourcecode code="bash"] ubuntu@ip-172-31-5-125:~$ node market-research.js
Invoked at command line.
Wrote market-research.csv
Symbol Name Market Cap Previous Close Price P/E Ratio Shares EPS Earnings
GOOG Google Inc. 296.4B 886.43 26.52 334374965 33.425 $11,176,483,205.13
FB Facebook, Inc. 58.924B 24.52 533.04 2403099511 0.046 $110,542,577.51
AAPL Apple Inc. 391.8B 420.80 10.04 931083650 41.912 $39,023,577,938.80
YHOO Yahoo! Inc. 27.802B 25.59 7.43 1086440016 3.444 $3,741,699,415.10
MSFT Microsoft Corpora 285.7B 34.01 17.55 8400470450 1.938 $16,280,111,732.10
LNKD LinkedIn Corporat 21.002B 188.18 729.38 111605909 0.258 $28,794,324.52
CRM Salesforce.com In 22.975B 38.55 N/A 595979248 NaN $0.00
ubuntu@ip-172-31-5-125:~$ node market-research.js FB ORCL
Invoked at command line.
Wrote market-research.csv
Symbol Name Market Cap Previous Close Price P/E Ratio Shares EPS Earnings
FB Facebook, Inc. 58.924B 24.52 533.04 2403099511 0.046 $110,542,577.51
ORCL Oracle Corporatio 144.4B 30.70 13.58 4703583062 2.261 $10,634,801,303.18
[/sourcecode]

# As an executable
chmod 777 market-research.js
./market-research.js
./market-research.js GOOG CRM

[sourcecode code="bash"] ubuntu@ip-172-31-5-125:~$ chmod 777 market-research.js
ubuntu@ip-172-31-5-125:~$ ./market-research.js
Invoked at command line.
Wrote market-research.csv
Symbol Name Market Cap Previous Close Price P/E Ratio Shares EPS Earnings
GOOG Google Inc. 296.4B 886.43 26.52 334374965 33.425 $11,176,483,205.13
FB Facebook, Inc. 58.924B 24.52 533.04 2403099511 0.046 $110,542,577.51
AAPL Apple Inc. 391.8B 420.80 10.04 931083650 41.912 $39,023,577,938.80
YHOO Yahoo! Inc. 27.802B 25.59 7.43 1086440016 3.444 $3,741,699,415.10
MSFT Microsoft Corpora 285.7B 34.01 17.55 8400470450 1.938 $16,280,111,732.10
LNKD LinkedIn Corporat 21.002B 188.18 729.38 111605909 0.258 $28,794,324.52
CRM Salesforce.com In 22.975B 38.55 N/A 595979248 NaN $0.00
ubuntu@ip-172-31-5-125:~$ ./market-research.js GOOG CRM
Invoked at command line.
Wrote market-research.csv
Symbol Name Market Cap Previous Close Price P/E Ratio Shares EPS Earnings
GOOG Google Inc. 296.4B 886.43 26.52 334374965 33.425 $11,176,483,205.13
CRM Salesforce.com In 22.975B 38.55 N/A 595979248 NaN $0.00
[/sourcecode]

# As a module, through another program invoked as a script
node market-research-wrapper.js

[sourcecode code="bash"]
ubuntu@ip-172-31-5-125:~$ node market-research-wrapper.js
Invoked via library call
Wrote market-research.csv
Symbol Name Market Cap Previous Close Price P/E Ratio Shares EPS Earnings
FB Facebook, Inc. 58.924B 24.52 533.04 2403099511 0.046 $110,542,577.51
ORCL Oracle Corporatio 144.4B 30.70 13.58 4703583062 2.261 $10,634,801,303.18</pre>
[/sourcecode]

# As a module, through another program being invoked as an executable
chmod 777 market-research-wrapper.js
./market-research-wrapper.js

[sourcecode code="bash"] ubuntu@ip-172-31-5-125:~$ chmod 777 market-research-wrapper.js
ubuntu@ip-172-31-5-125:~$ ./market-research-wrapper.js
Invoked via library call
Wrote market-research.csv
Symbol Name Market Cap Previous Close Price P/E Ratio Shares EPS Earnings
FB Facebook, Inc. 58.924B 24.52 533.04 2403099511 0.046 $110,542,577.51
ORCL Oracle Corporatio 144.4B 30.70 13.58 4703583062 2.261 $10,634,801,303.18</pre>
[/sourcecode]

# Also as a module - but with the external code being input at the command line
# via the -e flag
node -e "require('./market-research.js')"
node -e "var mr = require('./market-research.js'); mr.marketResearch();"
node -e "var mr = require('./market-research.js'); mr.marketResearch(["FB","ORCL"]);"

[sourcecode code="bash"] ubuntu@ip-172-31-5-125:~$ node -e "require('./market-research.js')"
Invoked via library call
ubuntu@ip-172-31-5-125:~$ node -e "var mr = require('./market-research.js'); mr.marketResearch();"
Invoked via library call
Wrote market-research.csv
Symbol Name Market Cap Previous Close Price P/E Ratio Shares EPS Earnings
GOOG Google Inc. 296.4B 886.43 26.52 334374965 33.425 $11,176,483,205.13
FB Facebook, Inc. 58.924B 24.52 533.04 2403099511 0.046 $110,542,577.51
AAPL Apple Inc. 391.8B 420.80 10.04 931083650 41.912 $39,023,577,938.80
YHOO Yahoo! Inc. 27.802B 25.59 7.43 1086440016 3.444 $3,741,699,415.10
MSFT Microsoft Corpora 285.7B 34.01 17.55 8400470450 1.938 $16,280,111,732.10
LNKD LinkedIn Corporat 21.002B 188.18 729.38 111605909 0.258 $28,794,324.52
CRM Salesforce.com In 22.975B 38.55 N/A 595979248 NaN $0.00
ubuntu@ip-172-31-5-125:~$ node -e "var mr = require('./market-research.js'); mr.marketResearch(["FB","ORCL"]);"
Invoked via library call
Wrote market-research.csv
Symbol Name Market Cap Previous Close Price P/E Ratio Shares EPS Earnings
FB Facebook, Inc. 58.924B 24.52 533.04 2403099511 0.046 $110,542,577.51
ORCL Oracle Corporatio 144.4B 30.70 13.58 4703583062 2.261 $10,634,801,303.18
[/sourcecode]

market-research.js

[sourcecode language="javascript"]
#!/usr/bin/env node
/*
Use the Yahoo Finance CSV API to do some basic market research calculations.

- Background: http://greenido.wordpress.com/2009/12/22/yahoo-finance-hidden-api/
- Example URL: http://finance.yahoo.com/d/quotes.csv?s=GOOG+FB+AAPL&amp;f=snj1pr
s: Symbol
n: Name
j1: Market Capitalization (in billions)
p: Price-per-share (at previous close)
r: Price to Earnings Ratio

Further references.

- https://github.com/danwrong/restler
- https://github.com/wdavidw/node-csv
- http://josscrowcroft.github.io/accounting.js
- http://stackoverflow.com/questions/4981891/node-js-equivalent-of-pythons-if-name-main
- http://nodejs.org/docs/latest/api/util.html#util_util_format_format

*/

var util = require('util');
var fs = require('fs');
var rest = require('restler');
var csv = require('csv');
var accounting = require('accounting');
var CSVFILE_DEFAULT = "market-research.csv";
var SYMBOLS_DEFAULT = ["GOOG", "FB", "AAPL", "YHOO", "MSFT", "LNKD", "CRM"];
var COLUMNS_DEFAULT = 'snj1pr'; // http://greenido.wordpress.com/2009/12/22/yahoo-finance-hidden-api
var HEADERS_DEFAULT = ["Symbol", "Name", "Market Cap", "Previous Close Price",
"P/E Ratio", "Shares", "EPS", "Earnings"];

var financeurl = function(symbols, columns) {
return util.format(
'http://finance.yahoo.com/d/quotes.csv?s=%s&amp;f=%s',
symbols.join('+'),
columns);
};

var marketCapFloat = function(marketCapString) {
return parseFloat(marketCapString.split('B')[0]) * 1e9;
};

var csv2console = function(csvfile, headers) {
console.log(headers.join("t"));
csv()
.from.path(csvfile)
.on('record', function(row, index) {
var shares = Math.round(marketCapFloat(row[2])/row[3], 0);
var eps = (row[3]/row[4]).toFixed(3);
var earnings = accounting.formatMoney(eps * shares);
outrow = row.concat([shares, eps, earnings]);
console.log(outrow.join("t"));
});
};

var buildfn = function(csvfile, headers) {
var response2console = function(result, response) {
if (result instanceof Error) {
console.error('Error: ' + util.format(response.message));
} else {
console.error("Wrote %s", csvfile);
fs.writeFileSync(csvfile, result);
csv2console(csvfile, headers);
}
};
return response2console;
};

var marketResearch = function(symbols, columns, csvfile, headers) {
symbols = symbols || SYMBOLS_DEFAULT;
columns = columns || COLUMNS_DEFAULT;
csvfile = csvfile || CSVFILE_DEFAULT;
headers = headers || HEADERS_DEFAULT;
var apiurl = financeurl(symbols, columns);
var response2console = buildfn(csvfile, headers);
rest.get(apiurl).on('complete', response2console);
};

if(require.main == module) {
console.error('Invoked at command line.');
var symbols = process.argv;
if(symbols.length &gt; 2) {
symbols = symbols.slice(2, symbols.length);
} else {
symbols = undefined;
}
marketResearch(symbols);
} else {
console.error('Invoked via library call');
}

exports.marketResearch = marketResearch;

[/sourcecode]

And here is a wrapper that shows how to invoke this as a module. We're able to do so because of the last line in market-research.js (namelyexports.marketResearch = marketResearch;):

market-research-wrapper.js

[sourcecode language="javascript"]
#!/usr/bin/env node
// Example of using market-research.js as a module
var mr = require('./market-research.js');
mr.marketResearch(["FB", "ORCL"]);[/sourcecode]

The best way to understand what the code does is to download it, edit it, change the arguments around, and so on. The quiz questions will walk you through the properties of individual functions.

banner1

적게 일하고 많이 버는 법을 늘 고민합니다. 일이 되게 하는 것에 간혹 목숨을 겁니다. 지금은 우아한형제들과 함께 일하고 있어요.