class A4Tools::AcresClient
Attributes
connect_time[R]
connected[R]
history[R]
password[RW]
ready[R]
server_info[R]
start_time[R]
token[R]
uri[R]
username[RW]
version[RW]
Public Class Methods
new(destination, username=nil, password=nil)
click to toggle source
# File lib/acres_client.rb, line 162 def initialize(destination, username=nil, password=nil) if destination.is_a? String then @uri = URI(destination) else @uri = URI(destination[:url] + destination[:ctx] + "/json?wrap") end @username = username @password = password @history = [] case @uri.scheme when 'http', 'https' @transport = HTTPTransporter.new(@uri) when 'ws', 'wss' @transport = WebSocketTransporter.new(@uri) end passthrough(@transport) @transport.on(:message) { |trigger, message| snoop_token(message) } @transport.on(:message) { |trigger, message| add_message_to_history(:server, message) } @transport.on(:sent) { |trigger, message| add_message_to_history(:client, message) } @transport.on(:connect) { |trigger, message| @ready = true } @transport.on(:disconnect) do |trigger, message| @ready = false @start_time = nil end @connected = false @connect_time = nil @start_time = nil @authenticated = false @token = nil end
Public Instance Methods
add_message_to_history(sender, message, timestamp=nil)
click to toggle source
# File lib/acres_client.rb, line 197 def add_message_to_history(sender, message, timestamp=nil) timestamp ||= Time.now @start_time ||= timestamp history.push({ time: timestamp, sender: sender, message: (symbolify(JSON.parse(message)) rescue nil), raw:message }) end
app_info()
click to toggle source
# File lib/acres_client.rb, line 360 def app_info info = ObjectBuilder[:app_info_for_script].value info[:currentTalkVersion] = @version unless @version.nil? info end
attempt_snoop(result)
click to toggle source
# File lib/acres_client.rb, line 214 def attempt_snoop(result) if not(result.nil?) and result.has_key? :tok and result.has_key? :serverInfo then # this is a connection token @connected = true @token = result[:tok] if result.has_key? :tok @server_info = result[:serverInfo] if result.has_key? :serverInfo signal(:connect) true else false end end
authenticate(username=nil, password=nil)
click to toggle source
# File lib/acres_client.rb, line 256 def authenticate(username=nil, password=nil) return nil unless connect_if_needed request = { tok: @token, username: username || @username, password: make_password(password || @password), automaticReconnect: false } result = send_message(wrapped_message("login", "com.acres4.common.info.ClientLoginRequest", request)) response = response_body(result) @authenticated = not(response.nil?) response end
authenticate_if_needed(username=nil, password=nil)
click to toggle source
# File lib/acres_client.rb, line 272 def authenticate_if_needed(username=nil, password=nil) return true if @authenticated return true unless authenticate(username, password).nil? return false end
connect()
click to toggle source
# File lib/acres_client.rb, line 235 def connect return nil unless transport_connect request = { appInfo: app_info, deviceInfo: device_info } result = send_message(wrapped_message("connect", "com.acres4.common.info.ConnectionRequest", request)) response = response_body(result) return nil unless response response end
connect_if_needed()
click to toggle source
# File lib/acres_client.rb, line 250 def connect_if_needed return true if @connected return true unless connect.nil? return false end
device_info()
click to toggle source
# File lib/acres_client.rb, line 366 def device_info ObjectBuilder[:device_info_for_system].value end
disconnect()
click to toggle source
# File lib/acres_client.rb, line 231 def disconnect @transport.disconnect end
empty_query(method, interface=nil)
click to toggle source
# File lib/acres_client.rb, line 338 def empty_query(method, interface=nil) jsonrpc_message(method, interface) end
inject_token(message, force=false, type=nil)
click to toggle source
# File lib/acres_client.rb, line 306 def inject_token(message, force=false, type=nil) return nil if message.nil? return message unless force or message[:tok].nil? type ||= talk.guess_class(message) unless type.nil? then cls = talk.class_named(type) has_tok = (cls[:field].select do |f| is_token_class = talk.name_matches?("com.acres4.common.info.ConnectionToken", f[:type].first) f[:name] == "tok" and is_token_class end).length >= 1 message[:tok] = @token if has_tok end message end
jsonrpc_message(method, interface=nil, cls=nil, body={})
click to toggle source
# File lib/acres_client.rb, line 323 def jsonrpc_message(method, interface=nil, cls=nil, body={}) body[:__class] = (cls || talk.guess_class(body)) unless body.nil? json = { __class:"com.acres4.common.info.JSONRPCRequest", method:method, params:body, id:next_msg_id, jsonrpc:"2.0" } json[:intf] = interface unless interface.nil? # don't even include it if we didn't specify it json end
make_password(password)
click to toggle source
# File lib/acres_client.rb, line 278 def make_password(password) { algorithm:"CLEARTEXT", contents:password, parameters:[] } end
next_msg_id()
click to toggle source
# File lib/acres_client.rb, line 286 def next_msg_id @msg_id = @msg_id.nil? ? 0 : @msg_id + 1 end
response_body(result)
click to toggle source
# File lib/acres_client.rb, line 347 def response_body(result) return nil unless result.code.to_i >= 200 and result.code.to_i < 300 jsonrpc = JSON.parse(result.body) return nil unless jsonrpc["jsonrpc"] == "2.0" return nil unless jsonrpc["error"].nil? return nil if jsonrpc["result"].nil? return symbolify jsonrpc["result"]["body"] unless jsonrpc["result"]["body"].nil? return symbolify jsonrpc["result"] end
send_message(message)
click to toggle source
# File lib/acres_client.rb, line 342 def send_message(message) message = message.to_json unless message.is_a? String @transport.send_message(message) end
snoop_token(message)
click to toggle source
# File lib/acres_client.rb, line 203 def snoop_token(message) begin response = symbolify JSON.parse(message) result = response[:result] return if attempt_snoop(result) attempt_snoop(result[:body]) unless result.nil? or result[:body].nil? rescue "" end end
transport_connect()
click to toggle source
# File lib/acres_client.rb, line 227 def transport_connect @transport.connect end
wrapped_message(method, cls, body)
click to toggle source
# File lib/acres_client.rb, line 290 def wrapped_message(method, cls, body) cls ||= talk.guess_class(body) body[:__class] = cls.to_s; body[:tok] = @token unless @token.nil? wrapper = { className:cls, body:body, namedObjectEncodingType:0, serializedEncoding:nil, __class:"com.acres4.common.info.NamedObjectWrapper" } jsonrpc_message(method, nil, "com.acres4.common.info.NamedObjectWrapper", wrapper) end