Module: Pushes

Defined in:
lib/pushes.rb,
lib/pushes/version.rb

Defined Under Namespace

Classes: Config, LaunchAgent, Notifier

Constant Summary collapse

DEFAULT_COMMAND =
'start'
VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.command_fetchObject

Commands



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pushes.rb', line 38

def self.command_fetch
  if first_run?
    config.initiate
    store_push_events
    notify_initiated
  else
    notify_push_events
    store_push_events
  end
rescue
end

.command_resetObject



62
63
64
65
66
# File 'lib/pushes.rb', line 62

def self.command_reset
  config.clear_storage
  command_stop
  command_start([10])
end

.command_start(args) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/pushes.rb', line 50

def self.command_start(args)
  interval = args.first.to_i
  start_interval = interval > 0 ? interval : 10

  launch_agent.start(start_interval)
  command_fetch
end

.command_stopObject



58
59
60
# File 'lib/pushes.rb', line 58

def self.command_stop
  launch_agent.stop
end

.configObject



127
128
129
# File 'lib/pushes.rb', line 127

def self.config
  @config ||= Config.new
end

.display_helpObject



135
136
137
# File 'lib/pushes.rb', line 135

def self.display_help
  say(help_message + "\n")
end

.display_versionObject



139
140
141
# File 'lib/pushes.rb', line 139

def self.display_version
  say("Pushes #{Pushes::VERSION}" + "\n")
end

.first_run?Boolean

Utilities

Returns:

  • (Boolean)


69
70
71
# File 'lib/pushes.rb', line 69

def self.first_run?
  !config.initiated?
end

.githubObject



131
132
133
# File 'lib/pushes.rb', line 131

def self.github
  @github ||= Octokit::Client.new(login: config., oauth_token: config.token)
end

.help_messageObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/pushes.rb', line 143

def self.help_message
  help = []
  help << 'Usage:'
  help << '  pushes COMMAND'
  help << ''
  help << 'Options:'
  help << '  -h, --help - Display help'
  help << ''
  help << 'Commands:'
  help << 'fetch            - Fetch new commits from GitHub'
  help << 'start [INTERVAL] - Start a LaunchAgent background process [INTERVAL in seconds. Default: 10]'
  help << 'stop             - Stop background process'

  help.join("\n")
end

.launch_agentObject



119
120
121
# File 'lib/pushes.rb', line 119

def self.launch_agent
  @launch_agent ||= LaunchAgent.new
end

.notifierObject



123
124
125
# File 'lib/pushes.rb', line 123

def self.notifier
  @notifier ||= Notifier.new
end

.notify_initiatedObject



115
116
117
# File 'lib/pushes.rb', line 115

def self.notify_initiated
  notifier.notify("You’re all set, just wait for new commits.\n~ Pushes", title: 'Ahoy Captain!')
end

.notify_push_eventsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pushes.rb', line 90

def self.notify_push_events
  push_events.each do |event|
    next if stored_push_events.include? event.id

    user = event.actor.
    branch = event.payload.ref.match(/[^\/]+$/)

    repo = event.repo
    commits = event.payload.commits

    if commits.size == 1
      url = "https://github.com/#{repo.name}/commit/#{commits.first.sha}"
    else
      parent_commit_sha = github.commit(repo.name, commits.first.sha).parents.first.sha
      url = "https://github.com/#{repo.name}/compare/#{parent_commit_sha[0..9]}...#{commits.last.sha[0..9]}"
    end

    title = repo.name
    commits_text = "commit#{'s' if commits.size > 1}"
    message = "#{user} pushed #{commits.size} #{commits_text} to #{branch}"

    notifier.notify(message, title: title, open: url)
  end
end

.push_eventsObject



73
74
75
# File 'lib/pushes.rb', line 73

def self.push_events
  @push_events ||= received_push_events
end

.received_push_eventsObject



86
87
88
# File 'lib/pushes.rb', line 86

def self.received_push_events
  github.received_events(config.).select { |e| e.type == 'PushEvent' }
end

.run(argv) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pushes.rb', line 12

def self.run(argv)
  command = argv.first || DEFAULT_COMMAND
  args = argv - [command]

  if %w(--help -h).include?(command)
    display_help
    return 1
  end

  if %w(--version -v).include?(command)
    display_version
    return 1
  end

  begin
    send("command_#{command}", args)
  rescue ArgumentError
    send("command_#{command}")
  rescue NoMethodError
    say "error: Unknown command '#{command}'"
  end

  1
end

.store_push_eventsObject



77
78
79
80
# File 'lib/pushes.rb', line 77

def self.store_push_events
  @stored_push_events = push_events.map(&:id)
  config.store(@stored_push_events)
end

.stored_push_eventsObject



82
83
84
# File 'lib/pushes.rb', line 82

def self.stored_push_events
  @stored_push_events ||= config.storage
end