Sha256: d38498c70a9b7dfb802d2e133b77f2e9b0e32df7ab23c8c59593186ee11ec6fe

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require "muchkeys/version"
require "muchkeys/errors"
require "net/http"

module MuchKeys

  class << self
    attr_accessor :configuration
  end

  def self.configure
    @configuration ||= Configuration.new
    yield(configuration)
  end

  def self.default_configure
    configure do
    end
  end

  class Configuration
    attr_accessor :consul_host

    # sensible defaults
    def initialize
      @consul_host = 'http://localhost:8500'
    end
  end

  def self.fetch_key(key_name)
    default_configure if !configuration
    return ENV[key_name] unless ENV[key_name].nil?

    response = Net::HTTP.get(URI(consul_url(key_name)))
    handle_response(response)
    fetch_body(response)
  end

  def self.consul_url(key_name)
    converted_key_name = convert_keyname(key_name)
    "#{configuration.consul_host}/v1/kv/#{converted_key_name}?raw"
  end

  def self.fetch_body(response)
    response.body
  end

  def self.handle_response(response_code)
    case response_code
    when (400..599)
      raise MuchKeys::InvalidKey
    end
  end

  def self.convert_keyname(key_name)
    key_name.gsub('___', '/')
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
muchkeys-0.0.0 lib/muchkeys.rb