Sha256: 87dc581a5e01b4d55cc3dff901a66a7cc379f2fe7e68b9fcdbc74cf097c77472

Contents?: true

Size: 1.65 KB

Versions: 7

Compression:

Stored size: 1.65 KB

Contents

require 'commander/blank'
require 'commander/command'

module PolyglotIos
  module Command
    class Projects
      include Helper::Terminal
      include Helper::General

      attr_accessor :options

      def self.init(options = Commander::Command::Options.new)
        new(options).call
      end

      def initialize(options = Commander::Command::Options.new)
        @options = options
      end

      def call
        list_projects
      end

      def filtered_projects
        projects
          .select { |key, _id| key[/^(.*?(#{option_query})[^$]*)$/i] }
          .sort_by { |p| p[0].downcase }
          .to_h
      end

      def projects
        prompt.say('Getting projects...')
        PolyglotIos::Resource::Project.api_url(option_site_url).token(option_token).depaginate.each_with_object({}) do |r, hash|
          hash[r.name] = r.id
        end
      end

      def option_query
        @option_query ||= @options.__hash__.fetch(:query, '')
      end

      def option_site_url
        @option_site_url = @options.__hash__.fetch(:url, PolyglotIos::DEFAULT_API_URL)
      end

      def option_token
        @option_token = @options.__hash__.fetch(:token, nil) || token
      end

      private

      def list_projects
        projects = filtered_projects
        selection = projects
          .each_with_object({}) do |project, hash|
            project_name = project[0]
            project_id = project[1]
            display_name = "#{project_name} => #{project_id}"
            hash[display_name] = project_id
          end
        prompt.say("  Name - ID: \n  ----------")
        selected_id = prompt
          .select("", selection)
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ios_polyglot_cli-2.7.1 lib/ios_polyglot_cli/commands/projects.rb
ios_polyglot_cli-2.7.0 lib/ios_polyglot_cli/commands/projects.rb
ios_polyglot_cli-2.6.3 lib/ios_polyglot_cli/commands/projects.rb
ios_polyglot_cli-2.6.2 lib/ios_polyglot_cli/commands/projects.rb
ios_polyglot_cli-2.6.1 lib/ios_polyglot_cli/commands/projects.rb
ios_polyglot_cli-2.6.0 lib/ios_polyglot_cli/commands/projects.rb
ios_polyglot_cli-2.5.1 lib/ios_polyglot_cli/commands/projects.rb