Class: Mxrb::CLI::Releases

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/cli/release.rb

Overview

Checks RubyGems for versions and GitHub Releases for human release notes.

Constant Summary collapse

LATEST_URI =
URI('https://rubygems.org/api/v1/versions/mxrb/latest.json')
RELEASE_URI =
'https://api.github.com/repos/lucamykael/mxrb/releases/tags/v%s'
CACHE_TTL =
6 * 60 * 60

Instance Method Summary collapse

Constructor Details

#initialize(installed: Mxrb::VERSION, cache_path: nil, clock: Time.method(:now), request: nil) ⇒ Releases

Returns a new instance of Releases.



29
30
31
32
33
34
# File 'lib/mxrb/cli/release.rb', line 29

def initialize(installed: Mxrb::VERSION, cache_path: nil, clock: Time.method(:now), request: nil)
  @installed = installed.to_s
  @cache_path = cache_path || default_cache_path
  @clock = clock
  @request = request || method(:http_get)
end

Instance Method Details

#changelog(version = nil) ⇒ Object

rubocop:disable Metrics/AbcSize



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mxrb/cli/release.rb', line 44

def changelog(version = nil) # rubocop:disable Metrics/AbcSize
  selected = version.to_s.empty? ? status(force: true).latest : version.to_s
  raise ReleaseError, 'Could not determine the latest published MXRB version' if selected.to_s.empty?

  payload = JSON.parse(@request.call(URI(format(RELEASE_URI, selected))))
  {
    version: selected, title: payload['name'] || "mxrb #{selected}",
    published_at: payload['published_at'], body: payload['body'].to_s,
    url: payload['html_url'] || "https://github.com/lucamykael/mxrb/releases/tag/v#{selected}"
  }
rescue ReleaseError, JSON::ParserError, IOError, SystemCallError, Timeout::Error, SocketError => e
  raise ReleaseError, "Could not load changelog: #{e.message}"
end

#status(force: false) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/mxrb/cli/release.rb', line 36

def status(force: false)
  payload = !force && read_cache
  payload ||= fetch_latest
  ReleaseStatus.new(@installed, payload && payload['version'], payload && payload['checked_at'])
rescue ReleaseError, JSON::ParserError, IOError, SystemCallError, Timeout::Error, SocketError
  ReleaseStatus.new(@installed, nil, nil)
end