module Piv
  module Helpers
    module Application
      extend ActiveSupport::Concern

      # TODO: Move to config file
      API_URL = "https://www.pivotaltracker.com/services/v5/"

      module ClassMethods
        attr_accessor :global_dir
      end

      def session_in_progress?
        !!current_session
      end

      def current_session
        Session.current
      end

      def client
        @client ||= Client.new(API_URL)
      end

      def global_installer
        @global_installer ||= MicroCommands::InstallGlobal.new(self.class.global_dir)
      end

      def assure_globally_installed
        global_installer.run :up unless global_installer.done?
      end
    end
  end
end


module Piv
  class ConfigurationError < StandardError; end

  module Helpers
    module Application
      extend ActiveSupport::Concern

      module ClassMethods
        attr_accessor :global_dir, :connection
      end

      def session_in_progress?
        !!current_session
      end

      def current_session
        Session.current
      end

      def client
        @client ||= Client.new(self.class.connection) do |c|
          c.options.timeout = 4
        end
      end

      def global_installer
        @global_installer ||= MicroCommands::InstallGlobal.new(self.class.global_dir)
      end

      def assure_globally_installed
        raise ConfigurationError, "make sure connection and global directory are established" unless self.class.global_dir && self.class.connection

        global_installer.run :up unless global_installer.done?
      end
    end
  end
end


Allow Piv::Helpers::Application module to include accessor methods to receive connection during configuration time. also raise an error as part of assuring global installation in the case the connection or global directory configs are missing
Allow Piv::Helpers::Application module to include accessor methods to receive connection during configuration time. also raise an error as part of assuring global installation in the case the connection or global directory configs are missing