Class: JsChat::Bridge

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

Overview

todo: can this be async and allow the server to have multiple threads?

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookie = nil) ⇒ Bridge

Returns a new instance of Bridge.



74
75
76
# File 'lib/jschat/http/jschat.rb', line 74

def initialize(cookie = nil)
  @cookie = cookie
end

Instance Attribute Details

Returns the value of attribute cookie.



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

def cookie
  @cookie
end

#identification_errorObject (readonly)

Returns the value of attribute identification_error.



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

def identification_error
  @identification_error
end

#last_errorObject (readonly)

Returns the value of attribute last_error.



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

def last_error
  @last_error
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
# File 'lib/jschat/http/jschat.rb', line 131

def active?
  return false unless cookie_set?
  response = ping
  if response.nil? or response['display'] == 'error'
    @last_error = response
    false
  else
    true
  end
end

#change(change_type, data) ⇒ Object



146
147
148
# File 'lib/jschat/http/jschat.rb', line 146

def change(change_type, data)
  send_json({ 'change' => change_type, change_type => data })
end

#connectObject



82
83
84
85
# File 'lib/jschat/http/jschat.rb', line 82

def connect
  response = send_json({ :protocol => 'stateless' })
  @cookie = response['cookie']
end

Returns:

  • (Boolean)


78
79
80
# File 'lib/jschat/http/jschat.rb', line 78

def cookie_set?
  !(@cookie.nil? or @cookie.empty?)
end

#identify(name, ip, session_length = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/jschat/http/jschat.rb', line 87

def identify(name, ip, session_length = nil)
  response = send_json({ :identify => name, :ip => ip, :session_length => session_length })
  if response['display'] == 'error'
    @identification_error = response
    false
  else
    true
  end
end

#join(room) ⇒ Object



119
120
121
# File 'lib/jschat/http/jschat.rb', line 119

def join(room)
  send_json({ :join => room }, false)
end

#lastlog(room) ⇒ Object



101
102
103
104
# File 'lib/jschat/http/jschat.rb', line 101

def lastlog(room)
  response = send_json({ :lastlog => room })
  response['messages']
end

#names(room) ⇒ Object



150
151
152
# File 'lib/jschat/http/jschat.rb', line 150

def names(room)
  send_json({'names' => room})
end

#part(room) ⇒ Object



123
124
125
# File 'lib/jschat/http/jschat.rb', line 123

def part(room)
  send_json({ :part => room })
end

#pingObject



142
143
144
# File 'lib/jschat/http/jschat.rb', line 142

def ping
  send_json({ 'ping' => Time.now.utc })
end

#recent_messages(room) ⇒ Object



111
112
113
# File 'lib/jschat/http/jschat.rb', line 111

def recent_messages(room)
  send_json({ 'since' => room })['messages']
end

#room_update_timesObject



115
116
117
# File 'lib/jschat/http/jschat.rb', line 115

def room_update_times
  send_json({ 'times' => 'all' })
end

#roomsObject



97
98
99
# File 'lib/jschat/http/jschat.rb', line 97

def rooms
  send_json({ :list => 'rooms' })
end

#search(phrase, room) ⇒ Object



106
107
108
109
# File 'lib/jschat/http/jschat.rb', line 106

def search(phrase, room)
  response = send_json({ :search => phrase, :room => room })
  response['messages']
end

#send_json(h, get_results = true) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/jschat/http/jschat.rb', line 158

def send_json(h, get_results = true)
  response = nil
  h[:cookie] = @cookie if cookie_set?
  c = TCPSocket.open(ServerConfig['ip'], ServerConfig['port'])
  c.send(h.to_json + "\n", 0)
  if get_results
    response = c.gets
    response = JSON.parse(response)
  end
ensure
  c.close
  response
end

#send_message(message, to) ⇒ Object



127
128
129
# File 'lib/jschat/http/jschat.rb', line 127

def send_message(message, to)
  send_json({ :send => message, :to => to }, false)
end

#send_quit(name) ⇒ Object



154
155
156
# File 'lib/jschat/http/jschat.rb', line 154

def send_quit(name)
  send_json({'quit' => name })
end