Sha256: f9bf022824e651fbcb64a3daf6c87e23bb2c734a1070c33bf5aea48152adf9e6

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

require 'io/console'
require 'uffizzi'
require 'uffizzi/auth_helper'

module Uffizzi
  class CLI::Projects
    include ApiClient

    def run
      return 'You are not logged in' unless Uffizzi::AuthHelper.signed_in?

      hostname = ConfigFile.read_option(:hostname)
      response = fetch_projects(hostname)

      if response[:code] == Net::HTTPOK
        handle_succeed_response(response)
      else
        handle_failed_response(response)
      end
    end

    private

    def handle_failed_response(response)
      ApiClient.print_errors(response[:body][:errors])
    end

    def handle_succeed_response(response)
      projects = response[:body][:projects]
      if projects.empty?
        puts 'No projects related to this email'
        return
      end
      if projects.size == 1
        ConfigFile.write_option(:project, projects.first[:slug])
      end
      print_projects(projects)
    end

    def print_projects(projects)
      projects.each do |project|
        puts (project[:slug]).to_s
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
uffizzi-cli-0.1.4.3 lib/uffizzi/cli/projects.rb
uffizzi-cli-0.1.4.2 lib/uffizzi/cli/projects.rb
uffizzi-cli-0.1.3 lib/uffizzi/cli/projects.rb