Skip to content

Instantly share code, notes, and snippets.

@camillebaldock
Last active December 23, 2015 01:39
Show Gist options
  • Save camillebaldock/6561809 to your computer and use it in GitHub Desktop.
Save camillebaldock/6561809 to your computer and use it in GitHub Desktop.
Dashing widget to display a different gem from ruby-toolbox.com every minute

Ruby Toolbox Dashing widget

Dashing widget to display a different gem from ruby-toolbox.com every minute

Live demo:

http://camilledashing.herokuapp.com/dashboard

Usage

Put the ruby_toolbox.rb file in your /jobs folder

Create a new ruby_toolbox folder in your /widgets folder, and add the three following files to that folder:

  • ruby_toolbox.scss
  • ruby_toolbox.html
  • ruby_toolbox.coffee

Add the following two gems to your Gemfile:

gem 'nokogiri'
gem 'typhoeus'

then run:

bundle

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

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
  <div data-id="ruby_toolbox" id="ruby_toolbox" data-view="RubyToolbox" data-moreinfo="Random gems from Ruby-toolbox.com"></div>
</li>

Settings

A new gem is chosen every minute by default, but you can easily change it on line 5 of ruby_toolbox.rb.

class Dashing.RubyToolbox extends Dashing.Widget
<h1 class="title" data-bind="title"></h1>
<h3 data-bind="gem['name']"></h3>
<p class="description" data-bind="gem['description']"></p>
<p class="more-info" data-bind="moreinfo"></p>
require 'nokogiri'
require 'open-uri'
require 'typhoeus'
SCHEDULER.every '1m' do |job|
doc=Nokogiri::HTML(open("https://www.ruby-toolbox.com/categories/by_name"))
category_links = doc.css('#content ul.group_items>li>a')
hydra = Typhoeus::Hydra.new
requests = Array.new
category_links.each do |category_link|
requests.push(Typhoeus::Request.new("https://www.ruby-toolbox.com"+category_link.attr('href')))
end
requests.map { |request| hydra.queue(request) }
hydra.run
gems = Array.new
requests.each do |request|
doc = Nokogiri::HTML(request.response.body)
projects = doc.css('.project')
projects.each do |project|
gems.push({ name: project.css('.project-label').text, description: project.css('.description').text[0...300]})
end
end
random_gem = gems.sample(1).first
send_event('ruby_toolbox', gem: random_gem)
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #a61414;
$value-color: #fff;
$title-color: rgba(255, 255, 255, 0.7);
$label-color: rgba(255, 255, 255, 0.7);
$moreinfo-color: rgba(255, 255, 255, 0.7);
// ----------------------------------------------------------------------------
// Widget-list styles
// ----------------------------------------------------------------------------
#ruby_toolbox {
background-color: $background-color;
vertical-align: top;
.title {
color: $title-color;
}
h4 {
margin-bottom:40px;
}
.more-info {
color: $moreinfo-color;
}
.description {
font-size:16px;
padding-top:10px;
}
.gem_note {
font-size:14px;
text-align:left;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment