class Berkshelf::Source

def ==(other)

def ==(other)
  return false unless other.is_a?(self.class)
  type == other.type && uri == other.uri
end

def api_client

def api_client
  @api_client ||= case type
                  when :chef_server
                    APIClient.chef_server(server_url: uri.to_s, **options)
                  when :artifactory
                    # Don't accidentally mutate the options.
                    client_options = options.dup
                    api_key = client_options.delete(:api_key)
                    APIClient.new(uri, headers: { "X-Jfrog-Art-Api" => api_key }, **client_options)
                  when :chef_repo
                    ChefRepoUniverse.new(uri_string, **options)
                  else
                    APIClient.new(uri, **options)
                  end
end

def api_timeout

def api_timeout
  Berkshelf::Config.instance.api.timeout.to_i
end

def build_universe

Returns:
  • (Array) -
def build_universe
  @universe = api_client.universe
rescue => ex
  @universe = []
  raise ex
end

def cookbook(name, version)

Returns:
  • (APIClient::RemoteCookbook) -

Parameters:
  • version (String) --
  • name (String) --
def cookbook(name, version)
  universe.find { |cookbook| cookbook.name == name && cookbook.version == version }
end

def default?

Returns:
  • (true, false) -
def default?
  @default_ ||= uri.host == URI.parse(Berksfile::DEFAULT_API_URL).host
end

def hash

def hash
  [type, uri_string, options].hash
end

def initialize(berksfile, source, **options)

Parameters:
  • source (String, Berkshelf::SourceURI) --
  • berksfile (Berkshelf::Berksfile) --
def initialize(berksfile, source, **options)
  @options = { timeout: api_timeout, open_timeout: [(api_timeout / 10), 3].max, ssl: {} }
  @options.update(options)
  case source
  when String
    # source "https://supermarket.chef.io/"
    @type = :supermarket
    @uri_string = source
  when :chef_server
    # source :chef_server
    @type = :chef_server
    @uri_string = options[:url] || Berkshelf::Config.instance.chef.chef_server_url
  when Hash
    # source type: uri, option: value, option: value
    source = source.dup
    @type, @uri_string = source.shift
    @options.update(source)
  end
  # Default options for some source types.
  case @type
  when :chef_server
    @options[:client_name] ||= Berkshelf::Config.instance.chef.node_name
    @options[:client_key] ||= Berkshelf::Config.instance.chef.client_key
  when :artifactory
    @options[:api_key] ||= Berkshelf::Config.instance.chef.artifactory_api_key || ENV["ARTIFACTORY_API_KEY"]
  when :chef_repo
    @options[:path] = uri_string
    # If given a relative path, expand it against the Berksfile's folder.
    @options[:path] = File.expand_path(@options[:path], File.dirname(berksfile ? berksfile.filepath : Dir.pwd))
    # Lie because this won't actually parse as a URI.
    @uri_string = "file://#{@options[:path]}"
  end
  # Set some default SSL options.
  Berkshelf::Config.instance.ssl.each do |key, value|
    @options[:ssl][key.to_sym] = value unless @options[:ssl].include?(key.to_sym)
  end
  @options[:ssl][:cert_store] = ssl_policy.store if ssl_policy.store
  @universe = nil
end

def inspect

def inspect
  "#<#{self.class.name} #{type}: #{uri.to_s.inspect}, #{options.map { |k, v| "#{k}: #{v.inspect}" }.join(", ")}>"
end

def latest(name)

Returns:
  • (APIClient::RemoteCookbook) -

Parameters:
  • name (String) --
def latest(name)
  versions(name).max
end

def search(name)

Returns:
  • (Array - Array

Parameters:

  • name (String) --
def search(name)
  universe
    .select { |cookbook| cookbook.name =~ Regexp.new(name) }
    .group_by(&:name)
    .collect { |_, versions| versions.max_by { |v| Semverse::Version.new(v.version) } }
end

def ssl_policy

def ssl_policy
  @ssl_policy ||= SSLPolicy.new
end

def to_s

def to_s
  case type
  when :supermarket
    uri.to_s
  when :chef_repo
    options[:path]
  else
    "#{type}: #{uri}"
  end
end

def universe

Returns:
  • (Array) -
def universe
  @universe || build_universe
end

def uri

def uri
  @uri ||= SourceURI.parse(uri_string)
end

def versions(name)

Returns:
  • (Array) -

Parameters:
  • name (String) --
def versions(name)
  universe.select { |cookbook| cookbook.name == name }
end