Sha256: 23be34276f0ae3c7e866f22023fb712db1d33d4f7b7564a287d6f0bb8d446447

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

require 'acquia/cloud/entity'
require 'net/http'
require 'uri'

module Acquia
  class Cloud
    class DatabaseBackup < Entity
      include Operations::Delete

      def initialize(cloud, site, environment, database, backup)
        @cloud = cloud
        @site = site
        @environment = environment
        @database = database
        @data = backup
        @url = "/sites/#{@site}/envs/#{@environment}/dbs/#{@database}/backups/#{data['id']}"
      end

      def id
        data['id']
      end

      def checksum
        data['checksum']
      end

      def deleted?
        data['deleted'] != '0'
      end

      def name
        data['name']
      end

      def path
        data['path']
      end

      def started
        Time.at(data['started'].to_i)
      end

      def completed
        Time.at(data['completed'].to_i)
      end

      def type
        data['type'].to_s
      end

      def link
        data['link']
      end

      def download_to(path, &block)
        uri = URI.parse(link)

        File.open(path, 'wb') do |io|
          https = uri.scheme == 'https'
          Net::HTTP.start(uri.host, uri.port, use_ssl: https) do |http|
            request = Net::HTTP::Get.new uri
            request.basic_auth *@cloud.api.credentials

            http.request request do |response|
              response.read_body do |chunk|
                io.write chunk
                block.call(response, chunk) if block_given?
              end
            end
          end
        end
      end

      def download(&block)
        download_to(File.basename(path), &block)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
acquia-cloud-0.5.1 lib/acquia/cloud/database_backup.rb