lib/gooddata/helpers/auth_helpers.rb in gooddata-0.6.19 vs lib/gooddata/helpers/auth_helpers.rb in gooddata-0.6.20

- old
+ new

@@ -1,9 +1,9 @@ # encoding: utf-8 require 'fileutils' -require 'multi_json' +require 'json' require_relative 'global_helpers' module GoodData module Helpers @@ -22,13 +22,43 @@ else {} end end + # Try read environemnt + # + # Tries to read it from ~/.gooddata file or from environment variable GD_SERVER + # @param [String] credentials_file_path (credentials_file) Path to .gooddata file + # @return [String] server token from environment variable, .gooddata or nil + def read_environment(credentials_file_path = credentials_file) + goodfile = read_credentials(credentials_file_path) + [ENV['GD_ENVIRONMENT'], goodfile[:environment], GoodData::Project::DEFAULT_ENVIRONMENT].find { |x| !x.nil? && !x.empty? } + end + + # Try read server + # + # Tries to read it from ~/.gooddata file or from environment variable GD_SERVER + # @param [String] credentials_file_path (credentials_file) Path to .gooddata file + # @return [String] server token from environment variable, .gooddata or DEFAULT_URL + def read_server(credentials_file_path = credentials_file) + goodfile = read_credentials(credentials_file_path) + [ENV['GD_SERVER'], goodfile[:server], GoodData::Rest::Connection::DEFAULT_URL].find { |x| !x.nil? && !x.empty? } + end + + # Try read token + # + # Tries to read it from ~/.gooddata file or from environment variable GD_PROJECT_TOKEN + # @param [String] credentials_file_path (credentials_file) Path to .gooddata file + # @return [String] auth token from environment variable, .gooddata or nil + def read_token(credentials_file_path = credentials_file) + goodfile = read_credentials(credentials_file_path) + [ENV['GD_PROJECT_TOKEN'], goodfile[:auth_token], goodfile[:token]].find { |x| !x.nil? && !x.empty? } + end + # Writes credentials def write_credentials(credentials, credentials_file_path = credentials_file) File.open(credentials_file_path, 'w', 0600) do |f| - f.puts MultiJson.encode(credentials, :pretty => true) + f.puts JSON.pretty_generate(credentials) end credentials end def remove_credentials_file(credentials_file_path = credentials_file)