class ThreeScaleToolbox::Remotes

Attributes

config[R]

Public Class Methods

from_uri(uri_str) click to toggle source
# File lib/3scale_toolbox/remotes.rb, line 4
def from_uri(uri_str)
  uri = Helper.parse_uri(uri_str)

  authentication = uri.user
  uri.user = ''
  { authentication: authentication, endpoint: uri.to_s }
end
new(config) click to toggle source
# File lib/3scale_toolbox/remotes.rb, line 13
def initialize(config)
  @config = config
end

Public Instance Methods

add(key, remote) click to toggle source
# File lib/3scale_toolbox/remotes.rb, line 32
def add(key, remote)
  update do |rmts|
    rmts.tap { |r| r[key] = remote }
  end
end
add_uri(name, uri) click to toggle source
# File lib/3scale_toolbox/remotes.rb, line 27
def add_uri(name, uri)
  remote = self.class.from_uri(uri)
  add(name, remote)
end
all() click to toggle source

Fetch remotes Perform validation

# File lib/3scale_toolbox/remotes.rb, line 21
def all
  rmts = (config.data :remotes) || {}
  raise_invalid unless validate(rmts)
  rmts
end
delete(key, &block) click to toggle source
# File lib/3scale_toolbox/remotes.rb, line 38
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
fetch(name) click to toggle source
# File lib/3scale_toolbox/remotes.rb, line 54
def fetch(name)
  all.fetch(name) { raise_not_found(name) }
end

Private Instance Methods

raise_invalid() click to toggle source
# File lib/3scale_toolbox/remotes.rb, line 78
def raise_invalid
  raise ThreeScaleToolbox::Error, "invalid remote configuration from config file #{config.config_file}"
end
raise_not_found(remote_str) click to toggle source
# File lib/3scale_toolbox/remotes.rb, line 74
def raise_not_found(remote_str)
  raise ThreeScaleToolbox::Error, "remote '#{remote_str}' not found from config file #{config.config_file}"
end
update() { |rmts || {}| ... } click to toggle source

Update remotes Perform validation

# File lib/3scale_toolbox/remotes.rb, line 66
def update
  config.update(:remotes) do |rmts|
    yield(rmts || {}).tap do |new_rmts|
      raise_invalid unless validate(new_rmts)
    end
  end
end
valid?(remote) click to toggle source
# File lib/3scale_toolbox/remotes.rb, line 82
def valid?(remote)
  remote.is_a?(Hash) && remote.key?(:endpoint) && remote.key?(:authentication)
end
validate(remotes) click to toggle source
# File lib/3scale_toolbox/remotes.rb, line 86
def validate(remotes)
  case remotes
  when Hash then remotes.values.all?(&method(:valid?))
  else false
  end
end