Module: NabaztagHackKit::Mods::Playground

Defined in:
lib/nabaztag_hack_kit/mods/playground.rb

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



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
# File 'lib/nabaztag_hack_kit/mods/playground.rb', line 28

def self.registered(app)
  app.helpers Playground::Helpers

  app.get '/' do
    redirect '/playground'
  end

  app.get '/playground' do
    File.read(public_file('index.html'))
  end

  # API
  app.get '/playground/commands' do # return list of commands
    Message::Api.constants.sort.each_with_object({}) do |constant, hash|
      if constant.to_s.length > 2
        hash[constant] = Message::Api.const_get(constant)
      end
    end.to_json
  end

  app.get '/playground/bunnies' do # return list of bunnies
    Bunny.all.to_json
  end

  app.post '/playground/bunnies/:bunnyid' do #  {"command"=>["40"], "command_values"=>[["1,2,3,4"],[]]}
    if (bunny = Bunny.find(params[:bunnyid]))
      bunny.queue_commands commands(params[:command], params[:command_values])
      bunny.to_json
    end
  end

  app.post '/playground/bunnies' do #  {"bunny"=>["0019db9c2daf"], "command"=>["40"], "command_values"=>[["1,2,3,4"],[]]}
    Array(params[:bunny]).uniq.each do |bunnyid|
      if (bunny = Bunny.find(bunnyid))
        bunny.queue_commands commands(params[:command], params[:command_values])
      end
    end

    redirect '/playground'
  end

  ##################################################################

  app.on 'ping' do |bunny|
    bunny&.next_message!
  end

  app.on 'request' do |_bunny, data|
    if (bunny = Bunny.find_or_initialize_by_id(data[:bunnyid]))
      bunny.seen!
    end
    nil # pass it on
  end
end