Merge pull request #546 from BrianHepler/load_compliments_from_remote_file

Load compliments from remote file
This commit is contained in:
Michael Teeuw 2016-11-30 16:43:14 +01:00 committed by GitHub
commit c949548150
3 changed files with 61 additions and 0 deletions

View File

@ -28,6 +28,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Added option `disabled` for modules. - Added option `disabled` for modules.
- Added option `address` to set bind address. - Added option `address` to set bind address.
- Added option `onlyTemp` for currentweather module to show show only current temperature and weather icon. - Added option `onlyTemp` for currentweather module to show show only current temperature and weather icon.
- Added option `remoteFile` to compliments module to load compliment array from filesystem.
### Updated ### Updated
- Modified translations for Frysk. - Modified translations for Frysk.

View File

@ -56,6 +56,15 @@ The following properties can be configured:
<br><b>Default value:</b> See <i>compliment configuration</i> below. <br><b>Default value:</b> See <i>compliment configuration</i> below.
</td> </td>
</tr> </tr>
<tr>
<td><code>remoteFile</code></td>
<td>External file from which to load the compliments<br>
<br><b>Possible values:</b>Path to a JSON file containing compliments, configured
as per the value of the <i>compliments configuration</i> (see below). An object with three arrays:
morning, afternoon and evening. - <code>compliments.json</code>
<br><b>Default value:</b> <code>null</code> (Do not load from file)
</td>
</tr>
</tbody> </tbody>
</table> </table>
@ -123,3 +132,32 @@ config: {
} }
} }
```` ````
### External Compliment File
You may specify an external file that contains the three compliment arrays. This is particularly useful if you have a
large number of compliments and do not wish to crowd your `config.js` file with a large array of compliments.
Adding the `remoteFile` variable will override an array you specify in the configuration file.
This file must be straight JSON. Note that the array names need quotes
around them ("morning", "afternoon", "evening", "snow", "rain", etc.).
#### Example compliments.json file:
````json
{
"morning" : [
"Good morning, sunshine!",
"Who needs coffee when you have your smile?",
"Go get 'em, Tiger!"
],
"afternoon" : [
"Hitting your stride!",
"You are making a difference!",
"You're more fun than bubble wrap!"
],
"evening" : [
"You made someone smile today, I know it.",
"You are making a difference.",
"The day was better for your efforts."
]
}
````

View File

@ -29,6 +29,7 @@ Module.register("compliments",{
] ]
}, },
updateInterval: 30000, updateInterval: 30000,
remoteFile: null,
fadeSpeed: 4000 fadeSpeed: 4000
}, },
@ -46,6 +47,12 @@ Module.register("compliments",{
this.lastComplimentIndex = -1; this.lastComplimentIndex = -1;
if (this.config.remoteFile != null) {
this.complimentFile((response) => {
this.config.compliments = JSON.parse(response);
});
}
// Schedule update timer. // Schedule update timer.
var self = this; var self = this;
setInterval(function() { setInterval(function() {
@ -104,6 +111,21 @@ Module.register("compliments",{
}, },
/* complimentFile(callback)
* Retrieve a file from the local filesystem
*/
complimentFile: function(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', this.file(this.config.remoteFile), true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
callback(xobj.responseText);
}
};
xobj.send(null);
},
/* complimentArray() /* complimentArray()
* Retrieve a random compliment. * Retrieve a random compliment.
* *