class GnomeCampfireNotifications

Constants

CONFIG_ATTRS
CONFIG_SYSPATH
HOST
NOTIFICATION_GFX_FILENAME
NOTIFICATION_GFX_SYSPATH
REQD_CONFIG_ATTRS

Attributes

config[R]

Public Class Methods

new() click to toggle source
# File lib/gnome-campfire-notifications.rb, line 17
def initialize
  @username_cache = []

  load_config
  try_icon unless @config[:icon_path]
end

Public Instance Methods

get_username(id) click to toggle source
# File lib/gnome-campfire-notifications.rb, line 38
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
load_config() click to toggle source
# File lib/gnome-campfire-notifications.rb, line 57
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
send_notification(item) click to toggle source
# File lib/gnome-campfire-notifications.rb, line 30
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_for_bash(item["body"])}\"")
  end
end
start() click to toggle source
# File lib/gnome-campfire-notifications.rb, line 24
def start
  on_message do |item|
    send_notification(item)
  end
end

Private Instance Methods

escape_for_bash(string) click to toggle source
# File lib/gnome-campfire-notifications.rb, line 118
def escape_for_bash(string)
  string.gsub(/("|`)/, '\\\\\\1')
end
gem_dir() click to toggle source
# File lib/gnome-campfire-notifications.rb, line 110
def gem_dir
  Gem::Specification.find_by_name("gnome-campfire-notifications").gem_dir
end
icon() click to toggle source
# File lib/gnome-campfire-notifications.rb, line 95
def icon
  " -i #{@config[:icon_path]}"
end
notification_gfx_paths() click to toggle source
# File lib/gnome-campfire-notifications.rb, line 105
def notification_gfx_paths
  [[NOTIFICATION_GFX_SYSPATH, NOTIFICATION_GFX_FILENAME],
   [gem_dir, "assets", NOTIFICATION_GFX_FILENAME]].map { |p| p.join('/') }
end
on_message() { |json| ... } click to toggle source
# File lib/gnome-campfire-notifications.rb, line 68
def on_message
  EventMachine::run do
    stream = Twitter::JSONStream.connect(
      host:  HOST,
      path: "/room/#{@config[:roomid]}/live.json",
      auth: "#{@config[:token]}:x",
    )

    stream.each_item do |item|
      json = JSON::parse(item)
      if json["type"] == "TextMessage"
        yield(json)
      end
    end

    stream.on_error { |m| puts "ERROR: #{m.inspect}" }
    stream.on_max_reconnects { |timeout, retries| puts "Tried #{retries} times to connect." }
  end
end
room_url() click to toggle source
# File lib/gnome-campfire-notifications.rb, line 114
def room_url
  "#{@config[:subdomain]}.campfirenow.com"
end
should_send?(username, body) click to toggle source
# File lib/gnome-campfire-notifications.rb, line 88
def should_send?(username, body)
  return false if @config[:self_user] && username == @config[:self_user]
  return !body.match(@config[:filter]).nil? if @config[:filter]

  true
end
try_icon() click to toggle source
# File lib/gnome-campfire-notifications.rb, line 99
def try_icon
  if path = notification_gfx_paths.detect { |p| File.exists?(p) }
    @config[:icon_path] = path
  end
end