Skip to content

Instantly share code, notes, and snippets.

@littlemove
Created January 18, 2016 17:15
Show Gist options
  • Save littlemove/7afaba938957950ec202 to your computer and use it in GitHub Desktop.
Save littlemove/7afaba938957950ec202 to your computer and use it in GitHub Desktop.
require 'execjs'
# JS file path with the Kigo price calculation algorithm.
kigo_js_path = "#{Rails.root}/db/kigo_price_calculation.js"
source = %Q(
var price_info = '{}';
var checkin = new Date('2016-05-07');
var checkout = new Date('2016-05-17');
var guests = 2;
#{File.read(kigo_js_path)}
)
ExecJS.exec(source)
@pacoguzman
Copy link

setup do
  @ctx = ExecJS.compile(File.read(kigo_js_path))
end

def test_something
  @ctx.call("var guests = 2;")
  @ctx.call("var price_info = {};")

  actual = @ctx.call("kigo_price_function_wadus(guests, price_info);") # I hope this return some ruby instance that you can check

  assert actual, "Insufficient info"
end

@pacoguzman
Copy link

ctx = ExecJS.compile('function wadus(guests) { return "number of guests: " + guests;};')
=> #<ExecJS::ExternalRuntime::Context:0x007f97fa3bfa08
ctx.call("wadus", 3)
=> "number of guests: 3"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment