lib/socrates/adapters/memory.rb in socrates-0.1.10 vs lib/socrates/adapters/memory.rb in socrates-0.1.11

- old
+ new

@@ -1,48 +1,70 @@ require "socrates/adapters/stubs" module Socrates module Adapters class Memory + include StubUserDirectory + CLIENT_ID = "MEMORY" + CHANNEL = "C1" - attr_reader :history, :dms - attr_accessor :email, :users + attr_reader :history + attr_accessor :client_id def initialize - @history = [] - @dms = Hash.new { |hash, key| hash[key] = [] } - @users = [] + super() + @history = Hash.new { |hash, key| hash[key] = [] } end - def client_id_from_context(_context) - CLIENT_ID + def client_id_from(context: nil, user: nil) + raise ArgumentError, "Must provide one of :context or :user" if context.nil? && user.nil? + + @client_id || CLIENT_ID end - def send_message(message, *) - @history << message + def channel_from(context: nil, user: nil) + raise ArgumentError, "Must provide one of :context or :user" if context.nil? && user.nil? + + user.nil? ? CHANNEL : users_channel(user) end - def send_direct_message(message, user, *) - user = user.id if user.respond_to?(:id) + def send_message(message, channel) + raise ArgumentError, "Channel is required" unless channel.present? - @dms[user] << message + @history[channel] << message end - def last_message - @history.last + def send_direct_message(message, user) + raise ArgumentError, "User is required" unless user.present? + + @history[users_channel(user)] << message end - def add_user(id: nil, name: nil, first: nil, last: nil, email: nil) - users << User.new(id, name, Profile.new(first, last, email)) + # + # Methods for fetching messages and dms in specs... + # + + def msgs + @history[CHANNEL] end - def users_list(*) - Response.new(users) + def last_msg + msgs[-1] end - def lookup_email(*) - email + def dms(user) + @history[users_channel(user)] + end + + def last_dm(user) + dms(user)[-1] + end + + private + + def users_channel(user) + user.respond_to?(:id) ? user.id : user end end end end