Class: Drone::Builds::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/drone/builds/parser.rb

Constant Summary collapse

STATUS_COLORS =
{
  success: :light_green,
  running: :light_blue,
  failure: :light_red,
  killed: :light_white,
  pending: :light_yellow
}.freeze
MESSAGE_MAX_WIDTH =
45

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Parser

Returns a new instance of Parser.



17
18
19
# File 'lib/drone/builds/parser.rb', line 17

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/drone/builds/parser.rb', line 5

def client
  @client
end

Instance Method Details

#parse_author(author) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/drone/builds/parser.rb', line 43

def parse_author(author)
  current_github_user = HOME_ENV[:GITHUB_USER]
  if author == current_github_user
    author.colorize(:light_white)
  else
    author
  end
end

#parse_build(build) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/drone/builds/parser.rb', line 25

def parse_build(build)
  {
    number: parse_number(build['number']),
    status: parse_status(build['status']),
    author: parse_author(build['author']),
    ref: parse_ref(build['ref']),
    message: parse_message(build['message'])
  }
end

#parse_builds(builds) ⇒ Object



21
22
23
# File 'lib/drone/builds/parser.rb', line 21

def parse_builds(builds)
  builds.map { |build| parse_build(build) }
end

#parse_message(message) ⇒ Object



56
57
58
59
60
# File 'lib/drone/builds/parser.rb', line 56

def parse_message(message)
  message = message.to_s.lines.first.to_s
  message = message.gsub("of github.com:#{client.owner}/#{client.name}", '')
  message[0..MESSAGE_MAX_WIDTH]
end

#parse_number(number) ⇒ Object



35
36
37
# File 'lib/drone/builds/parser.rb', line 35

def parse_number(number)
  number.to_s
end

#parse_ref(ref) ⇒ Object



52
53
54
# File 'lib/drone/builds/parser.rb', line 52

def parse_ref(ref)
  ref.gsub('refs/heads/', '').gsub('/head', '').gsub('refs/pull/', '#')
end

#parse_status(status) ⇒ Object



39
40
41
# File 'lib/drone/builds/parser.rb', line 39

def parse_status(status)
  status.colorize(STATUS_COLORS[status.to_sym])
end

#table(data, fields) ⇒ Object



62
63
64
# File 'lib/drone/builds/parser.rb', line 62

def table(data, fields)
  Terminal::Table.new(rows: [fields, :separator] + data.map(&:values))
end