Sha256: d424f1d4152c55c5a033650f4579385605b6da537213d510b38fb2ea475434ad

Contents?: true

Size: 1.9 KB

Versions: 11

Compression:

Stored size: 1.9 KB

Contents

module TwitterFriendly
  module RateLimit
    def rate_limit
      RateLimit.new(perform_get('/1.1/application/rate_limit_status.json'))
    rescue => e
      logger.warn e.inspect
      nil
    end

    private

    def perform_get(*args)
      internal_client.send(:perform_get, *args)
    end

    class RateLimit
      def initialize(status)
        @status = status
      end

      def resources
        @status[:resources]
      end

      def verify_credentials
        extract_remaining_and_reset_in(resources[:account][:'/account/verify_credentials'])
      end

      def friend_ids
        extract_remaining_and_reset_in(resources[:friends][:'/friends/ids'])
      end

      def follower_ids
        extract_remaining_and_reset_in(resources[:followers][:'/followers/ids'])
      end

      def users
        extract_remaining_and_reset_in(resources[:users][:'/users/lookup'])
      end

      def friends(parallel: true)
        if parallel
          {friend_ids: friend_ids, users: users}
        else
          extract_remaining_and_reset_in(resources[:friends][:'/friends/list'])
        end
      end

      def followers(parallel: true)
        if parallel
          {follower_ids: follower_ids, users: users}
        else
          extract_remaining_and_reset_in(resources[:followers][:'/followers/list'])
        end
      end

      def to_h
        {
          verify_credentials: verify_credentials,
          friend_ids: friend_ids,
          follower_ids: follower_ids,
          users: users
        }
      end

      def inspect
        'verify_credentials ' + verify_credentials.inspect +
          ' friend_ids ' + friend_ids.inspect +
          ' follower_ids ' + follower_ids.inspect +
          ' users ' + users.inspect
      end

      private

      def extract_remaining_and_reset_in(limit)
        {remaining: limit[:remaining], reset_in: (Time.at(limit[:reset]) - Time.now).round}
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
twitter_friendly-2.0.1.pre.alpha lib/twitter_friendly/rate_limit.rb
twitter_friendly-2.0.0.pre.alpha lib/twitter_friendly/rate_limit.rb
twitter_friendly-1.2.3 lib/twitter_friendly/rate_limit.rb
twitter_friendly-1.2.2 lib/twitter_friendly/rate_limit.rb
twitter_friendly-1.2.1 lib/twitter_friendly/rate_limit.rb
twitter_friendly-1.2.0 lib/twitter_friendly/rate_limit.rb
twitter_friendly-1.1.0 lib/twitter_friendly/rate_limit.rb
twitter_friendly-1.0.0 lib/twitter_friendly/rate_limit.rb
twitter_friendly-0.3.0 lib/twitter_friendly/rate_limit.rb
twitter_friendly-0.2.1 lib/twitter_friendly/rate_limit.rb
twitter_friendly-0.2.0 lib/twitter_friendly/rate_limit.rb