Skip to content

Instantly share code, notes, and snippets.

@humzakhatri
Last active February 13, 2021 08:30
Show Gist options
  • Save humzakhatri/f828cf626248f22120440b7d7325c1d4 to your computer and use it in GitHub Desktop.
Save humzakhatri/f828cf626248f22120440b7d7325c1d4 to your computer and use it in GitHub Desktop.
Calls an exchange rate api to fetch and display forex rates.

Description

Simple widget to display forex rates for different currencies.

##Dependencies

httparty

Add it to dashing's gemfile:

gem 'httparty'

and run bundle install. Everything should work now :)

##Usage

To use this widget, copy the forex.rb file in your /jobs folder.

To include the widget in a dashboard, add the following snippet to the dashboard layout file:

<li data-row="2" data-col="2" data-sizex="1" data-sizey="1">
  <div data-id="gbp" data-view="Number" data-title="GBP"></div>

##Settings

Add as many of these you like, but make sure to change the 'id' values to the desired exchange rate. By default we have 4, "aed", "gbp", "aud", "eur". Since the api returns a lot more, you can edit the 'forex.rb' file to add currencies. You can also change the base (USD by default) in the url.

The data is updated every 30 seconds, this can again be changed in the 'todoist_tasks.rb' file.

This uses the standard widget for displaying Numbers.

Please leave a review and your feedback below, Thank you :)

require 'httparty'
url = 'https://api.exchangerate.host/latest?base=USD'
SCHEDULER.every '30s' do
response = HTTParty.get(url)
parsed = JSON.parse(response.body)
aed = parsed['rates']['AED']
gbp = parsed['rates']['GBP']
aud = parsed['rates']['AUD']
eur = parsed['rates']['EUR']
send_event("aed", { value: aed, current: aed, last: aed })
send_event("gbp", { value: gbp, current: gbp, last: gbp })
send_event("aud", { value: aud, current: aud, last: aud })
send_event("eur", { value: eur, current: eur, last: eur })
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment