Class: Mmi::Source::Github

Inherits:
Object
  • Object
show all
Defined in:
lib/mmi/source/github.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Github

Returns a new instance of Github.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mmi/source/github.rb', line 15

def initialize(options)
	@options = options
	
	@owner       = options['owner'      ]
	@repo        = options['repo'       ]
	@asset_id    = options['asset_id'   ]
	@release     = options['release'    ]
	@file        = options['file'       ]
	@install_dir = options['install_dir']
	@filename    = options['filename'   ]
	
	if self.owner
		if self.repo
			if self.install_dir
				if self.asset_id
					# Pass.
				else
					if self.release
						if self.file
							# Pass.
						else
							raise Mmi::MissingAttributeError, 'Missing "source.file" from asset because "source.asset_id" is not provided.'
						end
					else
						raise Mmi::MissingAttributeError, 'Missing "source.release" from asset because "source.asset_id" is not provided.'
					end
				end
			else
				raise Mmi::MissingAttributeError, 'Missing "source.install_dir" from asset.'
			end
		else
			raise Mmi::MissingAttributeError, 'Missing "source.repo" from asset.'
		end
	else
		raise Mmi::MissingAttributeError, 'Missing "source.owner" from asset.'
	end
end

Instance Attribute Details

#asset_idObject (readonly)

Returns the value of attribute asset_id.



11
12
13
# File 'lib/mmi/source/github.rb', line 11

def asset_id
  @asset_id
end

#fileObject (readonly)

Returns the value of attribute file.



13
14
15
# File 'lib/mmi/source/github.rb', line 13

def file
  @file
end

#filenameObject (readonly)

Returns the value of attribute filename.



9
10
11
# File 'lib/mmi/source/github.rb', line 9

def filename
  @filename
end

#install_dirObject (readonly)

Returns the value of attribute install_dir.



8
9
10
# File 'lib/mmi/source/github.rb', line 8

def install_dir
  @install_dir
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/mmi/source/github.rb', line 4

def options
  @options
end

#ownerObject (readonly)

Returns the value of attribute owner.



6
7
8
# File 'lib/mmi/source/github.rb', line 6

def owner
  @owner
end

#releaseObject (readonly)

Returns the value of attribute release.



12
13
14
# File 'lib/mmi/source/github.rb', line 12

def release
  @release
end

#repoObject (readonly)

Returns the value of attribute repo.



7
8
9
# File 'lib/mmi/source/github.rb', line 7

def repo
  @repo
end

Instance Method Details

#cached_asset_responseObject



57
58
59
# File 'lib/mmi/source/github.rb', line 57

def cached_asset_response
	@asset_get ||= ::Github::Client::Repos::Releases::Assets.new.get(owner: self.owner, repo: self.repo, id: self.asset_id)
end

#display_nameObject



86
87
88
# File 'lib/mmi/source/github.rb', line 86

def display_name
	repository_url
end

#download_urlObject



61
62
63
64
65
66
67
# File 'lib/mmi/source/github.rb', line 61

def download_url
	if self.asset_id
		cached_asset_response.browser_download_url
	else
		"#{repository_url}/releases/download/#{release}/#{file}"
	end
end

#install(dir) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mmi/source/github.rb', line 69

def install(dir)
	install_dir = File.expand_path(self.install_dir, dir)
	filepath    = File.join(install_dir, self.filename || (self.asset_id ? cached_asset_response.name : self.file))
	
	Mmi.info "Downloading #{download_url.inspect} into #{filepath.inspect}."
	
	FileUtils.mkdir_p(install_dir)
	
	begin
		stream = URI.open(download_url)
		
		IO.copy_stream(stream, filepath)
	rescue OpenURI::HTTPError => e
		Mmi.fail! %Q{Error when requesting asset.\n#{e.inspect}}
	end
end

#repository_urlObject



53
54
55
# File 'lib/mmi/source/github.rb', line 53

def repository_url
	"https://github.com/#{self.owner}/#{self.repo}"
end