# frozen_string_literal: true

require "net/http"

module Exports
  class Nightly
    def initialize(dir, bucket)
      @dir = dir
      @bucket = bucket
    end

    def run
      system("tar czf #{@dir}/orders.tgz data/orders")
      upload(File.join(@dir, "orders.tgz"))
    end

    def upload(path)
      uri = URI("https://storage.example.com/#{@bucket}")
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      http.put(uri.path, File.binread(path))
    end

    def state(blob)
      Marshal.load(blob)
    end
  end
end
