Class: Growlfire::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/growlfire.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, token, room_name, options = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
# File 'lib/growlfire.rb', line 13

def initialize(host, token, room_name, options = {})
  @host, @token, @room_name = host, token, room_name
  @growl = Growl.new(options.fetch(:host) { 'localhost' }, 'Growlfire')
  @growl.add_notification('growlfire', 'Growlfire', Growlfire.icon)
  @log = options.fetch(:log) { Logger.new(STDOUT) }
  @log.level = Growlfire.debug? ? Logger::DEBUG : Logger::FATAL
  @user_names = {}
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



11
12
13
# File 'lib/growlfire.rb', line 11

def host
  @host
end

#room_nameObject (readonly)

Returns the value of attribute room_name.



11
12
13
# File 'lib/growlfire.rb', line 11

def room_name
  @room_name
end

#tokenObject (readonly)

Returns the value of attribute token.



11
12
13
# File 'lib/growlfire.rb', line 11

def token
  @token
end

Instance Method Details

#growl(title, body) ⇒ Object



41
42
43
# File 'lib/growlfire.rb', line 41

def growl(title, body)
  @growl.notify 'growlfire', title, body
end

#handle_message(message) ⇒ Object



34
35
36
37
38
39
# File 'lib/growlfire.rb', line 34

def handle_message(message)
  @log.debug "Stream: #{message.inspect}"
  if message['type'] == 'TextMessage'
    growl user_name(message['user_id']), message['body']
  end
end

#request(uri) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/growlfire.rb', line 84

def request(uri)
 Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
   request = Net::HTTP::Get.new uri.request_uri
   request.basic_auth uri.user, uri.password
   response = http.request request
   response.body
 end
end

#room_idObject



52
53
54
55
56
57
58
59
60
# File 'lib/growlfire.rb', line 52

def room_id
  @room_id ||= begin
    rooms = Yajl.load request room_list_uri
    @log.debug "Rooms: #{rooms.inspect}"
    room = rooms['rooms'].find{|r| r['name'] =~ /^#{room_name}$/i}
    raise 'Could not find room!' unless room
    room['id']
  end
end

#room_list_uriObject



80
81
82
# File 'lib/growlfire.rb', line 80

def room_list_uri
  URI.parse("https://#{token}:x@#{host}.campfirenow.com/rooms.json")
end

#room_uriObject



76
77
78
# File 'lib/growlfire.rb', line 76

def room_uri
  URI.parse("https://#{token}:[email protected]/room/#{room_id}/live.json")
end

#runObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/growlfire.rb', line 22

def run
  parser = Yajl::Parser.new
  parser.on_parse_complete = Proc.new {|message| handle_message(message)}

  EventMachine.run do
    opts = {inactivity_timeout: 0, connection_timeout: 0, keepalive: true}
    http = EventMachine::HttpRequest.new(room_uri).get(opts)
    http.stream {|chunk| parser << chunk }
    http.errback { stop! }
  end
end

#stop!Object



45
46
47
48
49
50
# File 'lib/growlfire.rb', line 45

def stop!
  EventMachine.stop
  growl 'Growlfire stopped', 'Growlfire shutting down.'
  @log.fatal 'Growlfire stopped.'
  exit
end

#user_name(user_id) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/growlfire.rb', line 62

def user_name(user_id)
  @user_names[user_id] ||= begin
    user = Yajl.load request user_uri user_id
    @log.debug "User: #{user.inspect}"
    user['user']['name']
  rescue
    user_id.to_s
  end
end

#user_uri(user_id) ⇒ Object



72
73
74
# File 'lib/growlfire.rb', line 72

def user_uri(user_id)
  URI.parse("https://#{token}:x@#{host}.campfirenow.com/users/#{user_id}.json")
end