Sha256: 03e2a0e352d9f5a108111158c88159069c566e4621fca96a52713bfe67a57aae

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

require 'net/http'
require 'nokogiri'
require 'date'

module Chicanery
  class Cctray
    attr_reader :name, :uri, :options

    def initialize name, url, options={}
      @name, @uri, @options = name, URI(url), options
    end

    def get
      req = Net::HTTP::Get.new(uri.path)
      req.basic_auth user, password if options[:user] and options[:password]
      res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https', verify_mode: OpenSSL::SSL::VERIFY_NONE) do |https|
        https.request(req)
      end
      res.body
    end

    def jobs
      jobs = {}
      Nokogiri::XML(get).css("Project").each do |project|
        job = {
          activity: project[:activity] == 'Sleeping' ? :sleeping : :building,
          last_build_status: parse_build_status(project[:lastBuildStatus]),
          last_build_time: project[:lastBuildTime].empty? ? nil : DateTime.parse(project[:lastBuildTime]).to_time.to_i,
          url: project[:webUrl],
          last_label: project[:lastBuildLabel]
        }
        jobs[project[:name]] = job unless filtered project[:name]
      end
      jobs
    end

    def parse_build_status status
      case status
      when /^Success/ then :success
      when /^Unknown/ then :unknown
      else :failure
      end
    end

    def filtered name
      return false unless options[:include]
      !options[:include].match(name)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chicanery-0.0.7 lib/chicanery/cctray.rb
chicanery-0.0.6 lib/chicanery/cctray.rb