Class: Aptly::Repository

Inherits:
Representation show all
Includes:
Publishable
Defined in:
lib/aptly/repository.rb

Overview

Aptly repository representation.

Instance Attribute Summary

Attributes inherited from Representation

#connection

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Publishable

#published?, #published_in

Methods inherited from Representation

#initialize

Constructor Details

This class inherits a constructor from Aptly::Representation

Class Method Details

.create(name, connection = Connection.new, **kwords) ⇒ Repository

Creates a new Aptly::Repository

Parameters:

  • name (String)

    name fo the repository

  • connection (Connection) (defaults to: Connection.new)

    connection to use for the instance

Returns:


152
153
154
155
156
157
158
# File 'lib/aptly/repository.rb', line 152

def create(name, connection = Connection.new, **kwords)
  options = kwords.merge(name: name)
  options = options.map { |k, v| [k.to_s.capitalize, v] }.to_h
  response = connection.send(:post, '/repos',
                             body: JSON.generate(options))
  new(connection, JSON.parse(response.body))
end

.exist?(name, connection = Connection.new, **kwords) ⇒ Boolean

Check if a repository exists.

Parameters:

  • name (String)

    the name of the repository which might exist

  • connection (Connection) (defaults to: Connection.new)

    connection to use for the instance

Returns:

  • (Boolean)

    whether or not the repository exists


164
165
166
167
168
169
# File 'lib/aptly/repository.rb', line 164

def exist?(name, connection = Connection.new, **kwords)
  get(name, connection, **kwords)
  true
rescue Aptly::Errors::NotFoundError
  false
end

.get(name, connection = Connection.new, **kwords) ⇒ Repository

Get a Aptly::Repository instance if the repository already exists.

Parameters:

  • name (String)

    name of the repository

  • connection (Connection) (defaults to: Connection.new)

    connection to use for the instance

Returns:


141
142
143
144
145
146
# File 'lib/aptly/repository.rb', line 141

def get(name, connection = Connection.new, **kwords)
  kwords = kwords.map { |k, v| [k.to_s.capitalize, v] }.to_h
  response = connection.send(:get, "/repos/#{name}",
                             query: kwords)
  new(connection, JSON.parse(response.body))
end

.list(connection = Connection.new, **kwords) ⇒ Array<Repository>

List all known repositories.

Parameters:

  • connection (Connection) (defaults to: Connection.new)

    connection to use for the instance

Returns:


174
175
176
177
# File 'lib/aptly/repository.rb', line 174

def list(connection = Connection.new, **kwords)
  response = connection.send(:get, '/repos', query: kwords)
  JSON.parse(response.body).collect { |r| new(connection, r) }
end

Instance Method Details

#add_file(path, **kwords) ⇒ Hash

Add a previously uploaded file to the Repository. FIXME: this should be called file

Returns:

  • (Hash)

    report data as specified in the API.


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/aptly/repository.rb', line 43

def add_file(path, **kwords)
  # Don't mangle query, the file API is inconsistently using camelCase
  # rather than CamelCase.
  response = connection.send(:post, "/repos/#{self.Name}/file/#{path}",
                             query: kwords,
                             query_mangle: false)
  hash = JSON.parse(response.body)
  error = Errors::RepositoryFileError.from_hash(hash)
  raise error if error
  hash['Report']['Added']
end

#add_package(packages, **kwords) ⇒ Object Also known as: add_packages

Add a package (by key) to the repository.

Parameters:

  • packages (Array<String>, String)

    a list of package keys or a single package key to add to the repository. The package key(s) must already be in the aptly database.


76
77
78
79
80
81
# File 'lib/aptly/repository.rb', line 76

def add_package(packages, **kwords)
  connection.send(:post, "/repos/#{self.Name}/packages",
                  query: kwords,
                  body: JSON.generate(PackageRefs: [*packages]))
  self
end

#delete!(**kwords) ⇒ nil Also known as: delete

Delete this repository.

Returns:

  • (nil)

    always returns nil


33
34
35
36
# File 'lib/aptly/repository.rb', line 33

def delete!(**kwords)
  connection.send(:delete, "/repos/#{self.Name}", query: kwords)
  nil
end

#delete_package(packages, **kwords) ⇒ Object Also known as: delete_packages

Deletes a package (by key) from the repository.

Parameters:

  • packages (Array<String>, String)

    a list of package keys or a single package key to add to the repository. The package key(s) must already be in the aptly database.


88
89
90
91
92
93
# File 'lib/aptly/repository.rb', line 88

def delete_package(packages, **kwords)
  connection.send(:delete, "/repos/#{self.Name}/packages",
                  query: kwords,
                  body: JSON.generate(PackageRefs: [*packages]))
  self
end

#edit!(**kwords) ⇒ self?

Note:

this possibly mutates the attributes depending on the HTTP response

Edit this repository's attributes as per the parameters.

Returns:

  • (self)

    if the instance data was mutated

  • (nil)

    if the instance data was not mutated


110
111
112
113
114
115
116
117
118
# File 'lib/aptly/repository.rb', line 110

def edit!(**kwords)
  response = connection.send(:put,
                             "/repos/#{self.Name}",
                             body: JSON.generate(kwords))
  hash = JSON.parse(response.body, symbolize_names: true)
  return nil if hash == marshal_dump
  marshal_load(hash)
  self
end

#packages(**kwords) ⇒ Array<String>

List all packages in the repository

Returns:

  • (Array<String>)

    list of packages in the repository


65
66
67
68
69
70
# File 'lib/aptly/repository.rb', line 65

def packages(**kwords)
  response = connection.send(:get, "/repos/#{self.Name}/packages",
                             query: kwords,
                             query_mangle: false)
  JSON.parse(response.body)
end

#publish(prefix, **kwords) ⇒ PublishedRepository

Convenience wrapper around Aptly.publish, publishing this repository locally and as only source of prefix.

Parameters:

  • prefix (String)

    prefix to publish under (i.e. published repo name). This must be escaped (see Aptly.escape_prefix)

Returns:

See Also:


102
103
104
# File 'lib/aptly/repository.rb', line 102

def publish(prefix, **kwords)
  Aptly.publish([{ Name: self.Name }], prefix, 'local', kwords)
end

#snapshot(name = nil, **kwords) ⇒ Snapshot

Creates a new Snapshot

Parameters:

  • name (String) (defaults to: nil)

    name of snapshot

Returns:


123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/aptly/repository.rb', line 123

def snapshot(name = nil, **kwords)
  # TODO: 1.0
  if name.nil? && !kwords.key?(:Name)
    # backwards compatible handling allows name to be passed though
    # kwords or the argument. Argument is preferred.
    raise ArgumentError, 'wrong number of arguments (given 0, expected 1)'
  end
  kwords[:Name] = name unless name.nil?
  response = connection.send(:post, "/repos/#{self.Name}/snapshots",
                             body: JSON.generate(kwords))
  Aptly::Snapshot.new(::Aptly::Connection.new, JSON.parse(response.body))
end

#upload(files) ⇒ Object

FIXME: needs to support single files Convenience wrapper around Files.upload, #add_file and Files.delete


57
58
59
60
61
# File 'lib/aptly/repository.rb', line 57

def upload(files)
  Files.tmp_upload(files, connection) do |dir|
    add_file(dir)
  end
end