Class: GnomeCampfireNotifications

Inherits:
Object
  • Object
show all
Defined in:
lib/gnome-campfire-notifications.rb

Constant Summary collapse

HOST =
'streaming.campfirenow.com'
NOTIFICATION_GFX_FILENAME =
'campfire.png'
NOTIFICATION_GFX_SYSPATH =
"/usr/share/icons/gnome/32x32/apps"
CONFIG_SYSPATH =
"#{ENV['HOME']}/.campfire.yml"
CONFIG_ATTRS =
%i(token subdomain roomid self_user filter icon_path)
REQD_CONFIG_ATTRS =
%i(token subdomain roomid)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGnomeCampfireNotifications

Returns a new instance of GnomeCampfireNotifications.



17
18
19
20
21
22
# File 'lib/gnome-campfire-notifications.rb', line 17

def initialize
  @username_cache = []

  load_config
  try_icon unless @config[:icon_path]
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/gnome-campfire-notifications.rb', line 15

def config
  @config
end

Instance Method Details

#get_username(id) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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_configObject



57
58
59
60
61
62
63
64
# 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) ⇒ Object



30
31
32
33
34
35
36
# 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

#startObject



24
25
26
27
28
# File 'lib/gnome-campfire-notifications.rb', line 24

def start
  on_message do |item|
    send_notification(item)
  end
end