Sha256: f27a8d73d3b451e451cc91ad912ea4a02bcc289859c1409806fdaa15eb0a6bc3

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

require 'uri'

module EY
  class Config
    CONFIG_FILES = ["config/ey.yml", "ey.yml"]

    def initialize(file = nil)
      require 'yaml'
      @file = file || CONFIG_FILES.find{|f| File.exists?(f) }
      @config = (@file ? YAML.load_file(@file) : {}) || {} # load_file returns `false' when the file is empty
      @config["environments"] = {} unless @config["environments"]
    end

    def method_missing(meth, *args, &blk)
      key = meth.to_s.downcase
      if @config.key?(key)
        @config[key]
      else
        super
      end
    end

    def respond_to?(meth)
      key = meth.to_s.downcase
      @config.key?(key) || super
    end

    def endpoint
      @endpoint ||= env_var_endpoint ||
        config_file_endpoint ||
        default_endpoint
    end

    def config_file_endpoint
      if endpoint = @config["endpoint"]
        assert_valid_endpoint endpoint, @file
      end
    end

    def env_var_endpoint
      if endpoint = ENV["CLOUD_URL"]
        assert_valid_endpoint endpoint, "CLOUD_URL"
      end
    end

    def default_endpoint
      URI.parse("https://cloud.engineyard.com/")
    end

    def default_endpoint?
      default_endpoint == endpoint
    end

    def default_environment
      d = environments.find do |name, env|
        env["default"]
      end
      d && d.first
    end

    def default_branch(environment = default_environment)
      env = environments[environment]
      env && env["branch"]
    end

    private

    def assert_valid_endpoint(endpoint, source)
      endpoint = URI.parse(endpoint) if endpoint.is_a?(String)
      return endpoint if endpoint.absolute?

      raise ConfigurationError.new('endpoint', endpoint.to_s, source, "endpoint must be an absolute URI")
    end

    class ConfigurationError < EY::Error
      def initialize(key, value, source, message=nil)
        super %|"#{key}" from #{source} has invalid value: #{value.inspect}#{": #{message}" if message}|
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
engineyard-1.4.7 lib/engineyard/config.rb