Sha256: 2995e76919ce31f18f76b8b44dae27924e2ca0b68af8a762dc85774c6158f322

Contents?: true

Size: 1.98 KB

Versions: 4

Compression:

Stored size: 1.98 KB

Contents

require 'r10k/module_repository'
require 'r10k/version'

require 'faraday'
require 'faraday_middleware/multi_json'
require 'faraday_middleware'

class R10K::ModuleRepository::Forge

  # @!attribute [r] forge
  #   @return [String] The forge hostname to use for requests
  attr_reader :forge

  # @!attribute [r] :conn
  #   @api private
  #   @return [Faraday]
  attr_reader :conn

  def initialize(forge = 'forge.puppetlabs.com')
    @forge = forge
    @conn  = make_conn
  end

  # Query for all published versions of a module
  #
  # @example
  #   forge = R10K::ModuleRepository::Forge.new
  #   forge.versions('adrien/boolean')
  #   #=> ["0.9.0-rc1", "0.9.0", "1.0.0", "1.0.1"]
  #
  # @param module_name [String] The fully qualified module name
  # @return [Array<String>] All published versions of the given module
  def versions(module_name)
    response = @conn.get("/api/v1/releases.json", {'module' => module_name})
    response.body[module_name].map do |version_info|
      version_info['version']
    end
  end

  # Query for the newest published version of a module
  #
  # @example
  #   forge = R10K::ModuleRepository::Forge.new
  #   forge.latest_version('adrien/boolean')
  #   #=> "1.0.1"
  #
  # @param module_name [String] The fully qualified module name
  # @return [String] The latest published version of the given module
  def latest_version(module_name)
    versions(module_name).last
  end

  private

  def make_conn
    # Force use of json_pure with multi_json on Ruby 1.8.7
    multi_json_opts = (RUBY_VERSION == "1.8.7" ? {:adapter => :json_pure} : {})

    Faraday.new(
      :url => "https://#{@forge}",
      :user_agent => "Ruby/r10k #{R10K::VERSION}"
    ) do |builder|
      builder.request(:multi_json, multi_json_opts)
      builder.response(:multi_json, multi_json_opts)

      # This needs to be _after_ request/response configuration for testing
      # purposes. Without this ordering the tests get badly mangled.
      builder.adapter(Faraday.default_adapter)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
r10k-1.4.2 lib/r10k/module_repository/forge.rb
r10k-1.4.1 lib/r10k/module_repository/forge.rb
r10k-1.4.0 lib/r10k/module_repository/forge.rb
r10k-1.3.5 lib/r10k/module_repository/forge.rb