Skip to content

Instantly share code, notes, and snippets.

@johnhamelink
Created February 24, 2016 17:11
Show Gist options
  • Select an option

  • Save johnhamelink/5629454ad80b3ae98c70 to your computer and use it in GitHub Desktop.

Select an option

Save johnhamelink/5629454ad80b3ae98c70 to your computer and use it in GitHub Desktop.
class UpdatePlayerService
def initialize
@pn_service = PlayerPushNotification
end
def update(current_user: nil, player: nil, changes: nil)
fail '[UpdatePlayerService#update] Must supply current_user' if current_user.nil?
fail '[UpdatePlayerService#update] Must supply player' if player.nil?
fail '[UpdatePlayerService#update] Must supply changes' if changes.nil?
player.update_attributes(changes)
push_if_necessary('update', current_user, player, changes)
player
end
def create(current_user: nil, player: nil, initial_creation: false)
fail '[UpdatePlayerService#create] Must supply current_user' if current_user.nil?
fail '[UpdatePlayerService#create] Must supply player' if player.nil?
player.save
push_if_necessary('create', current_user, player, initial_creation)
player
end
private
# Only send a Push Notification if the changes made in the last
# player update have modified the state of the player.
def push_if_necessary(kind, current_user, player, changes)
case kind
when 'update'
return if changes[:state].nil? || !player.state_changed?
@pn_service.new(
player: player,
current_user: current_user,
data: player.play.to_uri
)
return
when 'create'
@pn_service.new(
player: player,
current_user: current_user,
data: player.play.to_uri,
initial_creation: changes
)
return
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment