Class: Twterm::Tab::RateLimitStatus

Inherits:
AbstractTab show all
Includes:
Loadable, Scrollable
Defined in:
lib/twterm/tab/rate_limit_status.rb

Constant Summary collapse

@@mutex =
Mutex.new
@@status =
nil

Instance Method Summary collapse

Methods included from Scrollable

#scroller, #total_item_count

Methods included from Loadable

#initially_loaded!, #initially_loaded?

Methods inherited from AbstractTab

#==, #find_or_fetch_list, #find_or_fetch_status, #find_or_fetch_user, #render

Methods included from Subscriber

included, #subscribe, #unsubscribe

Constructor Details

#initialize(app, client) ⇒ RateLimitStatus

Returns a new instance of RateLimitStatus.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/twterm/tab/rate_limit_status.rb', line 12

def initialize(app, client)
  super(app, client)

  scroller.set_no_cursor_mode!

  @ticker = Scheduler.new(1) { render }

  @scheduler = Scheduler.new(10) { fetch }

  fetch.then { initially_loaded! }
end

Instance Method Details

#closeObject



24
25
26
27
28
# File 'lib/twterm/tab/rate_limit_status.rb', line 24

def close
  @scheduler.kill
  @ticker.kill
  super
end

#drawable_item_countObject



30
31
32
# File 'lib/twterm/tab/rate_limit_status.rb', line 30

def drawable_item_count
  window.maxy - 2
end

#imageObject



34
35
36
37
38
39
40
41
# File 'lib/twterm/tab/rate_limit_status.rb', line 34

def image
  return Image.string('Loading...') unless initially_loaded?

  items
    .drop(scroller.offset)
    .take(drawable_item_count)
    .reduce(Image.empty, :|)
end

#itemsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/twterm/tab/rate_limit_status.rb', line 43

def items
  return [] unless initially_loaded?

  @@status[:resources].flat_map do |category, limits|
    [
      !Image.string(category.to_s.gsub('_', ' ')).color(:green),
      Image.blank_line,
      *limits.flat_map do |endpoint, data|
        limit, remaining = data[:limit], data[:remaining]
        t = Time.at(data[:reset])
        diff = (t - Time.now).round

        [
          Image.string("  #{endpoint}").color(:cyan) - (
            limit == remaining \
              ? Image.empty
              : Image.string(" (Resets #{diff.positive? ? "in #{diff} seconds" : 'soon...'})")),
          Image.string('  ') - Image.remaining_resource(remaining, limit, 50) - Image.string(" #{remaining}/#{limit}"),
          Image.blank_line,
        ]
      end,
      Image.blank_line,
    ]
  end
end

#respond_to_key(key) ⇒ Object



69
70
71
# File 'lib/twterm/tab/rate_limit_status.rb', line 69

def respond_to_key(key)
  scroller.respond_to_key(key)
end

#titleObject



73
74
75
# File 'lib/twterm/tab/rate_limit_status.rb', line 73

def title
  'Rate Limit Status'.freeze
end