Class: Gitdocs::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/gitdocs/notifier.rb

Overview

Wrapper for the UI notifier

Constant Summary collapse

INFO_ICON =
File.expand_path('../../img/icon.png', __FILE__)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(show_notifications) ⇒ Notifier

Returns a new instance of Notifier.

Parameters:

  • show_notifications (Boolean)


14
15
16
17
# File 'lib/gitdocs/notifier.rb', line 14

def initialize(show_notifications)
  @show_notifications = show_notifications
  Guard::Notifier.turn_on if @show_notifications
end

Class Method Details

.error(title, message) ⇒ Object

Wrapper around #error for a single call to the notifier.

Parameters:

  • title (String)
  • message (String)


9
10
11
# File 'lib/gitdocs/notifier.rb', line 9

def self.error(title, message)
  Gitdocs::Notifier.new(true).error(title, message)
end

Instance Method Details

#error(title, message) ⇒ Object

Parameters:

  • title (String)
  • message (String)


45
46
47
48
49
50
51
52
53
# File 'lib/gitdocs/notifier.rb', line 45

def error(title, message)
  if @show_notifications
    Guard::Notifier.notify(message, title: title, image: :failure)
  else
    Kernel.warn("#{title}: #{message}")
  end
rescue # rubocop:disable Lint/HandleExceptions
  # Prevent StandardErrors from stopping the daemon.
end

#info(title, message) ⇒ Object

Parameters:

  • title (String)
  • message (String)


21
22
23
24
25
26
27
28
29
# File 'lib/gitdocs/notifier.rb', line 21

def info(title, message)
  if @show_notifications
    Guard::Notifier.notify(message, title: title, image: INFO_ICON)
  else
    puts("#{title}: #{message}")
  end
rescue # rubocop:disable Lint/HandleExceptions
  # Prevent StandardErrors from stopping the daemon.
end

#merge_notification(result, root) ⇒ Object

Parameters:

  • result (nil, Symbol, Array<String>, Hash<String => Integer>, #to_s)
  • root (String)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gitdocs/notifier.rb', line 57

def merge_notification(result, root)
  return if result.nil?
  return if result == :no_remote
  return if result == :ok
  return if result == {}

  if result.is_a?(Array)
    warn(
      'There were some conflicts',
      result.map { |f| "* #{f}" }.join("\n")
    )
  elsif result.is_a?(Hash)
    info(
      "Updated with #{change_to_s(result)}",
      "In #{root}:\n#{author_list(result)}"
    )
  else
    error(
      'There was a problem synchronizing this gitdoc',
      "A problem occurred in #{root}:\n#{result}"
    )
  end
end

#push_notification(result, root) ⇒ Object

Parameters:

  • result (nil, Symbol, Hash<String => Integer>, #to_s)

    push operation

  • root (String)


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gitdocs/notifier.rb', line 83

def push_notification(result, root)
  return if result.nil?
  return if result == :no_remote
  return if result == :nothing

  if result == :conflict
    warn("There was a conflict in #{root}, retrying", '')
  elsif result.is_a?(Hash)
    info("Pushed #{change_to_s(result)}", "#{root} has been pushed")
  else
    error("BAD Could not push changes in #{root}", result.to_s)
  end
end

#warn(title, message) ⇒ Object

Parameters:

  • title (String)
  • message (String)


33
34
35
36
37
38
39
40
41
# File 'lib/gitdocs/notifier.rb', line 33

def warn(title, message)
  if @show_notifications
    Guard::Notifier.notify(message, title: title)
  else
    Kernel.warn("#{title}: #{message}")
  end
rescue # rubocop:disable Lint/HandleExceptions
  # Prevent StandardErrors from stopping the daemon.
end