Class: UserObserver

Inherits:
ActiveRecord::Observer
  • Object
show all
Defined in:
app/models/user_observer.rb

Instance Method Summary collapse

Instance Method Details

#after_update(user) ⇒ Object


4
5
6
7
8
# File 'app/models/user_observer.rb', line 4

def after_update(user)
  unless user.no_send_email == true
    publish(:update, user)
  end
end

#before_save(user) ⇒ Object


10
11
12
13
14
# File 'app/models/user_observer.rb', line 10

def before_save(user)
  unless user.no_send_email == true
    send_user_confirmed_email(user) if user.confirmed_at_changed?
  end
end

#publish(type, user) ⇒ Object


16
17
18
19
20
21
22
23
24
# File 'app/models/user_observer.rb', line 16

def publish(type, user)
  Juggernaut.url = ENV['JUGG_URL']
  Juggernaut.publish("/observer/user/#{user.id}", {
    id: user.id,
    type: type,
    klass: user.class.name,
    record: user.changes
  })
end

#send_user_confirmed_email(user) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/user_observer.rb', line 26

def send_user_confirmed_email(user)
  unless Rails.env.test?
    $statsd.increment 'user.activated'
    Thread.new do
      url = "http://sendgrid.com/api/mail.send.json?api_user=#{ENV['SENDGRID_USERNAME']}&api_key=#{ENV['SENDGRID_PASSWORD']}&[email protected]&subject=Hospitium%20-%20User%20Confirmed&text=#{URI::encode(user.username)}%20confirmed%20an%20account.%20#{URI::encode(user.email)}%20in%20organization%20#{URI::encode(user.organization_name)}&[email protected]"
      resp = Net::HTTP.get_response(URI.parse(url))
      data = resp.body
      result = JSON.parse(data)
      if result.has_key? 'Error'
         raise 'web service error'
      end
    end
  end
end