How to properly read and parse data from Firebase in Nim? - nim-lang

I have a database in Firebase which currently I can access through CURL like this
curl "https://mydb.firebaseio.com/my_data.json?auth=XKJYED78634jsvdffwu7riugwer"
I want to make a Nim script to actively listen to this data for any changes and then generate web pages accordingly, what would be the best way to do that ? I am not able to find any library for Firebase support in Nim.
Thanks #jason, based on your response, this is what I did:
import std/[httpclient, json]
const url = "https://mydb.firebaseio.com/my_data.json?auth=XKJYED78634jsvdffwu7riugwer"
var client = newHttpClient()
let parsedJsonObj = parseJson(client.getContent(url))
client.close()
echo parsedJsonObj
Then I compiled this with -d:ssl flag and this is successfully reading the JSON data and dumping it to stdout. Another problem I am facing is that this JSON data is unstructured, is there any way to iterate through all records without knowing the key names ?

I'm no expert in webdev, but given that's a link that returns a json formatted file you can do something like the following:
import std/[httpclient, htmlgen, asyncdispatch, json]
const url = "https://mydb.firebaseio.com/my_data.json?auth=XKJYED78634jsvdffwu7riugwer" # do this safer using dotenvs or similar
proc buildSite(): Future[string] {.async.} =
let
myHttpClient = newAsyncHttpClient()
data = parseJson(await(myHttpClient.getContent(url)))
# Do stuff with `data, we'll just make a header for an example
result = h1("Hellow World")
myHttpClient.close()
proc main() {.async.} =
var isWatchingSite = true
while isWatchingSite:
try:
let site = await buildSite()
except HttpRequestError as err:
echo err.msg
await sleepAsync(10000)
waitFor main()

Related

Using ipfs in javascipt: how to read an object the same way as dumpign it to file from command line and reading the file?

I have some arrow file that I am trying to read in javascript. Dumping it to file via the commanline: ipfs get HASH and then
fs = require('fs')
a = fs.readFileSync(HASH)
da = arrow.Table.from(a)
works fine.
Loading the cid (HASH)
ipfs = require('ipfs')
ipfs.create({repo: String(Math.random() + Date.now()) }).then(x=>node=x).then(
node=>node.object.get(HASH)
).then(x=>data=x)
Gives my something that has a data.Data buffer in some other format and it does not load into an arrow Table in the same way. How can I get the bytes in the same was as the readFileSync?
It turns out you need to use the ipfs cat method and that returns an async iterator so there is a small step to be aware of to get that into the arrow table.
I am not sure if there is a direct method for the get.
async function docat() {
var out = []
for await (const result of node.cat(has)) {
out.push(result)
}
return out
}

How can I use exported strings as my elements in Puppeteer

I can't seem to declare a string const and use that as my element with Puppeteer. For example:
await page.click("#playerView");
Works fine, but:
const playerViewId = "#playerView";
await page.click(playerViewId);
Doesn't. I ultimately want to hold all my Page Elements in an object in a separate file to tidy up my project.
Any ideas why this isn't working?
Thanks
I can confirm that the following case does in fact work:
const playerViewId = '#playerView';
await page.click(playerViewId);
If this is not working, consider upgrading your version of Node.js and/or Puppeteer.
If you are trying to define your variable in a separate file, you can use:
// external-file.js:
module.exports.playerViewId = '#playerView';
// main-file.js:
const external_variables = require('./external-file');
const playerViewId = external_variables.playerViewId;
await page.click(playerViewId);
Additionally, you should check to make sure that the element with id playerView exists and has loaded completely before attempting to use page.click().

Logging variable in nodejs

I have a variable named, 'res'. This variable if used with console.log() print all request to a function. My question is... How to log this variable in a log.txt file?
Example using console.log(res)
Print in console:
[ 'AndroidShareANE',
'pixi.js',
'ShortSword',
'faceshiftparser',
'clmutils',
'chaikin-smooth',
'getuservideo',
'getboundingbox',
'clmdraw',
'SpriteSheetScrubberTutorial',
'interpolation-arrays',
This is a one part of response.
My purpose is log in log.txt file the var content. Identical to console.log() result.
Thanks for your collaboration, and sorry my bad english.
The easiest way is to create a new Console object that writes to your file. The docs have an example of this exact thing, which I'll reproduce here for posterity:
const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
// custom simple logger
const logger = new Console(output, errorOutput);
// use it like console
var count = 5;
logger.log('count: %d', count);
// in stdout.log: count 5
If you don't pass the second argument (errorOutput) to new Console then the error output will also be written to the output file.

How does this npm build work?

https://github.com/apigee-127/swagger-converter
I see this code:
var convert = require('swagger-converter');
var fs = require('fs');
var resourceListing = JSON.parse(fs.readFileSync('/path/to/petstore/index.json').toString());
var apiDeclarations = [ JSON.parse(fs.readFileSync('/path/to/petstore/pet.json').toString()),
JSON.parse(fs.readFileSync('/path/to/petstore/user.json').toString()),
JSON.parse(fs.readFileSync('/path/to/petstore/store.json').toString())
];
var swagger2Document = convert(resourceListing, apiDeclarations);
console.log(JSON.stringify(swagger2Document, null, 2));
I'm confsued as to what exactly I'm supposed to do here to run this? Do I start a node http server?
To run the file you pasted, just save the code into a file like script.js. Then from the command line (with node installed) run node script.js. That will run the file. Here's a breakdown of what it's doing:
var convert = require('swagger-converter');
This line gets reference to the swagger-converter module that you linked to. That module is designed to allow you to convert swagger documents into JSON.
var fs = require('fs');
This line gets reference to the node built-in filesystem module (fs for short). It provides an API for interacting with the filesystem on your machine when the script is running.
var resourceListing = JSON.parse(fs.readFileSync('/path/to/petstore/index.json').toString());
This line could be broken down to:
var indexContent = fs.readFileSync('/path/to/petstore/index.json');
JSON.parse(indexContent.toString());
readFileSync returns the contents of the index.json file as a buffer object, which is easily turned into a simple string with the call to .toString(). Then they pass it to JSON.parse which parses the string and turns it into a simple JavaScript object.
Fun Fact: They could have skipped those steps with a simple var resourceListing = require('/path/to/petstore/index.json');. Node knows how to read JSON files and automatically turn them into JavaScript objects. You need only pass the path to require.
var apiDeclarations = [ JSON.parse(fs.readFileSync('/path/to/petstore/pet.json').toString()),
JSON.parse(fs.readFileSync('/path/to/petstore/user.json').toString()),
JSON.parse(fs.readFileSync('/path/to/petstore/store.json').toString())
];
This bit of code does the same thing as the resourceListing except it creates an array of three JavaScript objects based on those JSON files. They also could have used require here to save a bit of work.
Then finally they use the converter to do the conversion and then they log that data to the terminal where your script is running.
var swagger2Document = convert(resourceListing, apiDeclarations);
console.log(JSON.stringify(swagger2Document, null, 2));
JSON.stringify is the opposite of JSON.parse. stringify turns a JavaScript object into a JSON string whereas parse turns a JSON string into a JavaScript object.

How to check express local variable exists when in script tag of jade?

I have a data variable sent to client-side, but it may not always be included as a variable in the express locals. If it doesn't exist, var data = !{JSON.stringify(data)}; returns var data = ; which causes a js error.
I've tried using conditionals prefixed with '-' but that doesn't seem to work.
script(type='text/javascript')
- if locals.data
var data = !{JSON.stringify(data)};
- else
var data = {};
How do I give it a default if locals.data is undefined?
Don't you hate it when you wrack your brain, then ask for help on SO, only to figure it out 5min later...
Looks like the following keeps the jade and javascript happy:
var data = !{ JSON.stringify(locals.data || '') };

Resources