module Resque::Plugins::NotiFailure::Server

Public Class Methods

erb_path(filename) click to toggle source
# File lib/resque/plugins/noti_failure/server.rb, line 7
def self.erb_path(filename)
  File.join(File.dirname(__FILE__), 'server', 'views', filename)
end
registered(app) click to toggle source
# File lib/resque/plugins/noti_failure/server.rb, line 11
def self.registered(app)
  app.get '/noti/subscribe' do
    token = ::Noti::Token.create_request_token(to('/noti/confirm'))
    response.set_cookie('noti-request', token.request_token)
    redirect token.redirect_url
  end

  app.get '/noti/confirm' do
    cookie = request.cookies['noti-request']

    unless cookie.nil?
      access_token = ::Noti::Token.get_access_token(cookie)
      Resque.redis.sadd('noti-users', access_token)
      response.delete_cookie('noti-request')
    end

    redirect url_path('noti')
  end

  app.get '/noti' do
    erb File.read(Resque::Plugins::NotiFailure::Server.erb_path('noti.erb'))
  end

  app.get '/noti/test' do
    puts "Failure url : #{Resque::Failure.url}"

    notification = Noti::Notification.new
    notification.title    = 'Test from resque'
    notification.text     = 'Some further information about this notification'
    notification.url      = to('/failed')
    notification.sound    = 'alert1'
    notification.image    = 'example'

    Resque.redis.smembers('noti-users').each do |token|
      begin
        notification.deliver_to token
      rescue Noti::Errors::AccessDenied
        Resque.redis.srem('noti-users', token)
      end
    end

    redirect url_path('noti')
  end
end