Sha256: 7936aaa598ccc55cfe3488a3def9b6fe26b919933cba27b8c6b8c57204880907

Contents?: true

Size: 1023 Bytes

Versions: 1

Compression:

Stored size: 1023 Bytes

Contents

require 'heroku-api'
require 'netrc'

##
# Base module for HerokuConf
module HerokuConf
  class << self
    DEFAULT_OPTIONS = {
      app: nil,
      keys: nil
    }

    def configure!(params = {})
      return if ENV['DYNO']
      app, keys = DEFAULT_OPTIONS.dup.merge!(params).values_at(:app, :keys)
      pairs = config_vars(app)
      pairs.select! { |k, _| keys.include? k } if keys
      pairs.each { |k, v| ENV[k] = v }
    end

    private

    def config_vars(app = nil)
      app ||= parse_app
      client.get_config_vars(app).body
    end

    def parse_app
      remotes.first[1].split('/').last.split('.').first
    end

    def remotes
      remotes = `git remote -v`.split("\n").map(&:split)
      remotes.select! { |x| x.first == 'heroku' && x.last == '(push)' }
      fail('No app found') unless remotes.size == 1
      remotes
    end

    def client
      @client ||= Heroku::API.new(api_key: api_key)
    end

    def api_key
      @api_key ||= Netrc.read['api.heroku.com'].password
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
herokuconf-0.0.6 lib/herokuconf.rb