Class: OpenBuildServiceAPI::Binary

Inherits:
Object
  • Object
show all
Defined in:
lib/models/binary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Binary

Returns a new instance of Binary.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
# File 'lib/models/binary.rb', line 6

def initialize(params = {})
  raise ArgumentError.new('repository needs to be an instance of OpenBuildServiceAPI::Repository') unless params[:repository].is_a?(Repository)

  @name = params[:name]
  @size = Filesize.from(params[:size])
  @created_at = Time.at(params[:created_at])
  @repository = params[:repository]
  @architecture = params[:architecture]
  @package = params[:package]
  @connection = params[:connection]
end

Instance Attribute Details

#architectureObject (readonly)

Returns the value of attribute architecture.



3
4
5
# File 'lib/models/binary.rb', line 3

def architecture
  @architecture
end

#created_atObject (readonly)

Returns the value of attribute created_at.



3
4
5
# File 'lib/models/binary.rb', line 3

def created_at
  @created_at
end

#local_file_pathObject (readonly)

Returns the value of attribute local_file_path.



3
4
5
# File 'lib/models/binary.rb', line 3

def local_file_path
  @local_file_path
end

#local_pathObject (readonly)

Returns the value of attribute local_path.



3
4
5
# File 'lib/models/binary.rb', line 3

def local_path
  @local_path
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/models/binary.rb', line 3

def name
  @name
end

#packageObject

Returns the value of attribute package.



4
5
6
# File 'lib/models/binary.rb', line 4

def package
  @package
end

#repositoryObject (readonly)

Returns the value of attribute repository.



3
4
5
# File 'lib/models/binary.rb', line 3

def repository
  @repository
end

#sizeObject (readonly)

Returns the value of attribute size.



3
4
5
# File 'lib/models/binary.rb', line 3

def size
  @size
end

Instance Method Details

#delete_local_copy!Object



44
45
46
47
48
49
50
51
52
# File 'lib/models/binary.rb', line 44

def delete_local_copy!
  return false unless @local_file_path
  return false unless File.exists?(@local_file_path)

  File.delete(@local_file_path)
  FileUtils.rm_r(@local_path) if Dir.empty?(@local_path) && @tmp_dir

  true
end

#download(destination = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/models/binary.rb', line 22

def download(destination=nil)
  delete_local_copy!

  if destination
    raise InvalidDownloadDirectoryPath.new("Path '#{destination}' is not a directory.") unless File.directory?(destination)

    destination = destination[0..-2] if destination.end_with?('/')
    @tmp_dir = false
  else
    destination = Dir.mktmpdir('obs-binary-download')
    @tmp_dir = true
  end

  @local_file_path = "#{destination}/#{@name}"
  @local_path = destination

  response = @connection.send_request(:get, "/build/#{CGI.escape(@package.project.name)}/#{CGI.escape(@repository.to_s)}/" \
                                      "#{CGI.escape(@architecture)}/#{CGI.escape(@package.name)}/#{CGI.escape(@name)}")
  File.write(@local_file_path, response.body)
  @local_file_path
end

#to_sObject



18
19
20
# File 'lib/models/binary.rb', line 18

def to_s
  @name
end