UNPKG

fluentnode

Version:

Fluent apis for node (based on the concepts used in C#'s FluentSharp

478 lines (320 loc) 16.9 kB
<!DOCTYPE html> <html> <head> <title>http.GET.litcoffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <ul id="jump_to"> <li> <a class="large" href="javascript:void(0);">Jump To &hellip;</a> <a class="small" href="javascript:void(0);">+</a> <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="assert_Array.html"> assert_Array.litcoffee </a> <a class="source" href="assert_Boolean.html"> assert_Boolean.litcoffee </a> <a class="source" href="assert_Function.html"> assert_Function.litcoffee </a> <a class="source" href="assert_Number.html"> assert_Number.litcoffee </a> <a class="source" href="assert_Object.html"> assert_Object.litcoffee </a> <a class="source" href="assert_String.html"> assert_String.litcoffee </a> <a class="source" href="fluentnode.html"> fluentnode.coffee </a> <a class="source" href="index.html"> index.md </a> <a class="source" href="Array.html"> Array.litcoffee </a> <a class="source" href="Boolean.html"> Boolean.litcoffee </a> <a class="source" href="Function.html"> Function.litcoffee </a> <a class="source" href="Number.html"> Number.litcoffee </a> <a class="source" href="Object.html"> Object.litcoffee </a> <a class="source" href="String.html"> String.litcoffee </a> <a class="source" href="console.html"> console.litcoffee </a> <a class="source" href="crypto.html"> crypto.litcoffee </a> <a class="source" href="fs.html"> fs.litcoffee </a> <a class="source" href="http.GET.html"> http.GET.litcoffee </a> <a class="source" href="http.POST.html"> http.POST.litcoffee </a> <a class="source" href="http.Server.html"> http.Server.litcoffee </a> <a class="source" href="http.html"> http.litcoffee </a> <a class="source" href="path.html"> path.litcoffee </a> <a class="source" href="process.html"> process.litcoffee </a> <a class="source" href="encoding.html"> encoding.litcoffee </a> <a class="source" href="globals.html"> globals.litcoffee </a> <a class="source" href="assert_Array.test.html"> assert_Array.test.coffee </a> <a class="source" href="assert_Boolean.test.html"> assert_Boolean.test.coffee </a> <a class="source" href="assert_Function.test.html"> assert_Function.test.coffee </a> <a class="source" href="assert_Number.test.html"> assert_Number.test.coffee </a> <a class="source" href="assert_Object.test.html"> assert_Object.test.coffee </a> <a class="source" href="assert_String.test.html"> assert_String.test.coffee </a> <a class="source" href="fluentnode.test.html"> fluentnode.test.coffee </a> <a class="source" href="Array.test.html"> Array.test.coffee </a> <a class="source" href="Boolean.test.html"> Boolean.test.coffee </a> <a class="source" href="Function.test.html"> Function.test.coffee </a> <a class="source" href="Number.test.html"> Number.test.coffee </a> <a class="source" href="Object.test.html"> Object.test.coffee </a> <a class="source" href="String.test.html"> String.test.coffee </a> <a class="source" href="console.test.html"> console.test.coffee </a> <a class="source" href="crypto.test.html"> crypto.test.coffee </a> <a class="source" href="fs.test.html"> fs.test.coffee </a> <a class="source" href="http.GET.test.html"> http.GET.test.coffee </a> <a class="source" href="http.POST.test.html"> http.POST.test.coffee </a> <a class="source" href="http.Server.test.html"> http.Server.test.coffee </a> <a class="source" href="http.test.html"> http.test.coffee </a> <a class="source" href="path.test.html"> path.test.coffee </a> <a class="source" href="process.test.html"> process.test.coffee </a> <a class="source" href="encoding.test.html"> encoding.test.coffee </a> <a class="source" href="globals.test.html"> globals.test.coffee </a> </div> </li> </ul> <ul class="sections"> <li id="title"> <div class="annotation"> <h1>http.GET.litcoffee</h1> </div> </li> <li id="section-1"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-1">&#182;</a> </div> <p>dependencies</p> </div> <div class="content"><div class='highlight'><pre>http = <span class="hljs-built_in">require</span> <span class="hljs-string">'http'</span> https = <span class="hljs-built_in">require</span> <span class="hljs-string">'https'</span> url = <span class="hljs-built_in">require</span> <span class="hljs-string">'url'</span> Server = http.Server</pre></div></div> </li> <li id="section-2"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>settings</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-built_in">global</span>.__fluentnode ?= {} <span class="hljs-built_in">global</span>.__fluentnode.http ?= {} using <span class="hljs-built_in">global</span>.__fluentnode.http, <span class="hljs-function">-&gt;</span> @.HTTP_GET_TIMEOUT = <span class="hljs-number">500</span> @.HTTP_GET_DELAY = <span class="hljs-number">10</span></pre></div></div> </li> <li id="section-3"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>@.<strong>http_GET</strong> (callback)</p> <p>Makes a GET request to @ and callback with (error, data, res)</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::http_GET = <span class="hljs-function"><span class="hljs-params">(callback)</span> -&gt;</span> url = url.parse(@.str()) engine = <span class="hljs-keyword">if</span> url.protocol <span class="hljs-keyword">is</span> <span class="hljs-string">'https:'</span> <span class="hljs-keyword">then</span> https <span class="hljs-keyword">else</span> http req = engine.get @.str(), <span class="hljs-function"><span class="hljs-params">(res)</span> -&gt;</span> data = <span class="hljs-string">''</span>; res.<span class="hljs-literal">on</span> <span class="hljs-string">'data'</span>, <span class="hljs-function"><span class="hljs-params">(chunk)</span> -&gt;</span> data += chunk res.<span class="hljs-literal">on</span> <span class="hljs-string">'end'</span> , <span class="hljs-function"><span class="hljs-params">( )</span> -&gt;</span> callback <span class="hljs-literal">null</span>, data, res req.<span class="hljs-literal">on</span> <span class="hljs-string">'error'</span>,<span class="hljs-function"><span class="hljs-params">(err )</span> -&gt;</span> <span class="hljs-keyword">if</span> callback callback err, <span class="hljs-literal">null</span>, <span class="hljs-literal">null</span> req</pre></div></div> </li> <li id="section-4"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>@.<strong>http_GET</strong> (callback)</p> <p>Makes a GET request to @ and callback with data (i.e. string representation of requested url)</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::GET = <span class="hljs-function"><span class="hljs-params">(callback)</span> -&gt;</span> @.http_GET (error, data, res)-&gt; callback(data)</pre></div></div> </li> <li id="section-5"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>@.<strong>GET_Json</strong></p> <p>Makes a GET request to @ and JSON parses the html data received</p> <p>Will throw an error if the returned data is not a valid JSON objec</p> <p>Twin methods: json_GET</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::GET_Json = <span class="hljs-function"><span class="hljs-params">(callback)</span> -&gt;</span> @.http_GET (error, data, res)-&gt; <span class="hljs-keyword">if</span> data callback(data.json_Parse()) <span class="hljs-keyword">else</span> callback {} <span class="hljs-attribute">String</span>::json_GET = <span class="hljs-attribute">String</span>::GET_Json</pre></div></div> </li> <li id="section-6"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>@.<strong>json_GET_With_Timeout</strong></p> <p>Makes a GET request (with a 500ms wait for valid data), and JSON parses the html data received</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::json_GET_With_Timeout = <span class="hljs-function"><span class="hljs-params">(callback)</span>-&gt;</span> @.http_GET_With_Timeout (data)-&gt; <span class="hljs-keyword">if</span> data callback data.json_Parse() <span class="hljs-keyword">else</span> callback {}</pre></div></div> </li> <li id="section-7"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>@.<strong>http_GET_Wait_For_Null</strong></p> <p>Waits until a GET request to @ results null (or empty)</p> <p>This is a good util method to check when servers go offline</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::http_GET_Wait_For_Null = <span class="hljs-function"><span class="hljs-params">(callback)</span>-&gt;</span> timeout = <span class="hljs-built_in">global</span>.__fluentnode.http.HTTP_GET_TIMEOUT delay = <span class="hljs-built_in">global</span>.__fluentnode.http.HTTP_GET_DELAY attempts = ~~(timeout/delay) <span class="hljs-function"> <span class="hljs-title">try_Http_Get</span> = <span class="hljs-params">(next)</span> =&gt;</span> @.GET (data) =&gt; <span class="hljs-keyword">if</span> data next.invoke_In(delay) <span class="hljs-keyword">else</span> callback(<span class="hljs-literal">null</span>) <span class="hljs-function"> <span class="hljs-title">run_Tests</span> = <span class="hljs-params">(count)</span>=&gt;</span> <span class="hljs-keyword">if</span> count try_Http_Get ()-&gt; run_Tests(count-<span class="hljs-number">1</span>) <span class="hljs-keyword">else</span> callback <span class="hljs-keyword">new</span> Error(<span class="hljs-string">"[http_GET_Wait_For_Null] never got a null from server <span class="hljs-subst">#{@}</span> after <span class="hljs-subst">#{attempts}</span> attempts"</span>) run_Tests(attempts)</pre></div></div> </li> <li id="section-8"> <div class="annotation"> <div class="pilwrap "> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>@.<strong>http_GET_With_Timeout</strong></p> <p>Makes a GET request (with a 500ms wait for valid data)</p> </div> <div class="content"><div class='highlight'><pre><span class="hljs-attribute">String</span>::http_GET_With_Timeout = <span class="hljs-function"><span class="hljs-params">(callback)</span>-&gt;</span> timeout = <span class="hljs-built_in">global</span>.__fluentnode.http.HTTP_GET_TIMEOUT delay = <span class="hljs-built_in">global</span>.__fluentnode.http.HTTP_GET_DELAY attempts = ~~(timeout/delay) <span class="hljs-function"> <span class="hljs-title">try_Http_Get</span> = <span class="hljs-params">(next)</span> =&gt;</span> @.GET (data) =&gt; <span class="hljs-keyword">if</span> data callback(data) <span class="hljs-keyword">else</span> (delay).invoke_After(next) <span class="hljs-function"> <span class="hljs-title">run_Tests</span> = <span class="hljs-params">(count)</span>=&gt;</span> <span class="hljs-keyword">if</span> count try_Http_Get ()-&gt; run_Tests(count-<span class="hljs-number">1</span>) <span class="hljs-keyword">else</span> callback <span class="hljs-literal">null</span> run_Tests attempts</pre></div></div> </li> </ul> </div> </body> </html>