Class: Mtodos::Client
- Inherits:
-
Object
- Object
- Mtodos::Client
- Defined in:
- lib/mtodos.rb
Overview
retrieving ToDo and send notification client
Instance Method Summary collapse
- #critical?(todo) ⇒ Boolean
- #filter(data_array) ⇒ Object
-
#initialize(url, notify_url, cache_file: true, memcached_server: nil) ⇒ Client
constructor
A new instance of Client.
- #initialize_memcache(memcached_server) ⇒ Object
- #initialize_resource_type(url) ⇒ Object
- #message(todo) ⇒ Object
- #notify(todo) ⇒ Object
- #post_slack(todo) ⇒ Object
- #retrieve ⇒ Object
- #sent?(key) ⇒ Boolean
- #store(key) ⇒ Object
Constructor Details
#initialize(url, notify_url, cache_file: true, memcached_server: nil) ⇒ Client
Returns a new instance of Client.
47 48 49 50 51 52 53 54 55 56 |
# 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 |
Instance Method Details
#critical?(todo) ⇒ Boolean
92 93 94 |
# File 'lib/mtodos.rb', line 92 def critical?(todo) ['RC bug', 'testing auto-removal'].include?(todo[':type']) end |
#filter(data_array) ⇒ Object
86 87 88 89 90 |
# 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) ⇒ Object
58 59 60 61 62 63 64 |
# 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) ⇒ Object
66 67 68 69 70 71 72 |
# 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) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/mtodos.rb', line 130 def (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) ⇒ Object
112 113 114 115 |
# 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) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# 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, (todo).to_json, headers) when Net::HTTPSuccess true else false end end |
#retrieve ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# 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) ⇒ Boolean
96 97 98 99 100 101 102 103 104 |
# 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) ⇒ Object
106 107 108 109 110 |
# File 'lib/mtodos.rb', line 106 def store(key) @cache.set(key, true) rescue Memcached::ServerIsMarkedDead => e puts e end |