Class: Rockette::Commands::Export

Inherits:
Rockette::Command show all
Includes:
TextHelper
Defined in:
lib/rockette/commands/export.rb

Overview

Export and download APEX application

Instance Method Summary collapse

Methods included from TextHelper

#bail, #check_input, #file_not_found, #no_rockette_dir, #padder

Methods inherited from Rockette::Command

#command, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which

Constructor Details

#initialize(options) ⇒ Export

Returns a new instance of Export.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rockette/commands/export.rb', line 11

def initialize(options)
  super()
  @options = options
  @conf = Psych.load(File.read(CONF))
  @body = @conf["token_body"]
  @hdrs = @conf["token_hdrs"]
  @hdrs["Authorization"] = @conf["web_creds"][@options[:cred]] if @options[:cred]
  @filey = "f#{@options[:app_id]}.sql"
  @token = get_token
  @hdrs["Authorization"] = "Bearer " + @token
end

Instance Method Details

#checkerObject



23
24
25
26
27
28
# File 'lib/rockette/commands/export.rb', line 23

def checker
  app_url = "#{@options[:url]}deploy/apps/#{@options[:app_id]}"
  response = Rester.new(url: app_url, headers: @hdrs).rest_try
  bail unless response
  abort padder("App ID: #{@options[:app_id]}, not found. Received: #{response.code}") unless response.code == 200
end

#execute(input: $stdin, output: $stdout) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/rockette/commands/export.rb', line 46

def execute(input: $stdin, output: $stdout)
  check_input(input)
  response = exporter
  # Write file if export was grabbed.
  save_file = @options[:file] || @filey
  File.open(File.join(EXPORT_DIR, save_file), "wb") { |file| file.write(response.body) }
  output.puts padder("Finished downloading #{save_file}. Have a good one!")
end

#exporterObject



30
31
32
33
34
35
36
37
38
# File 'lib/rockette/commands/export.rb', line 30

def exporter
  checker
  puts padder("Found Application ID: #{@options[:app_id]}, proceeding...")
  export_url = "#{@options[:url]}deploy/app/#{@options[:app_id]}"
  response = Rester.new(url: export_url, headers: @hdrs).rest_try
  bail unless response
  abort padder("Download failed for App ID: #{@options[:app_id]}.") unless (200..201).include? response.code
  response
end

#get_tokenObject



40
41
42
43
44
# File 'lib/rockette/commands/export.rb', line 40

def get_token
  token_url = "#{@options[:url]}oauth/token"
  response = Rester.new(headers: @hdrs, meth: "Post", params: @body, url: token_url).rest_try
  return JSON.parse(response.body)["access_token"]
end