Sha256: 3cd8e7467dc6c47c3e28e5a2f269d90c0c3e7638899607d596b6726e36a079ab

Contents?: true

Size: 1.95 KB

Versions: 9

Compression:

Stored size: 1.95 KB

Contents

module ThreeScaleToolbox
  class Remotes
    class << self
      def from_uri(uri_str)
        uri = Helper.parse_uri(uri_str)

        authentication = uri.user
        uri.user = ''
        { authentication: authentication, endpoint: uri.to_s }
      end
    end

    def initialize(config)
      @config = config
    end

    ##
    # Fetch remotes
    # Perform validation
    #
    def all
      rmts = (config.data :remotes) || {}
      raise_invalid unless validate(rmts)
      rmts
    end

    def add_uri(name, uri)
      remote = self.class.from_uri(uri)
      add(name, remote)
    end

    def add(key, remote)
      update do |rmts|
        rmts.tap { |r| r[key] = remote }
      end
    end

    def delete(key)
      value = nil
      update do |rmts|
        # block should return rmts
        # but main method should return deleted value
        rmts.tap do |r|
          value = if block_given?
                    r.delete(key, &Proc.new)
                  else
                    r.delete(key)
                  end
        end
      end
      value
    end

    def fetch(name)
      all.fetch(name) { raise_not_found(name) }
    end

    private

    attr_reader :config

    ##
    # Update remotes
    # Perform validation
    #
    def update
      config.update(:remotes) do |rmts|
        yield(rmts || {}).tap do |new_rmts|
          raise_invalid unless validate(new_rmts)
        end
      end
    end

    def raise_not_found(remote_str)
      raise ThreeScaleToolbox::Error, "remote '#{remote_str}' not found from config file #{config.config_file}"
    end

    def raise_invalid
      raise ThreeScaleToolbox::Error, "invalid remote configuration from config file #{config.config_file}"
    end

    def valid?(remote)
      remote.is_a?(Hash) && remote.key?(:endpoint) && remote.key?(:authentication)
    end

    def validate(remotes)
      case remotes
      when Hash then remotes.values.all?(&method(:valid?))
      else false
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
3scale_toolbox-0.17.1 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.17.0 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.16.0 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.15.0 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.14.0 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.13.0 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.12.4 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.12.3 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.12.2 lib/3scale_toolbox/remotes.rb