Sha256: b14437ff8d056617e3938f0e3c1f0b84612f1d688119569f91438d3189adc783

Contents?: true

Size: 1.68 KB

Versions: 9

Compression:

Stored size: 1.68 KB

Contents

require 'minimart/utils/http'

module Minimart
  module Mirror

    # Source represents a single remote source to fetch cookbooks from.
    # Each source is specified in the inventory using the 'sources' key.
    class Source

      # @return [String] The URL for this source
      attr_accessor :base_url

      # @param [String] base_url The URL for this source
      def initialize(base_url)
        @base_url = base_url
      end

      # Search through the cookbooks available at this source, and return
      # the relevant version if it is available.
      # @return [Minimart::Mirror::SourceCookbook]
      def find_cookbook(cookbook_name, version)
        cookbooks.find do |cookbook|
          cookbook.name == cookbook_name && cookbook.version == version
        end
      end

      # Query this source for it's available cookbooks using the '/universe' endpoint.
      # @return [Array<Minimart::Mirror::SourceCookbook]
      def cookbooks
        @cookbooks ||= fetch_universe_data.each_with_object([]) do |cookbook_info, memo|
          name, versions = cookbook_info
          memo.concat versions.map { |version, attrs| build_cookbook(name, version, attrs) }
        end
      end

      def to_s
        base_url
      end

      private

      def build_cookbook(name, version, attrs)
        attrs = attrs.merge(name: name, version: version)
        SourceCookbook.new(attrs)
      end

      def fetch_universe_data
        Configuration.output.puts "Fetching the universe for #{base_url} ..."

        Utils::Http.get_json(base_url, 'universe')

      rescue RestClient::ResourceNotFound
        raise Error::UniverseNotFoundError.new "no universe found for #{base_url}"
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
minimart-1.2.5 lib/minimart/mirror/source.rb
minimart-1.2.4 lib/minimart/mirror/source.rb
minimart-1.2.3 lib/minimart/mirror/source.rb
minimart-1.2.0 lib/minimart/mirror/source.rb
minimart-1.1.6 lib/minimart/mirror/source.rb
minimart-1.1.3 lib/minimart/mirror/source.rb
minimart-1.0.2 lib/minimart/mirror/source.rb
minimart-1.0.1 lib/minimart/mirror/source.rb
minimart-0.0.1 lib/minimart/mirror/source.rb