Sha256: 5d884cbe59033eac44dd892c6038e9c2574c6c45c4266153e12a9bf7b2f57e3c

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

module Slappy
  module SlackAPI
    module Findable
      extend ActiveSupport::Concern
      extend Forwardable

      def_delegators :@data, :method_missing, :respond_to?

      def initialize(data)
        @data = Hashie::Mash.new data
      end

      module ClassMethods
        attr_reader :list_name, :api_name, :monitor_event

        def api_name=(api_name)
          @api_name = api_name
        end

        def list_name=(list_name)
          @list_name = list_name
        end

        def monitor_event=(target)
          target = [target] unless target.instance_of? Array
          @monitor_event = target
        end

        def list(options = {})
          @monitor_event.each do |event|
            Slappy.monitor event do
              @list = nil
            end
          end

          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) }
          end
          @list
        end

        def find(arg)
          return find_by_keyword(arg) if arg.instance_of? Hash
          find id: arg
        end

        def find_by_keyword(hash)
          hash.map { |key, value| list.find { |obj| obj.send(key) == value } }.first
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slappy-0.5.2 lib/slappy/slack_api/concerns/findable.rb
slappy-0.5.1 lib/slappy/slack_api/concerns/findable.rb
slappy-0.5.0 lib/slappy/slack_api/concerns/findable.rb