File: rate_limiting.rb

package info (click to toggle)
mikutter 3.8.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,544 kB
  • sloc: ruby: 20,548; sh: 99; makefile: 19
file content (53 lines) | stat: -rw-r--r-- 1,799 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-

require_relative "basic"
require_relative "utils"

# API残数
module MikuTwitter::RateLimiting

  # APIのリソース。
  # Resource = Struct.new(:limit, :remain, :reset, :endpoint)
  class Resource
    attr_reader :limit, :remain, :reset, :endpoint
    def initialize(limit, remain, reset, endpoint)
      type_strict [[limit, Numeric], [remain, Numeric], [reset, Time], [endpoint, :to_s]]
      @limit, @remain, @reset, @endpoint = limit, remain, reset.freeze, endpoint.to_s.freeze
    end

    alias [] __send__

    # 規制されているなら真
    def limit?
      remain and reset and remain <= 0 and Time.new <= reset end

    def inspect
      "#<MikuTwitter::RateLimiting::Resource #{@endpoint}:#{@remain}/#{@limit} #{@reset}>" end
    alias to_s inspect
  end

  def initialize(*args)
    super
    @api_remain = {}            # resource_name => Resource
  end

  def ratelimit(resource_name)
    type_strict resource_name => String
    @api_remain[resource_name] end

  # APIリクエスト制限の残数を返す(OAuthトークン毎)
  # _response_ にHTTPRequestを設定すると、その数が設定される
  def ratelimit_rewind(resource_name, response = nil)
    type_strict resource_name => :to_s
    resource_name = resource_name.to_s.freeze
    if response and response['X-Rate-Limit-Reset']
      time = Time.at(response['X-Rate-Limit-Reset'].to_i)
      @api_remain[resource_name] = Resource.new(response['X-Rate-Limit-Limit'].to_i,
                                                response['X-Rate-Limit-Remaining'].to_i,
                                                time,
                                                resource_name) end
    @api_remain[resource_name] end

end

class MikuTwitter; include MikuTwitter::RateLimiting end