Sha256: a4cf677d43dffa7d6be1e68c0144c26b617b65c725dcc10c44bef94bf8cee74c

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

module TwitterFriendly
  module REST
    module Users
      def verify_credentials(options = {})
        @twitter.send(__method__, {skip_status: true}.merge(options))&.to_hash
      end

      def user?(*args)
        @twitter.send(__method__, *args)
      end

      def user(*args)
        @twitter.send(__method__, *args)&.to_hash
      end

      MAX_USERS_PER_REQUEST = 100

      def users(values, options = {})
        if values.size <= MAX_USERS_PER_REQUEST
          @twitter.send(__method__, values, options)&.compact&.map(&:to_hash)
        else
          _users(values, options)
        end
      end

      def blocked_ids(*args)
        @twitter.send(__method__, *args)&.attrs&.fetch(:ids)
      end

      private

      def _users(values, options = {})
        options = {super_operation: :users, parallel: true}.merge(options)

        if options[:parallel]
          require 'parallel'

          parallel(in_threads: 10) do |batch|
            values.each_slice(MAX_USERS_PER_REQUEST) { |targets| batch.users(targets, options) }
          end.flatten
        else
          values.each_slice(MAX_USERS_PER_REQUEST).map do |targets|
            @twitter.send(:users, targets, options)
          end
        end&.flatten&.compact&.map(&:to_hash)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
twitter_friendly-0.2.1 lib/twitter_friendly/rest/users.rb
twitter_friendly-0.2.0 lib/twitter_friendly/rest/users.rb