Class: Flint::Client

Inherits:
EventMachine::HttpClient
  • Object
show all
Defined in:
lib/flint/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

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

#connectionObject

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

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
# File 'lib/flint/client.rb', line 75

def guarded?(guard, message)
  guard.all? do |attribute, test|
    case test
    when String then message[attribute] == test
    when Regexp then message[attribute] =~ test
    when Array  then test.include?(message[attribute])
    when Proc   then test.call(message[attribute])
    else false
    end
  end
end

#handle_message(message) ⇒ Object



69
70
71
72
73
# File 'lib/flint/client.rb', line 69

def handle_message(message)
  Array(@handlers[:message]).each do |guard, block|
    block.call(message) if guarded?(guard, message)
  end
end

#handle_readyObject



56
57
58
59
# File 'lib/flint/client.rb', line 56

def handle_ready
  Array(@handlers[:ready]).each { |_, block| block.call }
  @state = :ready
end

#joinObject



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

#listenObject



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/)
    message = JSON.parse(line).with_indifferent_access rescue nil
    handle_message(message) if message
  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

#runObject



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(message)
  return if @state == :initializing
  
  headers = { "authorization" => [@token, "X"] }
  message = { :message => message }
  EventMachine::HttpRequest.new("https://#{@account}.campfirenow.com/room/#{@room_id}/speak.json").post(:head => headers, :query => message)
end

#setup(token, account, room_id) ⇒ Object



19
20
21
22
23
24
# File 'lib/flint/client.rb', line 19

def setup(token, , room_id)
  @token   = token
  @account = 
  @room_id = room_id
  self
end