yahoo weather api

yahoo的weather api,可透過rss的方式,或使用YQL的方式取得。

以下是使用rss方式:
/**
* w參數,為woeid(每個城市都有一個獨一無二的值)
* p參數為zipCode
* 以上兩個參數使用其中一個就可以了。
*
* u參數為溫度的單位
*/
//xml
http://weather.yahooapis.com/forecastrss
//json
http://weather.yahooapis.com/forecastjson
//exampe:
http://weather.yahooapis.com/forecastrss?w=2442047&u=c
http://weather.yahooapis.com/forecastrss?p=USCA1116

取得woeid和zipCode的頁面http://weather.yahoo.com/

1.輸入城市名稱:

2.取得woeid

3.按此link

4.取得zip code

YQL:

Yql的console page

#可以查詢table的欄位
desc weather.search

紅色為欄位名稱

取得城市zip code

使用剛剛的zip code做收尋

使用http get的網址

/**
* 以下是url的參數
* q:sql語法
* callback:javascript的function名稱
* format:傳回的格式
*/
http://query.yahooapis.com/v1/public/yql

yql可以使用jsonp,也就是javascript callback。至於其他的table查詢,可參考yahoo的文件。

node.js multi-process

node.js本身並不支援multi-process,但是有其他方式可以達成。簡單來說,就是利用node.js的不會阻塞IO的特性,然後去執行終端機(terminal)的command line,然建立監聽事件,就可以達成multi-process。以有人實作出來,使用方法如同browserWorker,可以參考node.js的Worker

以下是一個簡單實作原理的範例:

main.js

var spawn = require('child_process').spawn,
    child_process = spawn('node', ['child.js']);

    child_process.stdout.on('data', function (data) {
      console.log('stdout: ' + data);
    }); 

    setInterval(function(){
      child_process.stdin.write("test");
    },199);

child.js

//當程式執行完畢,不關閉此程序
process.stdin.resume();
process.stdin.setEncoding('utf8');

process.stdin.on('data', function (chunk) {
    console.log('data: ' + chunk);
});

如果想要理解更多,請參考processchild_process的相關event,或者其他人實作的source code

Qunit

Qunitjavascriptunit test之一,這套工具由jquery team撰寫的,使用的方式比起Jsunit,更有javascript風格。

首先要先配置html:
<html>
<head>
    <title>QUnit Test Suite</title>
    <link rel="stylesheet" href="../qunit/qunit.css" type="text/css" media="screen">
    <script type="text/javascript" src="../qunit/qunit.js"></script>
</head>
<body>
    <h1 id="qunit-header">QUnit Test Suite</h1>
    <h2 id="qunit-banner"></h2>
    <div id="qunit-testrunner-toolbar"></div>
    <h2 id="qunit-userAgent"></h2>
    <ol id="qunit-tests"></ol>
    <div id="qunit-fixture">test markup</div>
</body>
</html>

在html配置部分,id名稱必須依照官方的命名,簡單來說只需要將測試的程式,加入到head裡面即可

Qunit所提供的幾個function:
/**
* @param {String} name 單元測試的名稱
* @param {Number} expected 要比對的數量(可選)
* @param {function} test 要測試的function
* asyncTest需要在測試完後,多呼叫一個start()
* 否則將不會向下測試
*/
test(name,expected,test)
asyncTest(name,expected,test)

/**
* @param {Number} amount 要比對的數量
*/
expect(amount)

/**
* @param {String} name module的名稱
* @param {Object} lifecycle 用來配置初始化和結束執行的function(可選)
* 分別為setup和teardown
*/
module(name,lifecycle)

/**
* @param {boolean} state
* @param {String} message
*/
ok(state,message)

/**
* @param {*} actual 比對值
* @param {*} expected 期望值
* @param {String} message (可選)
* equal:actual == expected
* deepEqual:actual === expected(深度比對,會一層一層比對)
* strictEqual:actual === expected
*/
equal(actual,expected,message)
notEqual(actual,expected,message)
deepEqual(actual,expected,message)
notDeepEqual(actual,expected,message)
strictEqual(actual,expected,message)
notStrictEqual(actual,expected,message)

/**
* 繼續往下執行測試
*/
start()
/**
* @param {Number} timeout 延遲向下執行的時間(可選)
* 若無輸入參數會等待呼叫start才會向下執行測試
*/
stop(timeout)
幾個簡單範例:
/*
* 一個簡單範例
*/
test("module without setup/teardown (default)", function() {
    expect(1);
    ok(true);
});
/*
* setup將會先執行,而teardown會最後執行
*/
var state;
module("setup/teardown test", {
    setup: function() {
        state = true;
        ok(true);
    },
    teardown: function() {
        ok(true);
    }
});

test("module with setup/teardown", function() {
    expect(3);
    ok(true);
});

/**
* stop和start運用
*/
module("asyncTest");

// stop呼叫2次,start也必須呼叫兩次
// 才會向下執行下個test

test("sync", 2, function() {
    stop();
    setTimeout(function() {
        ok(true);
        start();
    }, 13);
    stop();
    setTimeout(function() {
        ok(true);
        start();
    }, 125);
});

// 必須呼叫start才會執行下個test

asyncTest("asyncTest", 2, function() {
    ok(true);
    setTimeout(function() {
        state = "done";
        ok(true);
        start();
    }, 13);
});