Jade in ExecJS context - ruby

Very simply, I am having trouble running the compiled jade.js from jade-lang file in execjs.
If I include jade.js in an html file
jade.compile
returns a function.
When I do the following:
require 'execjs'
source = <<-src
var window = {};
#{open('jade.js').read}
var jade = window.jade;
src
context = ExecJS.compile source
context.eval "jade.compile"
=> nil
nil gets returned by eval.
Any idea where I'm going wrong? I've searched stack overflow, and even found a few gems that do this the same way as I am doing it, but when I execute on my system, it doesn't work. I've tried with therubyracer and node as my execjs runtimes.
Thanks for your help!

It appears that when the return from execjs is a function, it returns nil. So:
context.eval "jade.compile.toString()"
=> "function....."
Likewile
context.eval "jade.compile(#{template.to_json}).toString()"
=> "function...."
and
context.eval "jade.compile(#{template.to_json})()"
=> "some html"
This makes sense since the extract_result method of execjs/external_runtime.rb (line 62 as of this writing) uses JSON.decode, which will not decode a function. Only a derivative of object.

Related

How to save a parsed Gherkin into a Feature File (Ruby)

I need to programatically modify feature files of cucumber.
I have parsed a feature file using gherkin's gem 'gherkin/parser'.
The problem I find is that after parsing, I end up with a hash with the following data as example:
{:type=>:GherkinDocument, :feature=>{:type=>:Feature, :tags=>[], :location=>{:line=>1, :column=>1}, :language=>"en", :keyword=>"Feature", :name=>"MyFeature", :description=>" As an user\n I want to test a feature", :children=>[{:type=>:Scenario, :tags=>[{:type=>:Tag, :location=>{:line=>5, :column=>3}, :name=>"#MyTag"}], :location=>{:line=>6, :column=>3}, :keyword=>"Scenario", :name=>"My scenario", :steps=>[{:type=>:Step, :location=>{:line=>7, :column=>5}, :keyword=>"Given ", :text=>"I start the app"}, {:type=>:Step, :location=>{:line=>8, :column=>5}, :keyword=>"And ", :text=>"I generate a test user"}, {:type=>:Step, :location=>{:line=>9, :column=>5}, :keyword=>"And ", :text=>"I finish the flow"}]}]}, :comments=>[]}
is it possible to convert this GherkinDocument generated by the parser to a plain text feature file to save it? What method or gem should I use to get
According to the docs, you would use the Ruby Gherkin::Pickles::Compiler:
require 'gherkin/parser'
require 'gherkin/pickles/compiler'
parser = Gherkin::Parser.new
gherkin_document = parser.parse("Feature: ...")
# Make changes to gherkin_document
pickles = Gherkin::Pickles::Compiler.new.compile(gherkin_document)

AWS SDK Presigned Post Ruby

It's pretty straightforward but I have this issue:
Unexpected error while processing request: undefined method `PresignedPost' for Aws::S3:Module
My objective: Get the presigned URL for an object to which I can perform an upload file.
My gem has
gem 'aws-sdk', '~> 2'
Code:
##aws_creds = Aws::SharedCredentials.new(profile_name: profile)
Aws.config.update({region: 'us-west-2',credentials: ##aws_creds})
s3 = Aws::S3::Resource.new
#bucket = s3.bucket(bucketName)
form = Aws::S3::PresignedPost(:key => key )
if(form)
form.fields
end
you normally don't do a standalone presignedpost. you do it using the bucket method.
something like #bucket.presigned_post(:key=>key)
See doc:
http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/PresignedPost.html
Note: Normally you do not need to construct a PresignedPost yourself.
See Bucket#presigned_post and Object#presigned_post.

Add CSS minifier to Sprockets

I have a web application which uses rack.
The code:
set :assets, (Sprockets::Environment.new { |env|
env.js_compressor = Uglifier.new({
:output => {
:preserve_line => true,
:bracketize => true,
:beautify => true,
:indent_level => 4,
:semicolons => true,
},
:mangle => false
})
env.append_path(APP_ROOT + "/app/assets/images")
env.append_path(APP_ROOT + "/app/assets/javascripts")
env.append_path(APP_ROOT + "/app/assets/stylesheets")
})
I now want to add a CSS minifier to it.
Can someone explain why only javascript files are taken into the JS compressor above?
Can I add something like env.css_compressor = YUI::CssCompressor.new() after the JS_compressor to get my requirement done
UPDATE: Well the second actually worked. But I have no clue how it worked :)
You hadn't set up the Sprockets::Environment.css_compressor variable, so there was no compressor available to run on text/css assets.
puts Sprockets::Environment.methods.inspect
#=> [...#css_compressor, #css_compressor=, #js_compressor, #js_compressor=,...]
To answer your question about how assets are loaded, yes the default is to point to one load path and you can as well manipulate that to include others.
https://github.com/sstephenson/sprockets
The load path is an ordered list of directories that Sprockets uses to
search for assets. To add a directory to your environment's load path, use the append_path and prepend_path methods.

How to get mocha with chai assert to report file/line number?

I'm using mocha with chai.assert for my tests. Errors are caught and reported, but they don't show a file/line number where they happen. I'm used to having location information with tests in other languages, it's otherwise hard to figure out which assert failed.
Is there some way to get location information with mocha/chai/assert?
From version 1.9.1 onwards, if you set the includeStack flag to true, you'll get a stack trace on assertion failures:
var chai = require("chai");
chai.config.includeStack = true;
var assert = chai.assert;
describe("test", function () {
it("blah", function () {
assert.isTrue(false);
});
});
In versions prior to 1.9.1 you had to set chai.Assertion.includeStack = true. From 1.9.1 onwards this method of getting stack traces is deprecated. It is still available in 1.10.0 but may be removed in 1.11.0 or 2.0.0. (See here for details.)
The example above will show a stack trace where assert.isTrue fails. Like this:
AssertionError: expected false to be true
at Assertion.<anonymous> (.../node_modules/chai/lib/chai/core/assertions.js:193:10)
at Assertion.Object.defineProperty.get (.../node_modules/chai/lib/chai/utils/addProperty.js:35:29)
at Function.assert.isTrue (.../node_modules/chai/lib/chai/interface/assert.js:242:31)
at Context.<anonymous> (.../test.js:7:16)
[... etc ...]
(I've truncated the trace to what is only relevant and truncated the paths.) The last frame shown in what I've included above is the one where the error happened (.../test.js:7:16). I do not think that chai allows having only the file name and line number of the assertion call.
chai.Assertion.includeStack is now deprecated. Use chai.config instead
var chai = require("chai");
chai.config.includeStack = true;
var assert = chai.assert;

Handle json in ruby

In ruby how to convert the follwing into valid json
"{\"transferType\"=>\"D\", \"accountNumber\"=>\"123\", \"employeeId\"=>\"12\", \"districtNumber\"=>\"15\", \"orderedBy\"=>\"vbcb\", \"department\"=>\"vghghj\", \"po\"=>\"23434\", \"Phone\"=>\"4542433435\", \"Instructions\"=>\"vbvcnvbnb\"}"
The class of above is string. Because Without proper Json conversion i am problem like
Javascript replace for equal symbol
First of all you should make this in to proper JSON format. So replace the => in to :
and then try JSON.load or JSON.parse will work successfully.
2.2.3 :015 > data
=> "{\"transferType\"=>\"D\", \"accountNumber\"=>\"123\", \"employeeId\"=>\"12\", \"districtNumber\"=>\"15\", \"orderedBy\"=>\"vbcb\", \"department\"=>\"vghghj\", \"po\"=>\"23434\", \"Phone\"=>\"4542433435\", \"Instructions\"=>\"vbvcnvbnb\"}"
2.2.3 :016 > JSON.parse(data.gsub("=>", ":"))
=> {"transferType"=>"D", "accountNumber"=>"123", "employeeId"=>"12", "districtNumber"=>"15", "orderedBy"=>"vbcb", "department"=>"vghghj", "po"=>"23434", "Phone"=>"4542433435", "Instructions"=>"vbvcnvbnb"}
from the result you can do anything what do you want :)

Resources