Class: Dri::Commands::Fetch::Failures

Inherits:
Dri::Command show all
Defined in:
lib/dri/commands/fetch/failures.rb

Instance Attribute Summary

Attributes inherited from Dri::Command

#config, #emoji, #profile, #timezone, #token, #username

Instance Method Summary collapse

Methods inherited from Dri::Command

#add_color, #api_client, #command, #cursor, #editor, #logger, #pastel, #prompt, #spinner, #verify_config_exists

Constructor Details

#initialize(options) ⇒ Failures

Returns a new instance of Failures.



11
12
13
14
# File 'lib/dri/commands/fetch/failures.rb', line 11

def initialize(options)
  @options = options
  @today_iso_format = Time.now.strftime('%Y-%m-%dT00:00:00Z')
end

Instance Method Details

#execute(input: $stdin, output: $stdout) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dri/commands/fetch/failures.rb', line 16

def execute(input: $stdin, output: $stdout)
  verify_config_exists

  title = add_color('Title', :bright_yellow)
  triaged = add_color('Triaged?', :bright_yellow)
  author = add_color('Author', :bright_yellow)
  url = add_color('URL', :bright_yellow)

  failures = []
  labels = [ title, triaged, author, url ]
  triaged_counter = 0

  logger.info "Fetching today\'s failures..."
  
  spinner.run do 

    response = api_client.fetch_failures(date: @today_iso_format, state: 'opened')

    if response.nil?
      logger.info "Life is great, there are no new failures today!"
      exit 0
    end

    response.each do |failure|
      title = truncate(failure["title"], 60)
      author = failure["author"]["username"]
      url = failure["web_url"]
      award_emoji_url = failure["_links"]["award_emoji"]
      triaged = add_color('x', :red)

      emoji_awards = api_client.fetch_awarded_emojis(award_emoji_url)

      triaged = add_color('', :green) && triaged_counter += 1 if emoji_awards.find do |e| 
        e['name'] == emoji && e['user']['username'] == @username 
      end

      failures << [title, triaged, author, url]
    end
  end

  table = TTY::Table.new(labels, failures)
  puts table.render(:ascii, resize: true, alignments: [:center, :center, :center, :center])
  output.puts "\nFound: #{failures.size} failures, of these #{triaged_counter} have been triaged with a #{emoji}."
end