Sha256: c20b23f35b2e4796579193141781331bdad2a19a17ef596422ac4254de05e494

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

require 'nokogiri'
require 'open-uri'

module ProspectusDockerhub
  GOOD_STATUSES = %w[queued building success].freeze
  # rubocop:disable Metrics/LineLength
  STATUS_XPATH = '//span[contains(@class, "BuildStatus__statusWrapper__")]'.freeze
  # rubocop:enable Metrics/LineLength

  ##
  # Helper for automatically adding build status check
  class Build < Module
    def initialize(repo_slug)
      @repo_slug = repo_slug || raise('No repo specified')
    end

    def extended(other) # rubocop:disable Metrics/MethodLength
      actual_val, expected_val = parse_status

      other.deps do
        item do
          name 'dockerhub'

          expected do
            static
            set expected_val
          end

          actual do
            static
            set actual_val
          end
        end
      end
    end

    private

    def parse_status
      return [status, status] if GOOD_STATUSES.include?(status)
      [status, 'success']
    end

    def status
      @status ||= html.xpath(STATUS_XPATH).first.text.strip.downcase
    end

    def html
      @html ||= Nokogiri::HTML(open(url)) # rubocop:disable Security/Open
    end

    def url
      "#{base_url}/#{@repo_slug}/builds"
    end

    def base_url
      'https://hub.docker.com/r'
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
prospectus_dockerhub-0.0.8 lib/prospectus_dockerhub.rb
prospectus_dockerhub-0.0.7 lib/prospectus_dockerhub.rb
prospectus_dockerhub-0.0.6 lib/prospectus_dockerhub.rb