docs/examples/example_1_send_message.html
<!DOCTYPE html>
<html>
<head>
<title>Example 1: Sending a message to a resource</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 …</a>
<a class="small" href="javascript:void(0);">+</a>
<div id="jump_wrapper">
<div id="jump_page_wrapper">
<div id="jump_page">
<a class="source" href="example_1_send_message.html">
example_1_send_message.coffee
</a>
<a class="source" href="example_2_authentication.html">
example_2_authentication.coffee
</a>
</div>
</div>
</li>
</ul>
<ul class="sections">
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<h1 id="example-1-sending-a-message-to-a-resource">Example 1: Sending a message to a resource</h1>
<p>Prerequisites:</p>
<ul>
<li>RabbitMQ running</li>
<li>Memcached running</li>
</ul>
</div>
<div class="content"><div class='highlight'><pre>
AlchemyResource = <span class="hljs-built_in">require</span> <span class="hljs-string">'../src/alchemy-resource'</span></pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>Creating the <code>Hello</code> resource, whose path is ‘/hello’</p>
</div>
<div class="content"><div class='highlight'><pre>hello_resource = <span class="hljs-keyword">new</span> AlchemyResource.Resource(<span class="hljs-string">"Hello"</span>, <span class="hljs-string">'/hello'</span>)</pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>The Hello resource implements <code>show</code> which takes a body and returns a string of the name</p>
</div>
<div class="content"><div class='highlight'><pre>hello_resource.show = <span class="hljs-function"><span class="hljs-params">(context)</span> -></span>
{body: <span class="hljs-string">"Hello <span class="hljs-subst">#{context.body.name}</span>"</span>}</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Make show action public so no authentication is needed</p>
</div>
<div class="content"><div class='highlight'><pre>hello_resource.show.public = <span class="hljs-literal">true</span></pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>The resource service is created which contains the resource</p>
</div>
<div class="content"><div class='highlight'><pre>service = <span class="hljs-keyword">new</span> AlchemyResource.ResourceService(<span class="hljs-string">'hello.service'</span>, [hello_resource])</pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Start the Resource Service</p>
</div>
<div class="content"><div class='highlight'><pre>service.start()
.<span class="hljs-keyword">then</span>( <span class="hljs-function">-></span></pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>Service sending message to the resource,
it only knows the path and does not know where the service lives.</p>
</div>
<div class="content"><div class='highlight'><pre> service.send_message_to_resource({
path: <span class="hljs-string">'/hello'</span>
body: JSON.stringify({ name: <span class="hljs-string">"Alice"</span> })
verb: <span class="hljs-string">"GET"</span>
})
)
.<span class="hljs-keyword">then</span>( <span class="hljs-function"><span class="hljs-params">(response)</span> -></span>
<span class="hljs-built_in">console</span>.log(response.body) <span class="hljs-comment"># "Hello Alice"</span>
)
.<span class="hljs-keyword">finally</span>( <span class="hljs-function">-></span>
service.stop()
)</pre></div></div>
</li>
</ul>
</div>
</body>
</html>