summaryrefslogtreecommitdiff
path: root/lib/ats/cli/command.rb
blob: eeb58b35aa1375aa3ac45b068ef495729c5d8954 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module ATS
  module CLI
    class Command < Thor
      class_option :profile, default: :default, required: false
      class_option :debug, default: false, required: false

      def self.printable_commands(*args)
        super.map do |x|
          x[0] = x[0].gsub(/^ats/, "ats #{service_name.downcase}")
          x
        end
      end

      protected

      def api
        self.class.constant_name.new(
          configuration: configuration.fetch(profile)[self.class.service_name.downcase.to_sym],
          debug: configuration.debug,
        )
      end

      def print_json(json)
        say JSON.pretty_generate(json), :green
      end

      def configuration
        ATS.configure do |x|
          Net::Hippie.logger = Logger.new('/dev/null') unless options['debug']
          x.debug = options['debug']
        end
      end

      def profile
        options['profile'].to_sym
      end

      def self.constant_name
        Kernel.const_get("ATS::#{service_name}::API")
      end

      def self.service_name
        name.split("::")[2]
      end
    end
  end
end