Class: TCClient

Inherits:
Object
  • Object
show all
Defined in:
lib/version.rb,
lib/tc-client.rb

Constant Summary collapse

Version =
"0.0.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verbose = true) ⇒ TCClient

Returns a new instance of TCClient.



8
9
10
11
12
13
14
15
16
17
# File 'lib/tc-client.rb', line 8

def initialize(verbose=true)
  file = File.expand_path('~/.tc-client')
  raise "No configuration file found! Please provide one at #{file}" unless File.exists? file

  @config ||= YAML::load(File.open(file))
  @base_url = config["base_url"]
  @user = config["user"]
  @pwd = config["pwd"]
  @verbose = verbose
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



7
8
9
# File 'lib/tc-client.rb', line 7

def base_url
  @base_url
end

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/tc-client.rb', line 7

def config
  @config
end

#pwdObject

Returns the value of attribute pwd.



7
8
9
# File 'lib/tc-client.rb', line 7

def pwd
  @pwd
end

#userObject

Returns the value of attribute user.



7
8
9
# File 'lib/tc-client.rb', line 7

def user
  @user
end

Instance Method Details

#builds(project) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/tc-client.rb', line 36

def builds(project)
  p = projects.select{|x| x[:name].match /#{project}/i }
  return [{:name => "Can't find a project matching '#{project}'"}] if p.empty?
  xml = get(p.first[:href])
  doc = REXML::Document.new xml
  { :project_name => p.first[:name], :builds => doc.elements.collect("project/buildTypes/buildType") { |b| { :name => b.attributes["name"], :href => b.attributes["href"] } } }
end

#get(what) ⇒ Object



53
54
55
56
57
# File 'lib/tc-client.rb', line 53

def get(what)
  verbose("http://#{@user}:#{@pwd}@#{@base_url}#{what}") {|x|
    RestClient.get x
  }
end

#projectsObject



19
20
21
22
23
# File 'lib/tc-client.rb', line 19

def projects 
  xml = get("/httpAuth/app/rest/projects")
  doc = REXML::Document.new xml
  doc.elements.collect("projects/project") { |p| { :name => p.attributes["name"], :href => p.attributes["href"]}  }
end

#runningObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/tc-client.rb', line 25

def running
  xml = get("/httpAuth/app/rest/builds?locator=running:true")
  doc = REXML::Document.new xml
  builds = doc.elements.collect("builds/build") { |p| { :href => p.attributes["href"], :complete => p.attributes["percentageComplete"] } }
  names = builds.collect do |b| 
    d = REXML::Document.new get(b[:href])
    d.elements.collect("build/buildType") { |x| { :name => x.attributes["name"], :complete => b[:complete] } }
  end
  names.flatten
end

#statuses(project, build) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/tc-client.rb', line 44

def statuses(project, build)
  all = builds(project)
  b = all[:builds].select{|x| x[:name].match /#{build}/i}
  return [{:build => "Can't find a build matching: '#{build}'", :status => "FATAL"}] if b.empty?
  xml = get("#{b.first[:href]}/builds?count=10")
  doc = REXML::Document.new xml
  { :project_name => all[:project_name], :build_name => b.first[:name], :statuses => doc.elements.collect("builds/build") {|x| { :build => x.attributes["number"], :status => x.attributes["status"] } } }
end

#verbose(url) {|url| ... } ⇒ Object

Yields:

  • (url)


59
60
61
62
# File 'lib/tc-client.rb', line 59

def verbose(url)
  puts url if @verbose
  yield url if block_given?
end