Class: RubynCode::CLI::VersionCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyn_code/cli/version_check.rb

Overview

Non-blocking version check against RubyGems. Runs in a background thread so it never delays startup. Caches the result for 24 hours to avoid hammering the API.

Constant Summary collapse

RUBYGEMS_API =
'https://rubygems.org/api/v1/versions/rubyn-code/latest.json'
CACHE_FILE =
File.join(Config::Defaults::HOME_DIR, '.version_check')
CACHE_TTL =

24 hours

86_400

Instance Method Summary collapse

Constructor Details

#initialize(renderer:) ⇒ VersionCheck

Returns a new instance of VersionCheck.



15
16
17
18
19
# File 'lib/rubyn_code/cli/version_check.rb', line 15

def initialize(renderer:)
  @renderer = renderer
  @thread = nil
  @notified = false
end

Instance Method Details

#notify(timeout: 0) ⇒ Object

Prints a message if outdated. Non-blocking by default: if the check hasn’t finished yet, returns immediately so it can be retried before the next prompt. Notifies at most once.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubyn_code/cli/version_check.rb', line 32

def notify(timeout: 0)
  return if @notified
  return unless @thread

  @thread.join(timeout)
  return if @thread.alive?

  @notified = true
  return unless @result

  return unless newer?(@result, RubynCode::VERSION)

  @renderer.warning(
    "Update available: #{RubynCode::VERSION} -> #{@result}  " \
    '(gem install rubyn-code)'
  )
end

#startObject

Kicks off a background check. Call ‘notify` later to display results.



22
23
24
25
26
27
# File 'lib/rubyn_code/cli/version_check.rb', line 22

def start
  return if ENV['RUBYN_NO_UPDATE_CHECK']

  @thread = Thread.new { check }
  @thread.abort_on_exception = false
end