Class: SudachiInstaller::Releases

Inherits:
Object
  • Object
show all
Defined in:
lib/sudachi-installer/releases.rb

Overview

GitHub releases

Defined Under Namespace

Classes: AssetsAmbiguous, ReleaseNotExist

Instance Method Summary collapse

Constructor Details

#initialize(httpclient: Faraday) ⇒ Releases

Returns a new instance of Releases.



13
14
15
16
# File 'lib/sudachi-installer/releases.rb', line 13

def initialize(httpclient: Faraday)
  @octokit = Octokit::Client.new
  @http = httpclient
end

Instance Method Details

#compact_asset(asset) ⇒ Hash

Parameters:

  • asset (Hash)

Returns:

  • (Hash)


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sudachi-installer/releases.rb', line 47

def compact_asset(asset)
  {
    url: asset[:url],
    name: asset[:name],
    content_type: asset[:content_type],
    created_at: asset[:created_at],
    updated_at: asset[:updated_at],
    browser_download_url: asset[:browser_download_url],
    body: asset[:body]
  }
end

#compact_release(release) ⇒ Hash

Parameters:

  • release (Hash)

Returns:

  • (Hash)


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sudachi-installer/releases.rb', line 29

def compact_release(release)
  {
    id: release[:id],
    name: release[:name],
    tag_name: release[:tag_name],
    url: release[:url],
    html_url: release[:html_url],
    target_commitish: release[:target_commitish],
    created_at: release[:created_at],
    published_at: release[:published_at],
    assets: release[:assets].map { |asset| compact_asset(asset) }
  }
end

#download(tag) ⇒ void

This method returns an undefined value.

Parameters:

  • tag (String)

Raises:



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/sudachi-installer/releases.rb', line 146

def download(tag)
  rel = version(tag, compact: true)
  assets = rel[:assets]

  raise AssetsAmbiguous.new(tag) if assets.size > 1

  Downloader.new.download(
    assets.first[:browser_download_url],
    assets.first[:name],
    SudachiInstaller.config.jar_dir
  )
end

#fetchObject

fetch releases



70
71
72
73
74
75
# File 'lib/sudachi-installer/releases.rb', line 70

def fetch
  if !@releases
    prefetch
    @releases = @endpoint.get.data
  end
end

#latest(compact: true) ⇒ Hash

Parameters:

  • compact (boolean) (defaults to: true)

Returns:

  • (Hash)


111
112
113
114
115
116
117
118
119
120
# File 'lib/sudachi-installer/releases.rb', line 111

def latest(compact: true)
  prefetch
  release = @octokit.get(@endpoint.href + "/latest")

  if compact
    compact_release(release)
  else
    release
  end
end

#list(type: "compact") ⇒ Array

Returns:

  • (Array)


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sudachi-installer/releases.rb', line 80

def list(type: "compact")
  fetch

  case type
  when "minimum"
    minimum_list
  when "compact"
    @releases.map { |release| compact_release(release) }
  else
    @releases
  end
end

#minimum_listArray

Returns:

  • (Array)


96
97
98
99
100
101
102
103
104
105
# File 'lib/sudachi-installer/releases.rb', line 96

def minimum_list
  fetch

  @releases.map { |release|
    {
      id: release[:id],
      tag_name: release[:tag_name]
    }
  }
end

#prefetchObject

setup octokit



62
63
64
65
# File 'lib/sudachi-installer/releases.rb', line 62

def prefetch
  @repo = @octokit.repo(repo)
  @endpoint = @repo.rels[:releases]
end

#repostring

Returns:

  • (string)


21
22
23
# File 'lib/sudachi-installer/releases.rb', line 21

def repo
  "WorksApplications/Sudachi"
end

#version(tag, compact: false) ⇒ <Hash, nil>

Parameters:

  • tag (String)
  • compact (boolean) (defaults to: false)

Returns:

  • (<Hash, nil>)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/sudachi-installer/releases.rb', line 127

def version(tag, compact: false)
  release = minimum_list.find { |r| r[:tag_name] == tag }

  if release
    release_info = @octokit.get(@endpoint.href + "/#{release[:id]}")
    if compact
      compact_release(release_info)
    else
      release_info
    end
  else
    raise ReleaseNotExist.new(tag)
  end
end