lib/gnome-campfire-notifications.rb in gnome-campfire-notifications-0.1.3 vs lib/gnome-campfire-notifications.rb in gnome-campfire-notifications-0.2.0
- old
+ new
@@ -1,47 +1,78 @@
require "twitter/json_stream"
require "net/http"
require "json"
+require "yaml"
class GnomeCampfireNotifications
HOST = 'streaming.campfirenow.com'
NOTIFICATION_GFX_FILENAME = 'campfire.png'
NOTIFICATION_GFX_SYSPATH = "/usr/share/icons/gnome/32x32/apps"
- ATTR_MAP = {
- room_name: 'GNOME_CAMPFIRE_NOTIFICATIONS_ROOM_NAME',
- room_id: 'GNOME_CAMPFIRE_NOTIFICATIONS_ROOM_ID',
- token: 'GNOME_CAMPFIRE_NOTIFICATIONS_TOKEN'
- }
+ CONFIG_SYSPATH = "#{ENV['HOME']}/.campfire.yml"
+ CONFIG_ATTRS = %i(token subdomain roomid self_user filter icon_path)
+ REQD_CONFIG_ATTRS = %i(token subdomain roomid)
- def initialize
- if (missing = ATTR_MAP.values.select { |env| ENV[env].empty? }).any?
- raise "please set environment variable(s) #{missing.join(', ')}"
- end
+ attr_reader :config
- @options = ATTR_MAP.map.with_object({}) { |(key, env), opts| opts[key] = ENV[env] }
+ def initialize
@username_cache = []
- try_icon
+
+ load_config
+ try_icon unless @config[:icon_path]
end
def start
on_message do |item|
- username = get_username(item["user_id"].to_i)
- message = "#{item["body"].to_s.gsub(/'/, "\'")}"
+ send_notification(item)
+ end
+ end
- system("notify-send --hint=int:transient:1 -u low#{icon} '#{username}' '#{message}'")
+ def send_notification(item)
+ username = get_username(item["user_id"].to_i)
+
+ if should_send?(username, item["body"])
+ system("notify-send --hint=int:transient:1 -u low#{icon} \"#{username}\" \"#{escape_double_quotes(item["body"])}\"")
end
end
+ def get_username(id)
+ return "Unknown" if id.nil?
+
+ unless @username_cache[id]
+ req = Net::HTTP::Get.new("https://#{room_url}/users/#{id}.json")
+ req.basic_auth(@config[:token], "x")
+ http = Net::HTTP.new(room_url, 443)
+ http.use_ssl = true
+ resp = http.start { |h| h.request(req) }
+
+ json = JSON.parse(resp.body)
+
+ # Get username
+ @username_cache[id] = json["user"]["name"]
+ end
+
+ @username_cache[id]
+ end
+
+ def load_config
+ raise "please create #{CONFIG_SYSPATH}, see Github page for details" unless File.exists?(CONFIG_SYSPATH)
+ @config = YAML.load_file(CONFIG_SYSPATH).each.with_object({}) { |(k, v), h| h[k.to_sym] = v }
+
+ if !(missing = REQD_CONFIG_ATTRS.delete_if { |k| @config[k] }).empty?
+ raise "please set config option(s) in #{CONFIG_SYSPATH}: #{missing.join(', ')}"
+ end
+ end
+
private
def on_message
EventMachine::run do
stream = Twitter::JSONStream.connect(
host: HOST,
- path: "/room/#{room_id}/live.json",
- auth: "#{token}:x",
+ path: "/room/#{@config[:roomid]}/live.json",
+ auth: "#{@config[:token]}:x",
)
stream.each_item do |item|
json = JSON::parse(item)
if json["type"] == "TextMessage"
@@ -52,55 +83,39 @@
stream.on_error { |m| puts "ERROR: #{m.inspect}" }
stream.on_max_reconnects { |timeout, retries| puts "Tried #{retries} times to connect." }
end
end
- def get_username(id)
- return "Unknown" if id.nil?
+ def should_send?(username, body)
+ return false if @config[:self_user] && username == @config[:self_user]
+ return !body.match(@config[:filter]).nil? if @config[:filter]
- unless @username_cache[id]
- req = Net::HTTP::Get.new("https://#{room_url}/users/#{id}.json")
- req.basic_auth(@options[:token], "x")
- http = Net::HTTP.new(room_url, 443)
- http.use_ssl = true
- resp = http.start { |h| h.request(req) }
-
- json = JSON.parse(resp.body)
-
- # Get username
- @username_cache[id] = json["user"]["name"]
- end
-
- @username_cache[id]
+ true
end
def icon
- " -i #{@options[:icon_path]}"
+ " -i #{@config[:icon_path]}"
end
def try_icon
if path = notification_gfx_paths.detect { |p| File.exists?(p) }
- @options[:icon_path] = path
+ @config[:icon_path] = path
end
end
def notification_gfx_paths
[[NOTIFICATION_GFX_SYSPATH, NOTIFICATION_GFX_FILENAME],
- [gem_dir, "assets", NOTIFICATION_GFX_FILENAME]].join('/')
+ [gem_dir, "assets", NOTIFICATION_GFX_FILENAME]].map { |p| p.join('/') }
end
def gem_dir
Gem::Specification.find_by_name("gnome-campfire-notifications").gem_dir
end
def room_url
- "#{@options[:room_name]}.campfirenow.com"
+ "#{@config[:subdomain]}.campfirenow.com"
end
- def room_id
- @options[:room_id]
- end
-
- def token
- @options[:token]
+ def escape_double_quotes(string)
+ string.gsub(/"/, '\"')
end
end