Skip to content

Instantly share code, notes, and snippets.

@mehdicopter
Forked from Skeyelab/btcprice.coffee
Last active August 13, 2018 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehdicopter/0c55127fa4b3b4627d95f47cee73cfb7 to your computer and use it in GitHub Desktop.
Save mehdicopter/0c55127fa4b3b4627d95f47cee73cfb7 to your computer and use it in GitHub Desktop.

This pulls a BTC spot rate from GDAX.

#Usage

smashing install 0c55127fa4b3b4627d95f47cee73cfb7

Copy this into your dashboard

    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
      <div data-id="btcprice" data-view="Btcprice" data-btcprice="" data-suffix="€"></div>
    </li>
class Dashing.Btcprice extends Dashing.Widget
@accessor 'value', Dashing.AnimatedValue
@accessor 'btcprice', ->
if @get('value')
price = parseFloat(@get('value'))
<img src="http://logok.org/wp-content/uploads/2016/10/Bitcoin-Logo-640x480.png" class="btc_logo" alt="" style="">
<h3 data-bind="btcprice | append suffix" class="price value"></h3>
<p class="updated-at" data-bind="updatedAtMessage"></p>
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #3498db;
$title-color: rgba(255, 255, 255, 0.7);
$moreinfo-color: rgba(255, 255, 255, 0.7);
// ----------------------------------------------------------------------------
// Widget-text styles
// ----------------------------------------------------------------------------
.widget-btcprice {
background-color: $background-color;
.title {
color: $title-color;
}
.btc_logo {
position: relative; /* Declared position allows for location changes */
top: -20px; /* Moves the image 2px closer to the top of the page */
}
.more-info {
color: $moreinfo-color;
}
.updated-at {
color: rgba(255, 255, 255, 0.7);
}
.price {
font-size: 3em;
position: relative; /* Declared position allows for location changes */
bottom: 38px; /* Moves the image 2px closer to the top of the page */
}
}
require 'net/http'
require 'json'
require 'uri'
SCHEDULER.every '1m', allow_overlapping: false do
uri = URI.parse('https://api.coinbase.com/v2/prices/BTC-EUR/spot')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
json_response = JSON.parse(response.body)
btc_price = json_response['data']['amount']
btc_price = '%.2f' % btc_price.delete(',').to_f
send_event('btcprice', { value: btc_price.to_f} )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment