Class: EmergeCLI::Commands::DownloadOrderFiles

Inherits:
GlobalOptions
  • Object
show all
Defined in:
lib/commands/order_files/download_order_files.rb

Constant Summary collapse

EMERGE_ORDER_FILE_URL =
'order-files-prod.emergetools.com'.freeze

Instance Method Summary collapse

Methods inherited from GlobalOptions

#before

Constructor Details

#initialize(network: nil) ⇒ DownloadOrderFiles

Returns a new instance of DownloadOrderFiles.



24
25
26
# File 'lib/commands/order_files/download_order_files.rb', line 24

def initialize(network: nil)
  @network = network
end

Instance Method Details

#call(**options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/commands/order_files/download_order_files.rb', line 28

def call(**options)
  @options = options
  before(options)

  begin
    api_token = @options[:api_token] || ENV.fetch('EMERGE_API_TOKEN', nil)
    raise 'API token is required' unless api_token

    raise 'Bundle ID is required' unless @options[:bundle_id]
    raise 'App version is required' unless @options[:app_version]

    @network ||= EmergeCLI::Network.new(api_token:, base_url: EMERGE_ORDER_FILE_URL)
    output_name = @options[:output] || "#{@options[:bundle_id]}-#{@options[:app_version]}.gz"
    output_name = "#{output_name}.gz" unless output_name.end_with?('.gz')

    Sync do
      request = get_order_file(options[:bundle_id], options[:app_version])
      response = request.read

      File.write(output_name, response)

      if @options[:unzip]
        Logger.info 'Unzipping order file...'
        Zlib::GzipReader.open(output_name) do |gz|
          File.write(output_name.gsub('.gz', ''), gz.read)
        end
      end

      Logger.info 'Order file downloaded successfully'
    end
  rescue StandardError => e
    Logger.error "Failed to download order file: #{e.message}"
    Logger.error 'Check your parameters and try again'
    raise e
  ensure
    @network&.close
  end
end