Sha256: 16b6a5eee546f09966648386e79d1c8a9ed667e3ed4c50ce98243ba3c10e9d66

Contents?: true

Size: 977 Bytes

Versions: 4

Compression:

Stored size: 977 Bytes

Contents

require 'nabaztag_hack_kit/message'

module NabaztagHackKit
  class Bunny
    @bunnies = {}

    attr_reader :id, :last_seen, :queued_commands

    class << self
      def all
        @bunnies.values
      end

      def find(id)
        @bunnies[id]
      end

      def find_or_initialize_by_id(id)
        find(id) || Bunny.new(id)
      end

      def add(bunny)
        @bunnies[bunny.id] = bunny
      end
    end

    def initialize(id)
      @id              = id
      @queued_commands = []

      Bunny.add(self)
    end

    def seen!
      @last_seen = Time.now
    end

    def queue_commands(commands)
      @queued_commands << commands
    end

    def next_message!
      Message.build(*@queued_commands.shift || Message::Api::OK)
    end

    def to_json(state = nil, deepth = nil)
      {
        :id                   => id,
        :last_seen            => last_seen,
        :queued_commands_size => queued_commands.size,
      }.to_json
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nabaztag_hack_kit-0.1.0.beta8 lib/nabaztag_hack_kit/bunny.rb
nabaztag_hack_kit-0.1.0.beta3 lib/nabaztag_hack_kit/bunny.rb
nabaztag_hack_kit-0.1.0.beta2 lib/nabaztag_hack_kit/bunny.rb
nabaztag_hack_kit-0.1.0.beta1 lib/nabaztag_hack_kit/bunny.rb