Module: Resque::Plugins::ResqueSliders::Server

Defined in:
lib/resque-sliders/server.rb

Constant Summary collapse

VIEW_PATH =
File.join(File.dirname(__FILE__), 'server', 'views')
PUBLIC_PATH =
File.join(File.dirname(__FILE__), 'server', 'public')

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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 "/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

  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='')
      begin
        cache_control :public, :max_age => 1800
        file = File.join(PUBLIC_PATH, dir, filename)
        send_file file, :last_modified => File.mtime(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