Class: Flint::Client
- Inherits:
-
EventMachine::HttpClient
- Object
- EventMachine::HttpClient
- Flint::Client
- Defined in:
- lib/flint/client.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
Instance Method Summary collapse
- #guarded?(guard, message) ⇒ Boolean
- #handle_message(message) ⇒ Object
- #handle_ready ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #join ⇒ Object
- #listen ⇒ Object
- #process(chunk) ⇒ Object
- #register_handler(type, gaurd, &block) ⇒ Object
- #run ⇒ Object
- #say(message) ⇒ Object
- #setup(token, account, room_id) ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
5 6 7 8 9 10 11 12 |
# File 'lib/flint/client.rb', line 5 def initialize @state = :initializing @handlers = {} @token = nil @account = nil @room_id = nil @buffer = "" end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
3 4 5 |
# File 'lib/flint/client.rb', line 3 def connection @connection end |
Instance Method Details
#guarded?(guard, message) ⇒ Boolean
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/flint/client.rb', line 75 def guarded?(guard, ) guard.all? do |attribute, test| case test when String then [attribute] == test when Regexp then [attribute] =~ test when Array then test.include?([attribute]) when Proc then test.call([attribute]) else false end end end |
#handle_message(message) ⇒ Object
69 70 71 72 73 |
# File 'lib/flint/client.rb', line 69 def () Array(@handlers[:message]).each do |guard, block| block.call() if guarded?(guard, ) end end |
#handle_ready ⇒ Object
56 57 58 59 |
# File 'lib/flint/client.rb', line 56 def handle_ready Array(@handlers[:ready]).each { |_, block| block.call } @state = :ready end |
#join ⇒ Object
35 36 37 38 39 |
# File 'lib/flint/client.rb', line 35 def join headers = { "authorization" => [@token, "X"] } @connection = EventMachine::HttpRequest.new("https://#{@account}.campfirenow.com/room/#{@room_id}/join.json").post(:head => headers) @connection.headers { |headers| listen } end |
#listen ⇒ Object
41 42 43 44 45 46 |
# File 'lib/flint/client.rb', line 41 def listen headers = { "authorization" => [@token, "X"] } @connection = EventMachine::HttpRequest.new("https://streaming.campfirenow.com/room/#{@room_id}/live.json").get(:head => headers) @connection.headers { |headers| handle_ready } @connection.stream { |chunk| process(chunk) unless chunk.blank? } end |
#process(chunk) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/flint/client.rb', line 61 def process(chunk) @buffer += chunk while line = @buffer.slice!(/.*\r/) = JSON.parse(line).with_indifferent_access rescue nil () if end end |
#register_handler(type, gaurd, &block) ⇒ Object
14 15 16 17 |
# File 'lib/flint/client.rb', line 14 def register_handler(type, gaurd, &block) @handlers[type] ||= [] @handlers[type] << [gaurd, block] end |
#run ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/flint/client.rb', line 26 def run trap(:INT) { EventMachine.stop } trap(:TERM) { EventMachine.stop } EventMachine.run { join } @state = :setup end |
#say(message) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/flint/client.rb', line 48 def say() return if @state == :initializing headers = { "authorization" => [@token, "X"] } = { :message => } EventMachine::HttpRequest.new("https://#{@account}.campfirenow.com/room/#{@room_id}/speak.json").post(:head => headers, :query => ) end |
#setup(token, account, room_id) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/flint/client.rb', line 19 def setup(token, account, room_id) @token = token @account = account @room_id = room_id self end |