lib/herokuconf.rb in herokuconf-0.0.1 vs lib/herokuconf.rb in herokuconf-0.0.6

- old
+ new

@@ -1,13 +1,47 @@ +require 'heroku-api' +require 'netrc' + ## # Base module for HerokuConf module HerokuConf class << self - ## - # Insert a helper .new() method for creating a new App object - def new(*args) - self::App.new(*args) + 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 - -require 'herokuconf/app'