Skip to content

Instantly share code, notes, and snippets.

@cmourizard
Created July 22, 2014 20:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmourizard/b4388da77dbb8530631f to your computer and use it in GitHub Desktop.
Save cmourizard/b4388da77dbb8530631f to your computer and use it in GitHub Desktop.
Tumblr widget for Dashing

##Preview

Description

Simple Dashing widget (and associated job) to display quote from Tumblr blog. Uses Tumblr's API.

##Usage

To use this widget:

  • copy tumblr.html, tumblr.coffee, and tumblr.scss files into /widgets/tumblr folder
  • copy tumblr.rb file into /jobs folder.

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

<li data-row="1" data-col="1" data-sizex="2" data-sizey="1">
  <div data-id="quote" data-view="Tumblr" data-title="Quote" data-text="Author"></div>
</li>

##Settings

You'll need to configure your Tumblr token and url to the job file:

Quote is updated every hour, but you can change that by editing the job schedule.

class Dashing.Tumblr extends Dashing.Widget
<h1 class="title" data-bind="title | raw"></h1>
<h3 class="text" data-bind="text | raw"></h3>
<p class="more-info" data-bind="moreinfo"></p>
<p class="updated-at" data-bind="updatedAtMessage"></p>
#!/usr/bin/env ruby
require 'net/http'
require 'json'
tumblrToken = "YOUR TOKEN" # your Tumblr token/API Key (http://www.tumblr.com/docs/en/api/v2#auth)
tumblrUri = "BLOG HOST NAME" # the URL of the blog on Tumblr, ex: inspire.niptech.com
SCHEDULER.every '60m', :first_in => 0 do |job|
http = Net::HTTP.new("api.tumblr.com")
response = http.request(Net::HTTP::Get.new("/v2/blog/#{tumblrUri}/info?api_key=#{tumblrToken}"))
if response.code == "200"
# Retrieve total number of posts
data = JSON.parse(response.body)
nbQuotes = data["response"]["blog"]["posts"].to_i
randomNum = Random.rand(1..nbQuotes)
# Retrieve one random post
http = Net::HTTP.new("api.tumblr.com")
response = http.request(Net::HTTP::Get.new("/v2/blog/#{tumblrUri}/posts/quote?api_key=#{tumblrToken}&offset=#{randomNum}&limit=1"))
if Net::HTTPSuccess
data = JSON.parse(response.body)
send_event('quote', { title: data["response"]["posts"][0]["text"], text: data["response"]["posts"][0]["source"], moreinfo: tumblrUri})
end
end
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #12b0c5;
$title-color: rgba(255, 255, 255, 1);
$text-color: rgba(255, 255, 255, 0.7);
$moreinfo-color: rgba(255, 255, 255, 0.7);
// ----------------------------------------------------------------------------
// Widget-tumblr styles
// ----------------------------------------------------------------------------
.widget-tumblr {
background-color: $background-color;
.title {
color: $title-color;
}
.text {
color: $text-color;
font-style: italic;
}
.more-info {
color: $moreinfo-color;
}
.updated-at {
color: rgba(255, 255, 255, 0.7);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment