Class: Growlfire::Client
- Inherits:
-
Object
- Object
- Growlfire::Client
- Defined in:
- lib/growlfire.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#room_name ⇒ Object
readonly
Returns the value of attribute room_name.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #growl(title, body) ⇒ Object
- #handle_message(message) ⇒ Object
-
#initialize(host, token, room_name, options = {}) ⇒ Client
constructor
A new instance of Client.
- #request(uri) ⇒ Object
- #room_id ⇒ Object
- #room_list_uri ⇒ Object
- #room_uri ⇒ Object
- #run ⇒ Object
- #stop! ⇒ Object
- #user_name(user_id) ⇒ Object
- #user_uri(user_id) ⇒ Object
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, = {}) @host, @token, @room_name = host, token, room_name @growl = Growl.new(.fetch(:host) { 'localhost' }, 'Growlfire') @growl.add_notification('growlfire', 'Growlfire', Growlfire.icon) @log = .fetch(:log) { Logger.new(STDOUT) } @log.level = Growlfire.debug? ? Logger::DEBUG : Logger::FATAL @user_names = {} end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
11 12 13 |
# File 'lib/growlfire.rb', line 11 def host @host end |
#room_name ⇒ Object (readonly)
Returns the value of attribute room_name.
11 12 13 |
# File 'lib/growlfire.rb', line 11 def room_name @room_name end |
#token ⇒ Object (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 () @log.debug "Stream: #{.inspect}" if ['type'] == 'TextMessage' growl user_name(['user_id']), ['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_id ⇒ Object
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_uri ⇒ Object
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_uri ⇒ Object
76 77 78 |
# File 'lib/growlfire.rb', line 76 def room_uri URI.parse("https://#{token}:[email protected]/room/#{room_id}/live.json") end |
#run ⇒ Object
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 {|| ()} 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 |