Class: GitHooks::Notifier::JabberClient

Inherits:
Object
  • Object
show all
Includes:
Jabber
Defined in:
lib/git_hooks/notifier/jabber_client.rb

Defined Under Namespace

Classes: Buddy

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jabber_options) ⇒ JabberClient

Returns a new instance of JabberClient.



25
26
27
28
# File 'lib/git_hooks/notifier/jabber_client.rb', line 25

def initialize(jabber_options)
  @jabber_options = jabber_options
  init_jabber_backend!
end

Class Method Details

.deliver(options) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/git_hooks/notifier/jabber_client.rb', line 44

def self.deliver(options)
  commits    = options.delete(:commits)
  recipients = options.delete(:recipients)

  client_instance = new(options)
  client_instance.deliver(commits, recipients)
end

Instance Method Details

#backendObject



65
66
67
# File 'lib/git_hooks/notifier/jabber_client.rb', line 65

def backend
  @backend
end

#buddiesObject



36
37
38
# File 'lib/git_hooks/notifier/jabber_client.rb', line 36

def buddies
  roster.items.map { |roster_item| Buddy.new(roster_item.last.jid, roster) }
end

#create_message(from_commits) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/git_hooks/notifier/jabber_client.rb', line 69

def create_message(from_commits)
  message = "[#{GitHooks::Utils.hostname}] #{from_commits.size} commits have been pushed to '#{from_commits.repo_name}' on '#{from_commits.ref_name}':\n"
  from_commits.each do |commit|
    message << "\t#{commit.id[0...5]}...: '#{commit.short_message}' by #{commit.author.name}\n"
  end

  message
end

#create_recipients(recipients) ⇒ Object



30
31
32
33
34
# File 'lib/git_hooks/notifier/jabber_client.rb', line 30

def create_recipients(recipients)
  [recipients].flatten.map do |recipient|
    recipient.is_a?(Buddy) ? recipient : Buddy.new(recipient, roster)
  end
end

#deliver(commits, recipients) ⇒ Object



52
53
54
55
# File 'lib/git_hooks/notifier/jabber_client.rb', line 52

def deliver(commits, recipients)
  send_message_to create_message(commits), recipients
  true
end

#init_jabber_backend!Object



59
60
61
62
63
# File 'lib/git_hooks/notifier/jabber_client.rb', line 59

def init_jabber_backend!
  @backend = Client.new(JID.new(@jabber_options[:jid]))
  @backend.connect(@jabber_options[:server])
  @backend.auth(@jabber_options[:password])
end

#request_authorization_of(recipient) ⇒ Object



78
79
80
81
82
83
# File 'lib/git_hooks/notifier/jabber_client.rb', line 78

def request_authorization_of(recipient)
  request      = Jabber::Presence.new
  request.to   = recipient.respond_to?(:jid) ? recipient.jid : recipient
  request.type = :subscribe
  backend.send request
end

#rosterObject



40
41
42
# File 'lib/git_hooks/notifier/jabber_client.rb', line 40

def roster
  @roster ||= Roster::Helper.new(backend)
end

#send_message_to(message, recipients) ⇒ Object



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

def send_message_to(message, recipients)
  create_recipients(recipients).each do |recipient|
    subject = "Git commit notification"

    request_authorization_of(recipient) unless recipient.subscribed?

    msg = Message::new(recipient.jid, message).set_type(:normal).set_id('1').set_subject(subject)
    GitHooks::Logger.debug "msg: #{msg}"
    backend.send msg
  end
end