mattman (owner)

Revisions

gist: 210028 Download_button fork
public
Public Clone URL: git://gist.github.com/210028.git
Embed All Files: show embed
notifiers/sms.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require 'httparty'
class SMS
  include HTTParty
  
  def self.send(game, player)
    if !player.mobile.nil?
      message = "Hey #{player.name_first},\nYour game for today has changed! Details are\n Time: #{game.played_at.strftime("%H:%M")}\n Venue:#{game.venue.loc_name}"
      message = CGI::escape(message)
      url = "http://sms.didcoe.id.au/sendto/#{api}/#{player.mobile}/#{message}"
      res = get(url)
      res != "Fuck off :)" ? true : false
    end
  end
 
end
observers/contact.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# This models purpose is to sit there and observe
# (by usinging the observable gem from James Golick)
# changes to the time of games and action as needed
 
class Contact
  
  Contact::SMSACTIVE = true
  
  observes :game, :invokes => :notify_players, :after => :update
  
  def self.notify_players(game)
    now = Time.now
    # give me an integer respresentation (no decimal places) of the time between now and the match
    timespan = (game.played_at - now).to_i
    if timespan < 3.hours
      # SMS the user
      Contact.send_later(:send_change_notification_sms, game) # send a DJ worker the signal to queue this job up for sending
    else
      # email the user
      Contact.send_later(:send_change_notification_email, game) # send a DJ worker the signal to queue this job up for sending
    end
  end
  
  def self.send_change_notification_sms(game)
    if SMSACTIVE
      players = find_players(game)
      players.each do |player|
        SMS.send(game, player)
      end
    else
      puts "SMS IS NOT CURRENTLY TURNED ON FOR #{RAILS_ENV} - you can update this in the resepective config/environment/#{RAILS_ENV.downcase}.rb file"
    end
  end
    
  def self.send_change_notification_email(game)
    players = find_players(game)
    players.each do |player|
      ChangeMailer.deliver_timechange_email(player, game)
    end
  end
  
  def self.find_players(game)
    [game.team_home.players, game.team_away.players].flatten!
  end
  
end