class GitAgent

Constants

URLS
VERSION

Attributes

data_directory[RW]
homepage[R]
project[R]
url[R]
username[R]

Public Class Methods

new(args = {}) click to toggle source
# File lib/git_agent.rb, line 19
def initialize(args = {})
  @homepage = args[:homepage]
  @url, @username, @project = resolve_arguments(args)
end

Public Instance Methods

diff() click to toggle source
# File lib/git_agent.rb, line 24
def diff
  old_hash, new_hash = read_from_record, ls_remote
  old_keys, new_keys = old_hash.keys, new_hash.keys

  same_keys = new_keys & old_keys
  result = {
    added: new_keys - same_keys,
    deleted: old_keys - same_keys,
    updated: []
  }

  same_keys.inject(result[:updated]) do |memo, key|
    memo << key if old_hash[key] != new_hash[key]
    memo
  end

  record(new_hash)

  result
end
notify_if_different!() click to toggle source
# File lib/git_agent.rb, line 45
def notify_if_different!
  result = diff

  unless result.values.flatten.empty?
    notification = GitAgent::Notification.new(project, homepage, result)
    notification.send!
  end
end

Private Instance Methods

execute_git_ls_remote() click to toggle source
# File lib/git_agent.rb, line 79
def execute_git_ls_remote
  `git ls-remote #{url}`
end
ls_remote() click to toggle source
# File lib/git_agent.rb, line 56
def ls_remote
  result = execute_git_ls_remote.split("\n")
  result.inject({}) do |memo, line|
    arr = line.split("\t")
    memo[arr.last] = arr.first
    memo
  end
end
path() click to toggle source
# File lib/git_agent.rb, line 75
def path
  @path ||= File.join(GitAgent.data_directory, username)
end
read_from_record() click to toggle source
# File lib/git_agent.rb, line 65
def read_from_record
  return {} unless Dir.exist?(path)
  File.open(File.join(path, project)) { |file| YAML.load(file.read) }
end
record(hash) click to toggle source
# File lib/git_agent.rb, line 70
def record(hash)
  FileUtils.mkdir_p(path) unless Dir.exist?(path)
  File.open(File.join(path, project), 'w') { |file| file.write(YAML.dump(hash)) }
end
resolve_arguments(urls) click to toggle source
# File lib/git_agent.rb, line 83
def resolve_arguments(urls)
  if urls.key?(:git)
    username_and_project = urls[:git].split(':').last.split('/')
    return [urls[:git], username_and_project.first, username_and_project.last]
  end

  urls = urls.first
  raise ArgumentError, 'Need at least :git, :github or :bitbucket' unless URLS.key?(urls.first)

  username_and_project = urls.last.split("/")

  [
    "git@#{URLS[urls.first]}:#{urls.last}.git",
    username_and_project.first,
    "#{username_and_project.last}.git"
  ]
end