Skip to content

Commit

Permalink
Showing 3 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions samples/truffle/interop/weather/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
```
$ git clone https://github.com/lucasocon/openweather.git
RUBYOPT='-I samples/truffle/interop/weather/openweather/lib -I samples/truffle/interop/weather' graalvm/bin/js samples/truffle/interop/weather/weather.js
```

The demo hangs after printing the temperature, as some Ruby service threads
get stuck and aren't shutting down properly.

The Ruby `temperature_in_city` method has a `this` parameter which it wouldn't
normally have, because JS passes `this` as the first argument.
5 changes: 5 additions & 0 deletions samples/truffle/interop/weather/weather.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Interop.eval('application/x-ruby', 'require "weather"');

temperature_in_city = Interop.import('temperature_in_city')

console.log('Temperature in New York now: ' + temperature_in_city('New York') + '℃');
15 changes: 15 additions & 0 deletions samples/truffle/interop/weather/weather.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'openweather2'

Openweather2.configure do |config|
# This is the endpoint and API key from the tests in the Openweather2 gem
config.endpoint = 'http://api.openweathermap.org/data/2.5/weather'
config.apikey = 'dd7073d18e3085d0300b6678615d904d'
end

def temperature_in_city(this, name)
name = Truffle::Interop.from_java_string(name)
weather = Openweather2.get_weather(city: name, units: 'metric')
weather.temperature
end

Truffle::Interop.export_method :temperature_in_city

2 comments on commit e64c83f

@chrisseaton
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukasstadler I've written this using JS instead of R, because I'm not as familiar with how R works. Can you translate to R? You'll need 489d4316b0d8178b75e9990d8cd729edf23dad95 of GraalVM.

@lukasstadler
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I can do the translation. thanks a lot for the example!

Please sign in to comment.