class Glass::SubscriptionNotification

Attributes

collection[RW]
glass_item_id[RW]
google_account[RW]
params[RW]
reply_request_hash[RW]
user_actions[RW]

Public Class Methods

create(params) click to toggle source
# File lib/glass/subscription_notification.rb, line 8
def self.create(params)
  notification = new(params)
  notification.handle!
end
new(params) click to toggle source
# File lib/glass/subscription_notification.rb, line 12
def initialize(params)
  self.params = params
  self.collection = params[:collection]
  self.user_actions = params[:userActions]
  self.google_account = find_google_account(params)
  verify_authenticity!
end

Public Instance Methods

handle!() click to toggle source

Perform the corresponding notification actions

# File lib/glass/subscription_notification.rb, line 28
def handle!
  if collection == "locations"
    handle_location
  else
    self.glass_item_id = params[:itemId]
    handle_reply(params)
    handle_action
  end
end

Private Instance Methods

find_google_account(params) click to toggle source

Find the associated user from userToken

# File lib/glass/subscription_notification.rb, line 70
def find_google_account(params)
  GoogleAccount.find params[:userToken]
end
find_timeline_item(item_id) click to toggle source

Find a given timeline item owned by the user

# File lib/glass/subscription_notification.rb, line 75
def find_timeline_item(item_id)
  Glass::TimelineItem.find_by_glass_item_id_and_google_account_id(item_id, google_account.id)
end
handle_action() click to toggle source

Handle actions on a timeline_item with a given id (custom actions, delete, etc.)

# File lib/glass/subscription_notification.rb, line 57
def handle_action
  timeline_item = find_timeline_item(self.glass_item_id)

  # TODO: Should we uniq these? When glass doesn't have connection, it will queue up
  # actions, so users might press the same one multiple times.
  user_actions.uniq.each do |user_action|
    type = user_action[:type] == "CUSTOM" ? user_action[:payload] : user_action[:type]
    json_to_return = self.reply_request_hash ? self.reply_request_hash : self.params
    timeline_item.send("handles_#{type.downcase}", json_to_return)
  end if user_actions
end
handle_location() click to toggle source
# File lib/glass/subscription_notification.rb, line 39
def handle_location
  google_client = ::Glass::Client.new(google_account: self.google_account)
  google_account.update_location(google_client.get_location)
end
handle_reply(params) click to toggle source
# File lib/glass/subscription_notification.rb, line 44
def handle_reply(params)
  return unless has_user_action? :reply
  google_client = ::Glass::Client.new(google_account: self.google_account)
  self.reply_request_hash = google_client.get_timeline_item(params[:itemId])
  self.glass_item_id = reply_request_hash[:inReplyTo]
end
has_user_action?(action) click to toggle source
# File lib/glass/subscription_notification.rb, line 52
def has_user_action?(action)
  user_actions.select{|user_action| user_action["type"].downcase == action.to_s}.first
end
verify_authenticity!() click to toggle source

Verify authenticity of callback before doing anything

# File lib/glass/subscription_notification.rb, line 80
def verify_authenticity!
  unless params[:verifyToken] == google_account.verification_secret
    raise VerificationError.new("received: #{params[:verifyToken]}")
  end
end