Sha256: dc3c8eae71c0f9027912ca2dbbfa64865cf2f906e9ff373b66e06d952a85bcfc

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'erb'
require 'hashie'
require 'open-uri'
require 'rexml/document'
require 'sinatra/base'
require 'yaml'

module GreenScreen

  Job = Struct.new(:name, :url, :activity, :last_run)
  Run = Struct.new(:label, :time, :status)

  class App < Sinatra::Base

    set :app_file, __FILE__
    set :server, "thin"

    get '/' do

      @auto_refresh_period = 15

      @jobs = settings.config.sources.map do |source|
        load_source(source)
      end.flatten

      erb :index

    end

    private

    def load_source(source)
      jobs = load_cc_xml(source.url)
      if included_jobs = source.jobs
        jobs = jobs.select { |j| included_jobs.member?(j.name) }
      end
      jobs
    rescue => e
      $stderr.puts "ERROR loading #{source.url.inspect}: #{e}"
    end

    def load_cc_xml(url)
      xml = REXML::Document.new(open(url))
      xml.elements.to_a("Projects/Project").map do |project_element|
        data = project_element.attributes
        Job.new.tap do |job|
          job.name = data["name"]
          job.url = data["webUrl"]
          job.activity = data["activity"]
          job.last_run = Run.new.tap do |run|
            run.label = data["lastBuildLabel"]
            run.time = Time.parse(data["lastBuildTime"]).localtime
            run.status = data["lastBuildStatus"]
          end
        end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
greenscreen-0.1.2 lib/greenscreen/app.rb