I'm attempting to understand the code in conjunction with http://nodejs.org/api/util.html and here's what I understand with regards the code shown below
var util = require('util');
Includes the util module.
Which has some effect in the code shown below,
var financeurl = function(symbols, columns) {
return util.format(
'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s',
symbols.join('+'),
columns);
};
The second line where it says, and the three lines that follow it
return util.format
The node documentation says,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
util.format(format, [...])#
Returns a formatted string using the first argument as a printf-like format.
The first argument is a string that contains zero or more placeholders. Each placeholder is replaced with the converted value from its corresponding argument. Supported placeholders are:
%s - String.
%d - Number (both integer and float).
%j - JSON.
% - single percent sign ('%'). This does not consume an argument.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s',
is the first argument which is to be returned as a formatted string. It contains zero placeholders. Am I correct?
I'm somewhat lost with the next part, do 'symbols' and 'columns' come from node? Should I search elsewhere in the script for them? What I suspect they do is take the symbols of specific stocks and join, concatenate them to different columns.
Is my interpretation of the code with reference to the docs in someway correct?
Node's util.format() is a Variadic function - it takes a variable number of arguments.
The documentation specifies that the first parameter is a string template containing zero or more special placeholders. The remainder of the arguments are treated basically as an array and placeholders in the string template are filled in with these values and the resulting string is returned.
The string in the example - http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s contains two placeholders - both %s near the end of the string. In the code given, the first placeholder is replaced with the value of symbols.join('+') and the second with the value of columns.
(join() is a built in JavaScript method on Array that produces a String consisting of the toString() value of each element of the Array separated by default by a comma, or the provided String argument('+' in this case).)
symbols and columns do not come from Node - they are variables provided elsewhere in the program. You are correct in guessing that symbols contains the stock ticker symbols you're going to query the Yahoo Finance service with, but columns actually specifies the data columns that you want back from the service. (The values you can provide for columns and what they mean are described at the top of the program in a comment. As for how I know this... I'm taking the Startup Engineering course too... :D )
You're basically building up a URL for a request against the Yahoo Finance REST service - you can play around with it yourself and see what the program's getting from it by plugging in values in that string template -
http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=snj1pr - gives you a CSV of containing a bunch of different values for GOOG - Google's stock symbol.
http://finance.yahoo.com/d/quotes.csv?s=GOOG+AAPL+MSFT&f=sp - gives you a CSV of just the stock ticker symbol and the price-per-share of Google, Apple and Microsoft.