Sha256: 6b4cdc547b2ef1f8a7c0d9138332c5c53d38755795aa3abc01bd17fad61035a0

Contents?: true

Size: 995 Bytes

Versions: 2

Compression:

Stored size: 995 Bytes

Contents

# frozen_string_literal: true

module Spandx
  module Rubygems
    class Backups
      attr_reader :base_url

      def initialize(base_url: 'https://s3-us-west-2.amazonaws.com/rubygems-dumps/')
        @base_url = base_url
        @http = Net::Hippie::Client.new
      end

      def each
        response = @http.get(base_url)
        to_xml(response.body).search('//Contents/Key').reverse.each do |node|
          next unless valid?(node.text)

          yield Backup.new(URI.join(base_url, node.text), db_connection)
        end
      end

      private

      def to_xml(raw_xml)
        Nokogiri::XML(raw_xml).tap(&:remove_namespaces!)
      end

      def valid?(text)
        text.end_with?('public_postgresql.tar') &&
          text.start_with?('production')
      end

      def db_connection
        @db_connection ||=
          begin
            require 'pg'
            PG.connect(host: File.expand_path('tmp/sockets'), dbname: 'postgres')
          end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spandx-rubygems-0.1.3 lib/spandx/rubygems/backups.rb
spandx-rubygems-0.1.2 lib/spandx/rubygems/backups.rb