Sha256: 5ca2a377d112abd97e2dc65ed67be1e5984336f2fee46d85ab1459bbd98481ce

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

require 'highline'
require 'engineyard-cloud-client'
require 'engineyard/eyrc'

module EY
  class CLI
    class API
      def self.authenticate(ui)
        ui.info("We need to fetch your API token; please log in.")
        begin
          email    = ui.ask("Email: ")
          password = ui.ask("Password: ", true)
          token    = EY::CloudClient.authenticate(email, password, ui)
          EY::EYRC.load.api_token = token
          token
        rescue EY::CloudClient::InvalidCredentials
          ui.warn "Invalid username or password; please try again."
          retry
        end
      end

      attr_reader :token

      def initialize(endpoint, ui)
        @ui = ui
        EY::CloudClient.endpoint = endpoint

        @token = ENV['ENGINEYARD_API_TOKEN'] if ENV['ENGINEYARD_API_TOKEN']
        @token ||= EY::EYRC.load.api_token
        @token ||= self.class.authenticate(ui)

        unless @token
          raise EY::Error, "Sorry, we couldn't get your API token."
        end

        @api = EY::CloudClient.new(@token, @ui)
      end

      def respond_to?(*a)
        super or @api.respond_to?(*a)
      end

      protected

      def method_missing(meth, *args, &block)
        if @api.respond_to?(meth)
          @api.send(meth, *args, &block)
        else
          super
        end
      rescue EY::CloudClient::InvalidCredentials
        ui.warn "Authentication failed."
        @token = self.class.authenticate(@ui)
        @api = EY::CloudClient.new(@token, @ui)
        retry
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
engineyard-2.0.0.pre1 lib/engineyard/cli/api.rb
engineyard-1.7.0.pre2 lib/engineyard/cli/api.rb