Inside a Rails Service Object I have this:
test = %x[node lib/test.js "LOOK AT ME!"]
puts test
In a Javascript file, lib/test.js
, I have this:
var argument = process.argv[2];
console.log(argument);
This returns LOOK AT ME!
in the Rails server log as you'd expect.
If I run puts test.downcase
it returns look at me!
in the Rails server log as you'd expect.
Everything seems to be working until I try processing an HTML document.
In the Service Object I have this:
request = HTTParty.get("https://example.com/")
document = Nokogiri::HTML(request.body)
test = %x[node lib/test.js "#{document}"]
puts test
This is throwing an error in the Rails server log:
sh: 14: amp: not found
sh: 6: initial-scale=1.0>
<meta name=generator content=Jekyll: not found
sh: 30: row>
<img src=https://example.com/images/example.jpg alt=Example: not found
<!DOCTYPE html>
<html lang=en>
<head>
<meta http-equiv=Content-Type content=text/html
It looks like sh: 14: amp
is referencing a &
in the Google fonts call. Perhaps I need to do some kind of escaping?
How can I pass document
to that Javascript file and have it return without error?
Update:
If I call the file using fs.readFile
, as shown below, I get the expected return in the Rails server log.
var fs = require('fs');
fs.readFile('/path/to/document.html', 'utf8', function(err, data) {
if (err) throw err;
console.log(data);
});