Skip to content
This repository was archived by the owner on Jan 7, 2019. It is now read-only.

Conversation

cilim
Copy link
Contributor

@cilim cilim commented Oct 31, 2016

PR for issue: #60

@coveralls
Copy link

coveralls commented Oct 31, 2016

Coverage Status

Coverage decreased (-0.3%) to 96.962% when pulling 15accdc on mc-base-env-var-names into decbf60 on master.

@coveralls
Copy link

coveralls commented Nov 8, 2016

Coverage Status

Coverage decreased (-31.5%) to 66.102% when pulling 033ba53 on mc-base-env-var-names into d1c9a69 on master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.008%) to 97.617% when pulling 8d77646 on mc-base-env-var-names into d1c9a69 on master.

@coveralls
Copy link

coveralls commented Nov 9, 2016

Coverage Status

Coverage decreased (-0.09%) to 97.519% when pulling 6c3a8de on mc-base-env-var-names into ec85ae7 on master.

@cilim cilim assigned DamirSvrtan and unassigned cilim Nov 10, 2016
@cilim cilim assigned nikone and unassigned DamirSvrtan Nov 14, 2016
@cilim
Copy link
Contributor Author

cilim commented Nov 14, 2016

Ready for review @nikone

end
end
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

module Stormpath
  module Rails
    class ApiKey
      TEST_ENV_VARS = {
        required: {
          STORMPATH_CLIENT_APIKEY_ID: 'The id from your Stormpath API Key',
          STORMPATH_CLIENT_APIKEY_SECRET: 'The secret from your Stormpath API Key',
          STORMPATH_APPLICATION_HREF: 'The href to your application'
        },
        deprecated: {
          STORMPATH_API_KEY_ID: 'The id from your Stormpath API Key',
          STORMPATH_API_KEY_SECRET: 'The secret from your Stormpath API Key',
          STORMPATH_APPLICATION_URL: 'The url to your application'
        }
      }.freeze

      def credentials
        check_env_variable_names
        credentials_from_env_variables
      end

      private

      def check_env_variable_names
        show_deprecation_warning unless test_missing_required_env_vars.empty? && env_vars_not_set?

        raise set_up_message if env_vars_not_set?
      end

      def credentials_from_env_variables
        {
          id: ENV['STORMPATH_CLIENT_APIKEY_ID'] || ENV['STORMPATH_API_KEY_ID'],
          secret: ENV['STORMPATH_CLIENT_APIKEY_SECRET'] || ENV['STORMPATH_API_KEY_SECRET']
        }
      end

      def test_missing_deprecated_env_vars
        TEST_ENV_VARS[:deprecated].reject do |var, _|
          ENV[var.to_s]
        end
      end

      def test_missing_required_env_vars
        TEST_ENV_VARS[:required].reject do |var, _|
          ENV[var.to_s]
        end
      end

      def env_vars_not_set?
        !test_missing_deprecated_env_vars.empty? && !test_missing_required_env_vars.empty?
      end

      def show_deprecation_warning
        warn deprecation_warning
      end

      def deprecation_warning
        warn_message = "\n\n"
        40.times { warn_message << '*' }
        warn_message << 'STORMPATH RAILS'
        52.times { warn_message << '*' }
        warn_message << "\n\n"
        warn_message << TEST_ENV_VARS[:deprecated].map do |var, _|
          "\t#{var} is deprecated since the new version of the gem."
        end.join("\n")
        warn_message << "\n\tPlease update your environment variables to use the new names:\n"
        warn_message << "\n\t\texport STORMPATH_CLIENT_APIKEY_ID=your_api_key_id"
        warn_message << "\n\t\texport STORMPATH_CLIENT_APIKEY_SECRET=your_api_key_secret"
        warn_message << "\n\t\texport STORMPATH_APPLICATION_HREF=href_to_application\n\n"
        110.times { warn_message << '*' }
        warn_message << "\n\n"
        warn_message
      end

      def set_up_message
        set_up_message = "In order to use the stormpath-rails gem you need to set the following environment variables:\n\t"
        set_up_message << test_missing_required_env_vars.map do |var, message|
          "#{var} : #{message}"
        end.join("\n\t")
        set_up_message << "\nBe sure to configure these before trying to run your application.\n\n"
        set_up_message
      end

    end
  end
end

{
id: ENV['STORMPATH_CLIENT_APIKEY_ID'] || ENV['STORMPATH_API_KEY_ID'],
secret: ENV['STORMPATH_CLIENT_APIKEY_SECRET'] || ENV['STORMPATH_API_KEY_SECRET']
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

      def self.client
        self.connection ||= Stormpath::Client.new(api_key: api_key.credentials)
      end

      def self.api_key
        Stormpath::Rails::ApiKey.new
      end

@cilim cilim removed the review pr label Nov 25, 2016
@cilim cilim merged commit accf881 into master Nov 25, 2016
@cilim cilim deleted the mc-base-env-var-names branch November 25, 2016 15:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants