Skip to content

Instantly share code, notes, and snippets.

@greedo
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greedo/957ba26575b3f5e445dc to your computer and use it in GitHub Desktop.
Save greedo/957ba26575b3f5e445dc to your computer and use it in GitHub Desktop.

##Preview

Description

Simple Dashing widget (and associated job) to display recent Twitter data for a specific ticker symbol.

##Dependencies

twitter

Add it to dashing's gemfile:

gem 'twitter'

and run bundle install or you can require it manually

##Usage

To use this widget, copy comments.html, comments.coffee, and comments.scss into the /widgets/comments directory. Put the twitter.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="1" data-col="1" data-sizex="1" data-sizey="1">
  <div data-id="twitter_mentions" data-view="Comments" data-title="Recent Tweets"></div>
</li>

##Settings

You'll need to select the stock ticker symbol you wish to use. You can set it by editing the Twitter search query in twitter.rb

Tweets are fetched every 10 minutes, but you can change that by editing the job schedule.

class Dashing.Comments extends Dashing.Widget
@accessor 'quote', ->
"“#{@get('current_comment')?.body}”"
ready: ->
@currentIndex = 0
@commentElem = $(@node).find('.comment-container')
@nextComment()
@startCarousel()
onData: (data) ->
@currentIndex = 0
startCarousel: ->
setInterval(@nextComment, 8000)
nextComment: =>
comments = @get('comments')
if comments
@commentElem.fadeOut =>
@currentIndex = (@currentIndex + 1) % comments.length
@set 'current_comment', comments[@currentIndex]
@commentElem.fadeIn()
<h1 class="title" data-bind="title"></h1>
<div class="comment-container">
<h3><img data-bind-src='current_comment.avatar'/><span data-bind='current_comment.name' class="name"></span></h3>
<p class="comment" data-bind='quote'></p>
</div>
<p class="more-info" data-bind="moreinfo"></p>
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #eb9c3c;
$title-color: rgba(255, 255, 255, 0.7);
$moreinfo-color: rgba(255, 255, 255, 0.7);
// ----------------------------------------------------------------------------
// Widget-comment styles
// ----------------------------------------------------------------------------
.widget-comments {
background-color: $background-color;
.title {
color: $title-color;
margin-bottom: 15px;
}
.name {
padding-left: 5px;
}
.comment-container {
display: none;
}
.more-info {
color: $moreinfo-color;
}
}
require 'twitter'
client = Twitter::REST::Client.new do |config|
config.consumer_key = 'YOUR_CONSUMER_KEY'
config.consumer_secret = 'YOUR_CONSUMER_SECRET'
end
SCHEDULER.every '10m', :first_in => 0 do |job|
begin
tweet = twitter.search("$AAPL", :result_type => "recent").each.map do |tweet|
{ name: tweet.text, body: tweet.user.screenname }
end
send_event('twitter_mentions', comments: tweets)
rescue
end
end
@rogeriochaves
Copy link

The scheduler part should be:

SCHEDULER.every '10m', :first_in => 0 do |job|
  begin
    tweets = client.search("$AAPL", :result_type => "recent").each.map do |tweet|
      { name: tweet.text, body: tweet.user.screen_name }
    end
    send_event('twitter_mentions', comments: tweets)
  rescue
  end
end

@greedo
Copy link
Author

greedo commented Mar 27, 2015

yes, good catch @rogeriochaves

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment