module Resque::Plugins::ResqueSliders::Server

Constants

PUBLIC_PATH
VIEW_PATH

Public Class Methods

registered(app) click to toggle source
# File lib/resque-sliders/server.rb, line 9
def self.registered(app)
  require 'json'

  app.get '/sliders' do
    key = params.keys.first
    if %w(img css js).include? key
      public_view(params[key], key == 'img' ? 'images' : key)
    else
      @sliders = Commander.new
      redirect url_path("/sliders/#{@sliders.all_hosts.first}") if @sliders.all_hosts.length == 1
      slider_view :index
    end
  end

  app.get '/sliders/:host' do
    @sliders = Commander.new
    slider_view :index
  end

  app.post '/sliders/:host' do
    signals = params.reject { |x,y| x unless %w(pause stop play reload).include? x.to_s and y }
    if params[:quantity] && params[:queue]
      sliders = Commander.new
      queue = params[:queue].split.first
      quantity = params[:quantity].to_i
      if quantity.zero?
        sliders.delete(params[:host], queue)
      else
        sliders.change(params[:host], queue, quantity)
      end
    elsif signals.length == 1
      sliders = Commander.new
      sig = signals.keys.first.to_s
      sliders.set_signal_flag(sig, params[:host])
      content_type :json
      {:signal => sig, :host => params[:host]}.to_json
    end
  end

  # we need the ability to remove old hosts
  app.delete '/sliders/:host' do
    Commander.new.remove_all_host_keys(params[:host])
  end

  app.helpers do
    def slider_view(filename, options={}, locals={})
      erb(File.read(File.join(VIEW_PATH, "#{filename}.erb")), options, locals)
    end

    def public_view(filename, dir='')
      file = File.join(PUBLIC_PATH, dir, filename)
      begin
        cache_control :public, :max_age => 1800
        send_file file
      rescue Errno::ENOENT
        404
      end
    end

    def daemon_buttons(host, list=true)
      html_out = []
      icon_base = 'ui-icon ui-corner-all ui-state-default'
      case
        when @sliders.reload?(host)
          %w(pause stop alert)
        when (@sliders.pause?(host) or @sliders.stop?(host))
          %w(play stop refresh)
        else
          %w(pause stop refresh)
      end.each do |i|
        id = "#{host}:#{i.upcase}"
        klass = "#{icon_base} ui-icon-#{i}"
        klass += ' corner' unless list
        html_out << "<span id=\"#{id}\" class=\"#{klass}\"></span>"
      end
      if list
        '<li class="icons">' + html_out.join("</li><li class=\"icons\">") + '</li>'
      else
        html_out.reverse.join
      end
    end
  end

  app.tabs << "Sliders"

end

Public Instance Methods

daemon_buttons(host, list=true) click to toggle source
# File lib/resque-sliders/server.rb, line 68
def daemon_buttons(host, list=true)
  html_out = []
  icon_base = 'ui-icon ui-corner-all ui-state-default'
  case
    when @sliders.reload?(host)
      %w(pause stop alert)
    when (@sliders.pause?(host) or @sliders.stop?(host))
      %w(play stop refresh)
    else
      %w(pause stop refresh)
  end.each do |i|
    id = "#{host}:#{i.upcase}"
    klass = "#{icon_base} ui-icon-#{i}"
    klass += ' corner' unless list
    html_out << "<span id=\"#{id}\" class=\"#{klass}\"></span>"
  end
  if list
    '<li class="icons">' + html_out.join("</li><li class=\"icons\">") + '</li>'
  else
    html_out.reverse.join
  end
end
public_view(filename, dir='') click to toggle source
# File lib/resque-sliders/server.rb, line 58
def public_view(filename, dir='')
  file = File.join(PUBLIC_PATH, dir, filename)
  begin
    cache_control :public, :max_age => 1800
    send_file file
  rescue Errno::ENOENT
    404
  end
end
slider_view(filename, options={}, locals={}) click to toggle source
# File lib/resque-sliders/server.rb, line 54
def slider_view(filename, options={}, locals={})
  erb(File.read(File.join(VIEW_PATH, "#{filename}.erb")), options, locals)
end