class Beeline::Client

Constants

INITIAL_LATCH
MAX_LATCH

Attributes

commands[R]
messages[R]
session[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/beeline/client.rb, line 14
def initialize(options = {})
  @session = options[:session]
  @url = options[:url] || BEE_WS_URL
  @commands = options[:commands] || {}
  @messages = options[:messages] || {}
  @prefix = options[:prefix] || '$'
  @latch = INITIAL_LATCH
  @socket = nil
  @thread_running = false
end

Public Instance Methods

ping() click to toggle source
# File lib/beeline/client.rb, line 44
def ping
  start = Time.now
  
  puts 'Ping ... '
  
  socket.ping('Ping!') do
    puts "Pong! (#{Time.now - start})"
  end
end
reset_session() click to toggle source
# File lib/beeline/client.rb, line 25
def reset_session
  @session = nil
end
run(options = {async: false}) click to toggle source
# File lib/beeline/client.rb, line 29
def run(options = {async: false})
  async = !!options[:async]
  start_thread(async) unless !!@thread_running
  
  socket
end
run_loop() click to toggle source
# File lib/beeline/client.rb, line 36
def run_loop
  loop do
    start_thread(false) unless !!@thread_running
    
    sleep [@latch *= 2, MAX_LATCH].min
  end
end

Private Instance Methods

accept_pending_friend_requests() click to toggle source
# File lib/beeline/client.rb, line 87
def accept_pending_friend_requests
  return unless friendships[:accept] == 'auto'
  
  session.friend_requests.each do |friend_request|
    if socket.send({type: 'accept-friendship', payload: {id: friend_request['id']}}.to_json)
      puts "Accept friendship sent for #{friend_request['username']}"
    else
      puts "Could not accept friendship request: #{friend_request}"
    end
  end
end
authenticate() click to toggle source
# File lib/beeline/client.rb, line 69
def authenticate
  if result = socket.send({type: 'authenticate', payload: {username: hive_account, token: session.token}}.to_json)
    puts "Authentication sent: #{session.inspect}"
    
    ping
    
    accept_pending_friend_requests
  else
    puts "Could not authenticate."
  end
end
chat_message(conversation_id, to, message) click to toggle source
# File lib/beeline/client.rb, line 81
def chat_message(conversation_id, to, message)
  if socket.send({type: 'chat-message', payload: {conversation_id: conversation_id, to: to, message: message}}.to_json)
    puts "Reply sent to #{to}"
  end
end
process_acknowledged(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 180
def process_acknowledged(payload); end
process_chat_message(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 165
def process_chat_message(payload); end
process_conversation_created(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 171
def process_conversation_created(payload); end
process_conversation_removed(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 177
def process_conversation_removed(payload); end
process_conversation_renamed(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 174
def process_conversation_renamed(payload); end
process_friendship_accepted(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 195
def process_friendship_accepted(payload); end
process_friendship_rejected(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 204
def process_friendship_rejected(payload); end
process_friendship_removed(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 198
def process_friendship_removed(payload); end
process_friendship_requested(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 201
def process_friendship_requested(payload); end
process_member_added(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 183
def process_member_added(payload); end
process_member_removed(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 186
def process_member_removed(payload); end
process_message_deleted(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 168
def process_message_deleted(payload); end
process_moderator_added(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 189
def process_moderator_added(payload); end
process_moderator_removed(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 192
def process_moderator_removed(payload); end
process_status(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 162
def process_status(payload); end
process_user_blocked(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 207
def process_user_blocked(payload); end
process_user_unblocked(payload) click to toggle source

Override to procsss.

# File lib/beeline/client.rb, line 210
def process_user_unblocked(payload); end
socket() click to toggle source
# File lib/beeline/client.rb, line 58
def socket
  @socket ||= Faye::WebSocket::Client.new(BEE_WS_URL, [],
    extensions: [PermessageDeflate],
    headers: {
      'User-Agent' => AGENT_ID,
      'Authorization' => "Bearer #{session.token}"
    },
    ping: WS_KEEPALIVE_TIME
  )
end
start_thread(async = false) click to toggle source

@private

# File lib/beeline/client.rb, line 100
def start_thread(async = false)
  thread = Thread.new do
    EM.run {
      socket.on :open do |event|
        p [:ws, [:open]]
        authenticate
        @thread_running = true
      end

      socket.on :message do |event|
        data = JSON[event.data] rescue nil
        
        if !!data && !!data['type'] && !!data['payload']
          payload = data['payload']
          
          case data['type']
          when 'status' then process_status(payload)
          when 'reauthentication-required' then session.refresh_token
          when 'chat-message' then process_chat_message(payload)
          when 'message-deleted' then process_message_deleted(payload)
          when 'conversation-created' then process_conversation_created(payload)
          when 'conversation-renamed' then process_conversation_renamed(payload)
          when 'conversation-removed' then process_conversation_removed(payload)
          when 'acknowledged' then process_acknowledged(payload)
          when 'member-added' then process_member_added(payload)
          when 'member-removed' then process_member_removed(payload)
          when 'moderator-added' then process_moderator_added(payload)
          when 'moderator-removed' then process_moderator_removed(payload)
          when 'friendship-accepted' then process_friendship_accepted(payload)
          when 'friendship-removed' then process_friendship_removed(payload)
          when 'friendship-requested' then process_friendship_requested(payload)
          when 'friendship-rejected' then process_friendship_rejected(payload)
          when 'user-blocked' then process_user_blocked(payload)
          when 'user-unblocked' then process_user_unblocked(payload)
          else
            p [:beechat_message, data['type'], data['payload']]
          end
        else
          p [:ws, [:message, event.data]]
        end
      end
      
      socket.on :close do |event|
        p [:ws, [:close, event.code, event.reason]]
        @thread_running = false
        @socket = nil
        EM.stop
      end
      
      socket.on :error do |event|
        p [:ws, [:error, event.message.inspect]]
        @thread_running = false
        @socket = nil
        EM.stop
      end
    }
  end
  
  thread.join unless !!async
end