Skip to content
Kward Search API index

Class: Kward::UpdateCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/update_check.rb

Overview

Cached RubyGems update check for the interactive startup screen.

Defined Under Namespace

Classes: Notice

Constant Summary collapse

CHECK_INTERVAL_SECONDS =
24 * 60 * 60
LATEST_VERSION_URL =
URI("https://rubygems.org/api/v1/versions/kward/latest.json")

Instance Method Summary collapse

Constructor Details

#initialize(current_version:, config: ConfigFiles.read_config, path: ConfigFiles.update_check_cache_path, now: Time.now) ⇒ UpdateCheck

Returns a new instance of UpdateCheck.



16
17
18
19
20
21
# File 'lib/kward/update_check.rb', line 16

def initialize(current_version:, config: ConfigFiles.read_config, path: ConfigFiles.update_check_cache_path, now: Time.now)
  @current_version = current_version.to_s
  @config = config
  @path = path
  @now = now
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
# File 'lib/kward/update_check.rb', line 23

def enabled?
  return false if disabled_by_environment?

  updates = @config["updates"]
  return false if updates.is_a?(Hash) && updates["check"] == false

  true
end

#notice(refresh: false) ⇒ Object



32
33
34
35
36
37
# File 'lib/kward/update_check.rb', line 32

def notice(refresh: false)
  return nil unless enabled?

  refresh_if_stale if refresh
  notice_from_cache
end

#refreshObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/kward/update_check.rb', line 46

def refresh
  latest_version = fetch_latest_version
  write_cache(cache.merge(
    "checked_at" => @now.utc.iso8601,
    "latest_version" => latest_version
  ))
  true
rescue StandardError
  false
end

#refresh_if_staleObject



39
40
41
42
43
44
# File 'lib/kward/update_check.rb', line 39

def refresh_if_stale
  return false unless enabled?
  return false unless stale?

  refresh
end