Implement data.getText and data.setText shortcut functions

This commit is contained in:
Juan Cruz Viotti 2014-12-09 08:44:45 -04:00
parent 9f713395af
commit d9bfd22909
30 changed files with 148 additions and 197 deletions

View File

@ -266,7 +266,7 @@
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:53 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -87,7 +87,7 @@
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -83,7 +83,7 @@ Error: Invalid configuration file: /opt/resin.conf
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -70,7 +70,7 @@ Error: Invalid credentials
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -83,7 +83,7 @@ Error: You don&#39;t have any applications
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -83,7 +83,7 @@ Error: Couldn&#39;t find application
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -47,9 +47,8 @@
</span> </span>
<span class='note private title'>Private</span> <span class='note private title'>Private</span>
<span class='desc'> <span class='desc'>
Authenticate with the server <p>resin.auth.authenticate credentials, (error, token) -&gt; Authenticate with the server <pre><code>resin.auth.authenticate credentials, (error, token) -&gt; throw error if error? console.log(token)
throw error if error? </code></pre>
console.log(token)</p>
</span> </span>
</li> </li>
<li> <li>
@ -135,9 +134,8 @@
<strong>Note:</strong> <strong>Note:</strong>
You should use login() when possible, as it takes care of saving the token as well. You should use login() when possible, as it takes care of saving the token as well.
</div> </div>
<p>Authenticate with the server</p><p>resin.auth.authenticate credentials, (error, token) -&gt; <p>Authenticate with the server</p><pre><code>resin.auth.authenticate credentials, (error, token) -&gt; throw error if error? console.log(token)
throw error if error? </code></pre>
console.log(token)</p>
</div> </div>
<div class='tags'> <div class='tags'>
<h3>Parameters:</h3> <h3>Parameters:</h3>
@ -358,6 +356,10 @@
<br> <br>
</p> </p>
<div class='docstring'> <div class='docstring'>
<div class='note todo'>
<strong>TODO:</strong>
This should be moved somewhere else, as it only used by the auth actions
</div>
<p>Parse colon separated credentials</p><pre><code>resin.auth.parseCredentials &#39;johndoe:secret&#39;, (error, credentials) -&gt; throw error if error? console.log(credentials.username) console.log(credentials.password) <p>Parse colon separated credentials</p><pre><code>resin.auth.parseCredentials &#39;johndoe:secret&#39;, (error, credentials) -&gt; throw error if error? console.log(credentials.username) console.log(credentials.password)
</code></pre> </code></pre>
</div> </div>
@ -390,7 +392,7 @@
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -119,7 +119,7 @@ console.log(contents.remoteUrl)
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -169,7 +169,7 @@
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -37,7 +37,7 @@
</table> </table>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -50,20 +50,6 @@
<span class='note private title'>Private</span> <span class='note private title'>Private</span>
<span class='desc'> <span class='desc'>
Check if valid path <pre><code>console.log isValidPath(&#39;/Users/me&#39;) Check if valid path <pre><code>console.log isValidPath(&#39;/Users/me&#39;)
</code></pre>
</span>
</li>
<li>
<span class='signature'>
<a href='#isDirectory-'>
~
(void)
<b>isDirectory</b><span>(directory, callback)</span>
</a>
</span>
<span class='note private title'>Private</span>
<span class='desc'>
Check if path is directory <pre><code>console.log isDirectory(&#39;/usr/local/share&#39;)
</code></pre> </code></pre>
</span> </span>
</li> </li>
@ -112,51 +98,12 @@ console.log isValidPath([ 1, 2, 3 ]) # False
<span class='desc'>is valid path </span> <span class='desc'>is valid path </span>
</li> </li>
</ul> </ul>
</div>
</div>
<div class='method_details'>
<p class='signature' id='isDirectory-'>
~
(void)
<b>isDirectory</b><span>(directory, callback)</span>
<span class='note private'>Private</span>
<br>
</p>
<div class='docstring'>
<p>Check if path is directory</p><pre><code>console.log isDirectory(&#39;/usr/local/share&#39;) # True
console.log isDirectory(&#39;/Users/me/app.js&#39;) # False
</code></pre>
</div>
<div class='tags'>
<h3>Parameters:</h3>
<ul class='param'>
<li>
<span class='name'>directory</span>
<span class='type'>
(
<tt>String</tt>
)
</span>
&mdash;
<span class='desc'>directory </span>
</li>
<li>
<span class='name'>callback</span>
<span class='type'>
(
<tt>Function</tt>
)
</span>
&mdash;
<span class='desc'>callback(error, isDirectory) </span>
</li>
</ul>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -186,7 +186,7 @@ console.log(devices)
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -94,7 +94,7 @@ resin.errors.handle(error, shouldExit)
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -37,20 +37,6 @@
</table> </table>
<h2>Method Summary</h2> <h2>Method Summary</h2>
<ul class='summary'> <ul class='summary'>
<li>
<span class='signature'>
<a href='#isAbsolutePath-'>
~
(Boolean)
<b>isAbsolutePath</b><span>(p)</span>
</a>
</span>
<span class='note private title'>Private</span>
<span class='desc'>
Check is path is absolute <pre><code>console.log isAbsolutePath(&#39;/usr&#39;)
</code></pre>
</span>
</li>
<li> <li>
<span class='signature'> <span class='signature'>
<a href='#prefixObjectValuesWithPath-'> <a href='#prefixObjectValuesWithPath-'>
@ -71,46 +57,6 @@ console.log(object.dataPrefix)
</ul> </ul>
<h2>Method Details</h2> <h2>Method Details</h2>
<div class='methods'> <div class='methods'>
<div class='method_details'>
<p class='signature' id='isAbsolutePath-'>
~
(Boolean)
<b>isAbsolutePath</b><span>(p)</span>
<span class='note private'>Private</span>
<br>
</p>
<div class='docstring'>
<p>Check is path is absolute</p><pre><code>console.log isAbsolutePath(&#39;/usr&#39;) # True
console.log isAbsolutePath(&#39;../Music&#39;) # False
</code></pre>
</div>
<div class='tags'>
<h3>Parameters:</h3>
<ul class='param'>
<li>
<span class='name'>path</span>
<span class='type'>
(
<tt>String</tt>
)
</span>
&mdash;
<span class='desc'>path </span>
</li>
</ul>
<h3>Returns:</h3>
<ul class='return'>
<li>
<span class='type'></span>
(
<tt>Boolean</tt>
)
&mdash;
<span class='desc'>is absolute </span>
</li>
</ul>
</div>
</div>
<div class='method_details'> <div class='method_details'>
<p class='signature' id='prefixObjectValuesWithPath-'> <p class='signature' id='prefixObjectValuesWithPath-'>
~ ~
@ -159,7 +105,7 @@ console.log(object.dataPrefix) # /opt/resin
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -57,7 +57,7 @@
</dl> </dl>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -348,7 +348,7 @@
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -47,7 +47,7 @@
</table> </table>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -326,7 +326,7 @@
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -316,7 +316,7 @@
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -39,10 +39,10 @@
<ul class='summary'> <ul class='summary'>
<li> <li>
<span class='signature'> <span class='signature'>
<a href='#getAll-'> <a href='#getAllByApplication-'>
~ ~
(void) (void)
<b>getAll</b><span>(applicationId, callback)</span> <b>getAllByApplication</b><span>(applicationId, callback)</span>
</a> </a>
</span> </span>
<span class='desc'> <span class='desc'>
@ -67,17 +67,13 @@
<h2>Method Details</h2> <h2>Method Details</h2>
<div class='methods'> <div class='methods'>
<div class='method_details'> <div class='method_details'>
<p class='signature' id='getAll-'> <p class='signature' id='getAllByApplication-'>
~ ~
(void) (void)
<b>getAll</b><span>(applicationId, callback)</span> <b>getAllByApplication</b><span>(applicationId, callback)</span>
<br> <br>
</p> </p>
<div class='docstring'> <div class='docstring'>
<div class='note todo'>
<strong>TODO:</strong>
Rename this to getAllByApplication
</div>
<p>Get all environment variables by application</p><pre><code>resin.models.environmentVariables.getAll (error, environmentVariables) -&gt; throw error if error? console.log(environmentVariables) <p>Get all environment variables by application</p><pre><code>resin.models.environmentVariables.getAll (error, environmentVariables) -&gt; throw error if error? console.log(environmentVariables)
</code></pre> </code></pre>
</div> </div>
@ -158,7 +154,7 @@
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -52,7 +52,7 @@
</dl> </dl>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -156,7 +156,7 @@ console.log(result)
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -227,7 +227,7 @@ resin.server.request { method: &#39;GET&#39; url: &#39;/download&#39; p
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -77,7 +77,7 @@
</dl> </dl>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

View File

@ -235,7 +235,7 @@
</div> </div>
</div> </div>
<div id='footer'> <div id='footer'>
December 08, 14 10:08:42 by December 09, 14 08:42:54 by
<a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'> <a href='https://github.com/coffeedoc/codo' title='CoffeeScript API documentation generator'>
Codo Codo
</a> </a>

File diff suppressed because one or more lines are too long

View File

@ -66,14 +66,6 @@
(lib&#47;resin&#47;token&#47;token.coffee) (lib&#47;resin&#47;token&#47;token.coffee)
</small> </small>
</li> </li>
<li>
<a href='class/InvalidConfigFile.html#constructor-dynamic' target='main' title='constructor'>
#constructor
</a>
<small>
(InvalidConfigFile)
</small>
</li>
<li> <li>
<a href='class/NotFound.html#constructor-dynamic' target='main' title='constructor'> <a href='class/NotFound.html#constructor-dynamic' target='main' title='constructor'>
#constructor #constructor
@ -90,6 +82,14 @@
(InvalidCredentials) (InvalidCredentials)
</small> </small>
</li> </li>
<li>
<a href='class/InvalidConfigFile.html#constructor-dynamic' target='main' title='constructor'>
#constructor
</a>
<small>
(InvalidConfigFile)
</small>
</li>
<li> <li>
<a href='class/NotAny.html#constructor-dynamic' target='main' title='constructor'> <a href='class/NotAny.html#constructor-dynamic' target='main' title='constructor'>
#constructor #constructor
@ -130,14 +130,6 @@
(lib&#47;resin&#47;os&#47;os.coffee) (lib&#47;resin&#47;os&#47;os.coffee)
</small> </small>
</li> </li>
<li>
<a href='file/lib/resin/models/device.coffee.html#get-' target='main' title='get'>
~get
</a>
<small>
(lib&#47;resin&#47;models&#47;device.coffee)
</small>
</li>
<li> <li>
<a href='file/lib/resin/data/data-prefix.coffee.html#get-' target='main' title='get'> <a href='file/lib/resin/data/data-prefix.coffee.html#get-' target='main' title='get'>
~get ~get
@ -146,6 +138,14 @@
(lib&#47;resin&#47;data&#47;data-prefix.coffee) (lib&#47;resin&#47;data&#47;data-prefix.coffee)
</small> </small>
</li> </li>
<li>
<a href='file/lib/resin/models/device.coffee.html#get-' target='main' title='get'>
~get
</a>
<small>
(lib&#47;resin&#47;models&#47;device.coffee)
</small>
</li>
<li> <li>
<a href='file/lib/resin/models/application.coffee.html#get-' target='main' title='get'> <a href='file/lib/resin/models/application.coffee.html#get-' target='main' title='get'>
~get ~get
@ -154,14 +154,6 @@
(lib&#47;resin&#47;models&#47;application.coffee) (lib&#47;resin&#47;models&#47;application.coffee)
</small> </small>
</li> </li>
<li>
<a href='file/lib/resin/models/environment-variables.coffee.html#getAll-' target='main' title='getAll'>
~getAll
</a>
<small>
(lib&#47;resin&#47;models&#47;environment-variables.coffee)
</small>
</li>
<li> <li>
<a href='file/lib/resin/models/application.coffee.html#getAll-' target='main' title='getAll'> <a href='file/lib/resin/models/application.coffee.html#getAll-' target='main' title='getAll'>
~getAll ~getAll
@ -178,6 +170,14 @@
(lib&#47;resin&#47;models&#47;device.coffee) (lib&#47;resin&#47;models&#47;device.coffee)
</small> </small>
</li> </li>
<li>
<a href='file/lib/resin/models/environment-variables.coffee.html#getAllByApplication-' target='main' title='getAllByApplication'>
~getAllByApplication
</a>
<small>
(lib&#47;resin&#47;models&#47;environment-variables.coffee)
</small>
</li>
<li> <li>
<a href='file/lib/resin/models/device.coffee.html#getAllByApplication-' target='main' title='getAllByApplication'> <a href='file/lib/resin/models/device.coffee.html#getAllByApplication-' target='main' title='getAllByApplication'>
~getAllByApplication ~getAllByApplication
@ -258,22 +258,6 @@
(lib&#47;resin&#47;log&#47;log.coffee) (lib&#47;resin&#47;log&#47;log.coffee)
</small> </small>
</li> </li>
<li>
<a href='file/lib/resin/helpers/helpers.coffee.html#isAbsolutePath-' target='main' title='isAbsolutePath'>
~isAbsolutePath
</a>
<small>
(lib&#47;resin&#47;helpers&#47;helpers.coffee)
</small>
</li>
<li>
<a href='file/lib/resin/data/fs-utils/fs-utils.coffee.html#isDirectory-' target='main' title='isDirectory'>
~isDirectory
</a>
<small>
(lib&#47;resin&#47;data&#47;fs-utils&#47;fs-utils.coffee)
</small>
</li>
<li> <li>
<a href='file/lib/resin/auth/auth.coffee.html#isLoggedIn-' target='main' title='isLoggedIn'> <a href='file/lib/resin/auth/auth.coffee.html#isLoggedIn-' target='main' title='isLoggedIn'>
~isLoggedIn ~isLoggedIn
@ -346,14 +330,6 @@
(lib&#47;resin&#47;helpers&#47;helpers.coffee) (lib&#47;resin&#47;helpers&#47;helpers.coffee)
</small> </small>
</li> </li>
<li>
<a href='file/lib/resin/models/environment-variables.coffee.html#remove-' target='main' title='remove'>
~remove
</a>
<small>
(lib&#47;resin&#47;models&#47;environment-variables.coffee)
</small>
</li>
<li> <li>
<a href='file/lib/resin/models/device.coffee.html#remove-' target='main' title='remove'> <a href='file/lib/resin/models/device.coffee.html#remove-' target='main' title='remove'>
~remove ~remove
@ -370,6 +346,14 @@
(lib&#47;resin&#47;models&#47;application.coffee) (lib&#47;resin&#47;models&#47;application.coffee)
</small> </small>
</li> </li>
<li>
<a href='file/lib/resin/models/environment-variables.coffee.html#remove-' target='main' title='remove'>
~remove
</a>
<small>
(lib&#47;resin&#47;models&#47;environment-variables.coffee)
</small>
</li>
<li> <li>
<a href='file/lib/resin/server/server.coffee.html#request-' target='main' title='request'> <a href='file/lib/resin/server/server.coffee.html#request-' target='main' title='request'>
~request ~request

View File

@ -59,6 +59,23 @@ exports.get = haltIfNoPrefix (key, options, callback) ->
keyPath = constructPath(key) keyPath = constructPath(key)
fs.readFile(keyPath, options, callback) fs.readFile(keyPath, options, callback)
# Get data by key as text
#
# @param {String} key path relative to dataPrefix
# @param {Function} callback callback(error, value)
#
# @throw {Error} Will throw if data prefix was not previously set
#
# @note This is the same as get(), however it assumes utf8 encoding.
#
# @example Get text data
# resin.data.getText 'myTextFile', (error, data) ->
# throw error if error?
# console.log(data)
#
exports.getText = haltIfNoPrefix (key, callback) ->
exports.get(key, encoding: 'utf8', callback)
# Set/Update a data resource # Set/Update a data resource
# #
# @param {String} key path relative to dataPrefix # @param {String} key path relative to dataPrefix
@ -79,6 +96,23 @@ exports.set = haltIfNoPrefix (key, value, options, callback) ->
keyPath = constructPath(key) keyPath = constructPath(key)
fs.writeFile(keyPath, value, options, callback) fs.writeFile(keyPath, value, options, callback)
# Set/Update a data resource as text
#
# @param {String} key path relative to dataPrefix
# @param {String, Buffer} value key value
# @param {Function} callback callback(error)
#
# @throw {Error} Will throw if data prefix was not previously set
#
# @note This is the same as set(), however it assumes utf8 encoding.
#
# @example Set text data
# resin.data.setText 'greeting/en', 'Hello World!', (error) ->
# throw error if error?
#
exports.setText = haltIfNoPrefix (key, value, callback) ->
exports.set(key, value, encoding: 'utf8', callback)
# Check if value exists # Check if value exists
# #
# @param {String} key path relative to dataPrefix # @param {String} key path relative to dataPrefix

View File

@ -1,6 +1,7 @@
expect = require('chai').expect expect = require('chai').expect
_ = require('lodash') _ = require('lodash')
fsPlus = require('fs-plus') fsPlus = require('fs-plus')
sinon = require('sinon')
mock = require('../../../tests/utils/mock') mock = require('../../../tests/utils/mock')
async = require('async') async = require('async')
settings = require('../settings') settings = require('../settings')
@ -90,6 +91,29 @@ describe 'Data:', ->
expect(value).to.be.undefined expect(value).to.be.undefined
done() done()
describe '#getText()', ->
it 'should be able to read a valid key', (done) ->
data.getText FILESYSTEM.text.key, (error, value) ->
expect(error).to.not.exist
expect(value).to.equal(FILESYSTEM.text.contents)
done()
it 'should call get(), assumming utf8 encoding', (done) ->
mock.fs.restore()
dataGetSpy = sinon.spy(data, 'get')
mock.fs.init(FILESYSTEM)
key = FILESYSTEM.text.key
callback = (error, value) ->
expect(dataGetSpy).to.have.been.called
expect(dataGetSpy).to.have.been.calledWith(key, encoding: 'utf8', callback)
dataGetSpy.restore()
done()
data.getText(key, callback)
describe '#has()', -> describe '#has()', ->
it 'should return true if a file exists', (done) -> it 'should return true if a file exists', (done) ->
@ -147,6 +171,24 @@ describe 'Data:', ->
it('should be able to write a file', writeAndCheckFixture(FILES_FIXTURES.hello)) it('should be able to write a file', writeAndCheckFixture(FILES_FIXTURES.hello))
it('should be able to write a nested file', writeAndCheckFixture(FILES_FIXTURES.nested)) it('should be able to write a nested file', writeAndCheckFixture(FILES_FIXTURES.nested))
describe '#setText()', ->
it 'should call set(), assumming utf8 encoding', (done) ->
mock.fs.restore()
dataSetSpy = sinon.spy(data, 'set')
mock.fs.init(FILESYSTEM)
key = FILESYSTEM.text.key
contents = 'Hello World'
callback = (error, value) ->
expect(dataSetSpy).to.have.been.called
expect(dataSetSpy).to.have.been.calledWith(key, contents, encoding: 'utf8', callback)
dataSetSpy.restore()
done()
data.setText(key, contents, callback)
describe '#remove()', -> describe '#remove()', ->
removeAndCheckFile = (file) -> removeAndCheckFile = (file) ->

View File

@ -19,7 +19,7 @@ TOKEN_KEY = 'token'
# throw error if error? # throw error if error?
# #
exports.saveToken = (newToken, callback) -> exports.saveToken = (newToken, callback) ->
data.set(TOKEN_KEY, newToken, encoding: 'utf8', callback) data.setText(TOKEN_KEY, newToken, callback)
# Check if we have any token saved # Check if we have any token saved
# #
@ -48,7 +48,7 @@ exports.hasToken = (callback) ->
# console.log("My token is: #{token}") # console.log("My token is: #{token}")
# #
exports.getToken = (callback) -> exports.getToken = (callback) ->
data.get(TOKEN_KEY, encoding: 'utf8', callback) data.getText(TOKEN_KEY, callback)
# Remove token from the filesystem # Remove token from the filesystem
# #