class Gems::APIKey

An API key, as returned by the API key and trusted publishing endpoints

An API key is compared by all of its attributes, the key among them, so that two keys given the same name, by two accounts or by two token exchanges, are not taken for one another in a Set or as the keys of a Hash.

@api public

Constants

SCOPES

The scopes the RubyGems API defines for an API key

A scope the API does not define is refused by {API::APIKeyEndpoints#create_api_key} and {API::APIKeyEndpoints#update_api_key} rather than sent, so this is the list a caller building its own scopes can check against.

@api public

Public Instance Methods

The API key

The reader is declared, as the readers of the other attributes are, so that a pattern matches an API key by its key as it matches the rest, such as ‘in {name:, key:}`. The key is still left out of {#inspect}, so that printing an API key in a console does not print the key it carries.

A response that carries no key raises here, rather than answering with nil, since a caller asking for the key is asking for the one thing the endpoint was called for. A pattern is answered without it instead of raising (see {Resource#deconstruct_keys}).

@api public @return [String] the API key @raise [InvalidResponse] if the response has no key @example Get the key

api_key.key
# File lib/gems/api_key.rb, line 93
def key
  attributes.fetch("rubygems_api_key") do
    raise InvalidResponse.new(body: JSON.generate(attributes), message: "The API key response has no rubygems_api_key")
  end
end

The gem the API key is restricted to

The token exchange and an API key role answer with the gem the key they issue may act on, when it is restricted to one, as a record of the gem rather than as the gem endpoint answers with it, so the gem carries its name and little else. {API::APIKeyEndpoints#create_api_key} keeps the name of the gem it was asked to restrict the key to.

@api public @return [Gem, nil] the gem, or nil when the key may act on any gem @example Get the name of the gem an API key is restricted to

api_key.rubygem&.name
# File lib/gems/api_key.rb, line 72
def rubygem
  rubygem = self[:gem]
  Gem.new(rubygem) if rubygem
end