Skip to content

Instantly share code, notes, and snippets.

@visibilityspots
Last active August 29, 2015 14:04
Show Gist options
  • Save visibilityspots/ae32dc897e83980e16f6 to your computer and use it in GitHub Desktop.
Save visibilityspots/ae32dc897e83980e16f6 to your computer and use it in GitHub Desktop.
A dashing widget to show the chat availability from libraryh3lp.com

Libraryh3lp widget

One of the customers I worked for is using a chat service for libraries, https://us.libraryh3lp.com. Since we wanted to see that the covering group is available during working hours I created a dahing tile for it based on the presence-api: https://docs.libraryh3lp.com/presence-api.html.

In the example there are 2 users both member of a common chat group. If one of them is online the group is online. If both are offline the group will be offline. The background color of the tile is based on the availability of the group.

In the dashing root directory you need to add those gems to the Gemfile:

gem 'net/https'
gem 'uri'

And install them using

$ bundle install

Now copy the libraryh3lp.rb file into the jobs directory and edit the GROUP and users to your preference.

Next step is to insert the html code in your dashing file dashingRootDir/dashboards/####.erb

<li data-row="2" data-col="2" data-sizex="1" data-sizey="1">
  	  <div data-id="chat_stat" data-view="List" data-unordered="true" data-title="Chat"></div>
      <i class="icon-info-sign icon-background"></i>
</li>

That way you have a visual alert when the chat group goes offline.

<li data-row="2" data-col="2" data-sizex="1" data-sizey="1">
<div data-id="chat_stat" data-view="List" data-unordered="true" data-title="Chat"></div>
<i class="icon-info-sign icon-background"></i>
</li>
require "net/https"
require "uri"
def fetch(uri_str, limit = 10)
# You should choose a better exception.
raise ArgumentError, 'too many HTTP redirects' if limit == 0
response = Net::HTTP.get_response(URI(uri_str))
case response
when Net::HTTPSuccess then
response
when Net::HTTPRedirection then
location = response['location']
warn "redirected to #{location}"
fetch(location, limit - 1)
else
response.value
end
end
SCHEDULER.every '4s' do
group_req = fetch('http://uslibraryh3lp.com/presence/jid/GROUP/chat.libraryh3lp.com/text')
group = group_req.body
user1_req = fetch('http://uslibraryh3lp.com/presence/jid/USER1/chat.libraryh3lp.com/text')
user1 = user1_req.body
user2_req = fetch('http://us.libraryh3lp.com/presence/jid/USER2/libraryh3lp.com/text')
user2 = user2_req.body
chat_status = []
chat_status << {:label=>'Group', :value=>group}
chat_status << {:label=>'User1', :value=>user1}
chat_status << {:label=>'User2', :value=>user2}
if group == 'away'
color = 'orange'
elsif group == 'unavailable'
color = 'red'
else
color = 'green'
end
send_event('chat_stat', { items: chat_status, color: color})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment