lib/slappy/slack_api/concerns/findable.rb in slappy-0.5.2 vs lib/slappy/slack_api/concerns/findable.rb in slappy-0.6.0

- old
+ new

@@ -9,11 +9,11 @@ def initialize(data) @data = Hashie::Mash.new data end module ClassMethods - attr_reader :list_name, :api_name, :monitor_event + attr_reader :list_name, :api_name, :monitor_event, :monitor_registerd def api_name=(api_name) @api_name = api_name end @@ -24,24 +24,42 @@ def monitor_event=(target) target = [target] unless target.instance_of? Array @monitor_event = target end - def list(options = {}) + def register_monitor + return if @monitor_registerd @monitor_event.each do |event| Slappy.monitor event do @list = nil end end + @monitor_registerd = true + end + def list(options = {}) + register_monitor + unless @list - api_name = self.api_name || name.split('::').last.downcase + 's' - list_name = self.list_name || api_name method_name = "#{api_name}_list" - @list = Slack.send(method_name, options)[list_name].map { |data| new(data) } + options[:channel] = SlackAPI.find(options[:channel]).id if options[:channel] + result = Slack.send(method_name, options) + unless result['ok'] + exception = SlackError.new "Error message from slack (#{result['error']})" + fail exception, exception.message + end + @list = result[list_name].map { |data| new(data) } end @list + end + + def api_name + @api_name || name.split('::').last.downcase + 's' + end + + def list_name + @list_name || api_name end def find(arg) return find_by_keyword(arg) if arg.instance_of? Hash find id: arg