Sha256: 647b1924d86dec67c469408bbebf21cec148af1886f257f7035ca0cc05d707ec

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, &block)
      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, &block)
                  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-1.0.0 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.20.0 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.19.3 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.19.2 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.19.1 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.19.0 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.18.3 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.18.2 lib/3scale_toolbox/remotes.rb
3scale_toolbox-0.18.0 lib/3scale_toolbox/remotes.rb