Class: Travis::Surveillance::Build

Inherits:
Object
  • Object
show all
Defined in:
lib/travis/surveillance/build.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

ATTRIBUTES =
[:author_name, :branch, :commit, :committed_at, :committer_name,
:compare_url, :duration, :finished_at, :id, :message, :number, :project,
:result, :started_at]

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Build

Returns a new instance of Build.



23
24
25
26
27
# File 'lib/travis/surveillance/build.rb', line 23

def initialize(attrs = {})
  self.attributes = attrs

  populate
end

Instance Method Details

#add_job(json) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/travis/surveillance/build.rb', line 29

def add_job(json)
  if job = job_for(json['id'])
    return job
  end

  job = Job.new(json.merge({'build' => self}))
  jobs << job
  jobs.sort_by! { |j| j.id }
  job
end

#api_urlObject



99
100
101
# File 'lib/travis/surveillance/build.rb', line 99

def api_url
  @api_url ||= project.api_url + "/builds/#{@id}"
end

#attributes=(attrs = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/travis/surveillance/build.rb', line 40

def attributes=(attrs = {})
  attrs.each do |key, value|
    next if value.nil?
    if key == 'config'
      config.attributes = value
    elsif key == 'matrix'
      value.each do |job|
        add_job(job)
      end
    else
      send("#{key}=", (key[/_at$/] ? Time.parse(value) : value)) if ATTRIBUTES.include?(key.to_sym)
    end
  end
end

#building?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/travis/surveillance/build.rb', line 55

def building?
  result.nil?
end

#configObject



59
60
61
# File 'lib/travis/surveillance/build.rb', line 59

def config
  @config ||= Config.new
end

#durationObject



63
64
65
66
67
68
69
70
71
# File 'lib/travis/surveillance/build.rb', line 63

def duration
  if started_at && finished_at
    finished_at - started_at
  elsif started_at
    Time.now - started_at
  else
    nil
  end
end

#failed?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/travis/surveillance/build.rb', line 73

def failed?
  !result.nil? && !passed?
end

#job_for(id) ⇒ Object



77
78
79
# File 'lib/travis/surveillance/build.rb', line 77

def job_for(id)
  jobs.find { |j| j.id == id }
end

#jobsObject



81
82
83
# File 'lib/travis/surveillance/build.rb', line 81

def jobs
  @jobs ||= []
end

#passed?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/travis/surveillance/build.rb', line 85

def passed?
  !result.nil? && result.zero?
end

#stateObject



89
90
91
92
93
94
95
96
97
# File 'lib/travis/surveillance/build.rb', line 89

def state
  if building?
    'building'
  elsif passed?
    'passed'
  else
    'failed'
  end
end

#urlObject



103
104
105
# File 'lib/travis/surveillance/build.rb', line 103

def url
  @url ||= project.url + "/builds/#{@id}"
end