Sha256: c6dbe0ab340a98c6af1fc3d561e9c1b7643196ad499e2e3a7676452a514c43d3

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

module Roko
  module Source
    module Slack
      # access slack events
      #
      # @!attribute [Slack::Web::Client] client
      class Events
        DEFAULT_MESSAGE_COUNT_PER_PAGE = 100

        # @param client [Faraday::Connection]
        def initialize(client)
          @client = client
        end

        # @param start_date [String]
        # @param end_date [String]
        def fetch(start_date, end_date)
          user = my_user
          after = Date.parse(start_date).prev_day.to_s
          before = Date.parse(end_date).to_s
          query = "from:@#{user} after:#{after} before:#{before}"
          search_all_messages(query)
        end

        private

        def my_user
          @client.auth_test.user
        end

        # @param query [String] search query
        def search_all_messages(query)
          first_page = search_messages_per_page(query, 1)
          total_page_count = first_page.pagination.page_count

          return first_page.matches if total_page_count == 1

          all_messages = first_page.matches
          (2..total_page_count).each do |page_index|
            page = search_messages_per_page(query, page_index)
            all_messages.concat page.matches
          end
          all_messages
        end

        # @param query [String] search query
        # @param page [Int] page
        # @see https://api.slack.com/methods/search.messages
        # @see https://github.com/slack-ruby/slack-ruby-client/blob/master/lib/slack/web/api/endpoints/search.rb
        def search_messages_per_page(query, page)
          @client
            .search_messages({
                               query: query,
                               page: page,
                               count: DEFAULT_MESSAGE_COUNT_PER_PAGE,
                               sort: 'timestamp'
                             })
            .messages
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
roko-0.2.1 lib/roko/source/slack/events.rb
roko-0.2.0 lib/roko/source/slack/events.rb