class Mtodos::Client
retrieving ToDo and send notification client
Public Class Methods
new(url, notify_url, cache_file: true, memcached_server: nil)
click to toggle source
# File lib/mtodos.rb, line 47 def initialize(url, notify_url, cache_file: true, memcached_server: nil) @url = url @notify_url = notify_url if cache_file @cache = Cache.new else initialize_memcache(memcached_server) end initialize_resource_type(url) end
Public Instance Methods
critical?(todo)
click to toggle source
# File lib/mtodos.rb, line 92 def critical?(todo) ['RC bug', 'testing auto-removal'].include?(todo[':type']) end
filter(data_array)
click to toggle source
# File lib/mtodos.rb, line 86 def filter(data_array) data_array.select do |todo| todo if critical?(todo) && !sent?(todo[':shortname']) end end
initialize_memcache(memcached_server)
click to toggle source
# File lib/mtodos.rb, line 58 def initialize_memcache(memcached_server) if memcached_server.nil? @cache = Memcached.new('localhost:11211') else @cache = Memcached.new(memcached_server) end end
initialize_resource_type(url)
click to toggle source
# File lib/mtodos.rb, line 66 def initialize_resource_type(url) if url =~ %r{://udd.debian.org/} @resouce_type = 'udd' elsif url =~ /glaneuses.json/ @resouce_type = 'glaneuses' end end
message(todo)
click to toggle source
# File lib/mtodos.rb, line 130 def message(todo) if todo[':link'] msg = format('[%s] <%s|%s>: %s', todo[':source'], todo[':link'], todo[':description'], todo[':details']) else msg = format('[%s] %s: %s', todo[':source'], todo[':description'], todo[':details']) end { username: 'Debian Dashboard check bot', icon_url: 'https://www.debian.org/logos/openlogo-nd-75.png', text: msg } end
notify(todo)
click to toggle source
# File lib/mtodos.rb, line 112 def notify(todo) pat_slack = %r{https://hooks.slack.com/services/} @notify_url =~ pat_slack && post_slack(todo) && store(todo[':shortname']) end
post_slack(todo)
click to toggle source
# File lib/mtodos.rb, line 117 def post_slack(todo) headers = { 'Content-Type' => 'application/json' } uri = URI.parse(@notify_url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true case http.post(uri.path, message(todo).to_json, headers) when Net::HTTPSuccess true else false end end
retrieve()
click to toggle source
# File lib/mtodos.rb, line 74 def retrieve json_data = JSON.parse(Net::HTTP.get(URI.parse(@url))) if @resouce_type == 'udd' data_array = json_data elsif @resouce_type == 'glaneuses' data_array = json_data['udd'] end filter(data_array).each do |todo| notify(todo) end end
sent?(key)
click to toggle source
# File lib/mtodos.rb, line 96 def sent?(key) @cache.get(key) rescue Cache::NotFound false rescue Memcached::NotFound false rescue Memcached::ServerIsMarkedDead false end
store(key)
click to toggle source
# File lib/mtodos.rb, line 106 def store(key) @cache.set(key, true) rescue Memcached::ServerIsMarkedDead => e puts e end