module ZendeskAppsTools::Deploy

Public Instance Methods

check_job(job_id) click to toggle source
# File lib/zendesk_apps_tools/deploy.rb, line 65
def check_job(job_id)
  connection = get_connection

  loop do
    response = connection.get("/api/v2/apps/job_statuses/#{job_id}")
    info     = JSON.parse(response.body)
    status   = info['status']
    message  = info['message']
    app_id   = info['app_id']

    if %w(completed failed).include? status
      case status
      when 'completed'
        save_cache 'app_id' => app_id
        say_status @command, 'OK'
      when 'failed'
        say_status @command, message, :red
        exit 1
      end
      break
    end

    say_status 'Status', status
    sleep 3
  end
rescue Faraday::Error::ClientError => e
  say_error_and_exit e.message
end
check_status(response) click to toggle source
# File lib/zendesk_apps_tools/deploy.rb, line 56
def check_status(response)
  job = response.body
  job_response = JSON.parse(job)
  say_error_and_exit job_response['error'] if job_response['error']

  job_id = job_response['job_id']
  check_job job_id
end
deploy_app(connection_method, url, body) click to toggle source
# File lib/zendesk_apps_tools/deploy.rb, line 3
def deploy_app(connection_method, url, body)
  body[:upload_id] = upload(options[:path]).to_s

  connection = get_connection

  response = connection.send(connection_method) do |req|
    req.url url
    req.headers[:content_type] = 'application/json'
    req.body = JSON.generate body
  end

  check_status response

rescue Faraday::Error::ClientError, JSON::ParserError => e
  say_error_and_exit e.message
end
find_app_id() click to toggle source
# File lib/zendesk_apps_tools/deploy.rb, line 40
def find_app_id
  say_status 'Update', 'app ID is missing, searching...'
  name = get_value_from_stdin('Enter the name of the app:')

  connection = get_connection

  all_apps = connection.get('/api/v2/apps.json').body

  app_id = JSON.parse(all_apps)['apps'].find { |app| app['name'] == name }['id']

  save_cache 'app_id' => app_id
  app_id
rescue Faraday::Error::ClientError => e
  say_error_and_exit e.message
end
upload(path) click to toggle source
# File lib/zendesk_apps_tools/deploy.rb, line 20
def upload(path)
  connection = get_connection :multipart
  zipfile_path = options[:zipfile]

  if zipfile_path
    package_path = zipfile_path
  else
    package
    package_path = Dir[File.join path, '/tmp/*.zip'].sort.last
  end

  payload = { uploaded_data: Faraday::UploadIO.new(package_path, 'application/zip') }

  response = connection.post('/api/v2/apps/uploads.json', payload)
  JSON.parse(response.body)['id']

rescue Faraday::Error::ClientError, JSON::ParserError => e
  say_error_and_exit e.message
end