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.



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

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

Instance Attribute Details

Returns the value of attribute cookie.



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

def cookie
  @cookie
end

#identification_errorObject (readonly)

Returns the value of attribute identification_error.



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

def identification_error
  @identification_error
end

#last_errorObject (readonly)

Returns the value of attribute last_error.



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

def last_error
  @last_error
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

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



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

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

#connectObject



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

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

Returns:

  • (Boolean)


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

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

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



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

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



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

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

#lastlog(room) ⇒ Object



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

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

#names(room) ⇒ Object



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

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

#part(room) ⇒ Object



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

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

#pingObject



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

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

#recent_messages(room) ⇒ Object



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

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

#room_update_timesObject



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

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

#roomsObject



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

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

#search(phrase, room) ⇒ Object



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

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

#send_json(h, get_results = true) ⇒ Object



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

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



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

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

#send_quit(name) ⇒ Object



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

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