Class: Tc4r::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tc4r/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tc4r/client.rb', line 13

def initialize(options = {})
  default_options = { :server   => 'localhost',
                      :port     => 8111,
                      :username => nil,
                      :password => nil,
                      :transport => :http
                    }
  options = default_options.merge(options)

  @server = options[:server]
  @port = options[:port]
  @username = options[:username]
  @password = options[:password]
  @transport = options[:transport]
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



10
11
12
# File 'lib/tc4r/client.rb', line 10

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/tc4r/client.rb', line 8

def port
  @port
end

#serverObject (readonly)

Returns the value of attribute server.



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

def server
  @server
end

#transportObject (readonly)

Returns the value of attribute transport.



11
12
13
# File 'lib/tc4r/client.rb', line 11

def transport
  @transport
end

#usernameObject (readonly)

Returns the value of attribute username.



9
10
11
# File 'lib/tc4r/client.rb', line 9

def username
  @username
end

Instance Method Details

#base_urlObject



29
30
31
# File 'lib/tc4r/client.rb', line 29

def base_url
  @base_url ||= "#{transport}://#{server}:#{port}/httpAuth"
end

#build(bt, number) ⇒ Object



70
71
72
73
# File 'lib/tc4r/client.rb', line 70

def build(bt,number)
  build = Nestful.get rest_url("buildTypes/id:#{bt}/builds/number:#{number}") , merge_options( :format => :json )
  Tc4r::Build.new(Tc4r::Helpers.sym_score(build), self)
end

#build_type(bt) ⇒ Object



54
55
56
57
# File 'lib/tc4r/client.rb', line 54

def build_type(bt)
  build_type =  Nestful.get rest_url("buildTypes/id:#{bt}") , merge_options( :format => :json )
  BuildType.new(Tc4r::Helpers.sym_score(build_type), self)
end

#build_typesObject



47
48
49
50
51
52
# File 'lib/tc4r/client.rb', line 47

def build_types
  bts = Nestful.get rest_url('buildTypes') , merge_options( :format => :json )
  bts['buildType'].collect do |bt|
    BuildType.new(Tc4r::Helpers.sym_score(bt), self)
  end
end

#download_file(bt, number, file, options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tc4r/client.rb', line 75

def download_file(bt,number,file,options = {})
  default_options = {:with_progress => true, :destination => File.expand_path("./#{File.basename(file)}")}
  options = default_options.merge(options)

  download_parameters = {:buffer => true}
  download_parameters[:buffer_binmode] = true
  if options[:with_progress]
    download_parameters[:progress] = progress_bar
  end

  build = build(bt,number)
  f = Nestful.get download_url("#{bt}/#{build.id}:id/#{file}"), merge_options( download_parameters )
  f.close
  FileUtils.mv(f.path,options[:destination])

end

#download_url(path) ⇒ Object



37
38
39
# File 'lib/tc4r/client.rb', line 37

def download_url(path)
  "#{base_url}/repository/download/#{path}"
end

#merge_options(options) ⇒ Object



41
42
43
44
45
# File 'lib/tc4r/client.rb', line 41

def merge_options(options)
  { :user      => username,
    :password  => password,
    :auth_type => :basic }.merge(options)
end

#progress_barObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tc4r/client.rb', line 92

def progress_bar
  Proc.new do |conn, total, size| 
    @progress ||= Commander::UI::ProgressBar.new total, :title => "Downloading Build", :complete_message => "File download complete"
    @last_pct ||= 0
    current_pct = ((size.to_f/total.to_f)*100).floor
    @progress.instance_variable_set('@step',size)
    if current_pct != @last_pct
      @progress.show 
      @last_pct = current_pct
    end

    if @progress.finished?
      @progress = nil
    end
  end
end

#query_builds(bt, options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/tc4r/client.rb', line 59

def query_builds(bt, options = {})
  parameters = {}
  parameters[:status] = 'SUCCESS' if options[:status] == :success
  parameters[:status] = 'ERROR' if options[:status] == :error
  parameters[:tag] = options[:tag] if options[:tag]
  builds = Nestful.get rest_url("buildTypes/id:#{bt}/builds") , merge_options( :format => :json, :params => parameters )
  builds['build'].collect do |build|
    Build.new(Tc4r::Helpers.sym_score(build),self)
  end
end

#rest_url(path) ⇒ Object



33
34
35
# File 'lib/tc4r/client.rb', line 33

def rest_url(path)
  "#{base_url}/app/rest/#{path}"
end