Skip to content

Instantly share code, notes, and snippets.

@and7ey
Last active March 5, 2016 19:38
Show Gist options
  • Save and7ey/6a834f517791c9aa0cb5 to your computer and use it in GitHub Desktop.
Save and7ey/6a834f517791c9aa0cb5 to your computer and use it in GitHub Desktop.
VK Group Dashing Widget

#VK Group Dashing Widget

Dashing Widget for displaying VK group members count.

##Usage

To use this widget, copy the vk_group.rb file in your /jobs folder, then copy vk_group.yml into your /config folder and setup the group_id there (the VK API method used doesn't require api_token, so just group_id is needed).

Add gem 'rest-client' to your Gemfile and run bundle. If you would like to store and display previous group members count value, add gem 'redis' also (don't forget to install the Redis itself).

To include this 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="vk-group-widget" data-view="Number" data-title="VK Group" style="background-color:#567ca4"></div>
</li>

This widget is based on Number widget, so in result you will get something like this -

sample

#!/usr/bin/env ruby
require 'rest-client'
require 'json'
require 'time'
SCHEDULER.every '1d', :first_in => 0 do |job|
begin
crashes = {}
config_file = File.dirname(File.expand_path(__FILE__)) + '/../config/vk_group.yml'
config = YAML::load(File.open(config_file))
if config["vk_group_id"]
vkGroupID = config["vk_group_id"]
vkApiUrl = "https://api.vk.com/method"
vkApiMethod = "groups.getById"
response = RestClient.get "#{vkApiUrl}/#{vkApiMethod}?group_id=#{vkGroupID}&fields=members_count,counters"
json = JSON.parse(response.to_str)
if json["response"] && json["response"][0]
responseJSON = json["response"][0]
vkGroupName = responseJSON["name"]
vkGroupScreenName = responseJSON["screen_name"]
vkGroupMembersCount = responseJSON["members_count"]
data = { current: vkGroupMembersCount, title: vkGroupScreenName, moreinfo: vkGroupName }
# try to get previous value from Redis datastore, if available
# https://github.com/Shopify/dashing/wiki/How-to:-Store-data-to-and-display-from-database
if ENV.has_key? "REDISTOGO_URL"
require 'redis'
redis_uri = URI.parse(ENV["REDISTOGO_URL"])
redis = Redis.new(:host => redis_uri.host, :port => redis_uri.port, :password => redis_uri.password)
redisKey = "vk_"+vkGroupID+"_pv"
prev_value = redis.get(redisKey).to_i # read previous value from Redis
redis.set(redisKey, vkGroupMembersCount) # save current value to Redis
if prev_value != vkGroupMembersCount
data['last'] = prev_value
end
end
send_event('vk-group-widget', data)
end
else
puts "\e[33mFor the VK Group widget to work, you need to put your group id in the config/vk_group.yml file\e[0m"
end
end
end
# both numeric and alpha-numeric values are supported (for ex., 1 or "apiclub")
vk_group_id: "apiclub"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment