Sha256: 7ecbb21712b574ff8136b0ff5b19358896800450b5e303124a261b7082914791

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

require 'morale/client'
require 'morale/command'
require 'morale/authorization'

module Morale::Commands
  class Project
    class << self
      include Morale::Platform
      
      def list(change=false)
        begin
          projects = Morale::Command.client.projects
          if !projects.nil?
            projects.sort{|a,b| a['project']['name'] <=> b['project']['name']}.each_with_index do |record, i|
              puts "#{i += 1}. #{record['project']['name']}"
            end
            
            if change
              say "Choose a project: "
              index = ask
              project = projects[index.to_i - 1]
              
              if project.nil?
                say "Invalid project."
              end
              Morale::Account.project = project['project']['id'] unless project.nil?
            end
          else
            say "There were no projects found."
          end
        rescue Morale::Client::Unauthorized
          say "Authentication failure"
          Morale::Commands::Authorization.login
          retry if Morale::Authorization.retry_login?
        rescue Morale::Client::NotFound
          say "Communication failure"
        end
      end
      
      def select(id)
        begin
          projects = Morale::Command.client.projects
          if !projects.nil?
            project = projects[id.to_i - 1]
            if project.nil?
              say "Invalid project."
            end
            Morale::Account.project = project['project']['id'] unless project.nil?
          else
            say "There were no projects found."
          end
        rescue Morale::Client::Unauthorized
          say "Authentication failure"
          Morale::Commands::Authorization.login
          retry if Morale::Authorization.retry_login?
        rescue Morale::Client::NotFound
          say "Communication failure"
        end
      end
      
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
morale-0.1.2 lib/morale/commands/project.rb
morale-0.1.1 lib/morale/commands/project.rb
morale-0.1.0 lib/morale/commands/project.rb