Class: Rave::Models::Robot

Inherits:
User show all
Includes:
Rave::Mixins::Controller, Rave::Mixins::DataFormat, Singleton
Defined in:
lib/models/robot.rb

Overview

Contains Robot data, event handlers and cron jobs.

Constant Summary collapse

CONFIG_FILE =

:nodoc:

'config.yaml'

Constants included from Rave::Mixins::DataFormat

Rave::Mixins::DataFormat::PROFILE_JAVA_CLASS

Constants inherited from User

User::NOBODY_ID, User::ROBOT_PATTERN

Constants inherited from Component

Component::GENERATED_PATTERN, Component::GENERATED_PREFIX

Instance Attribute Summary

Attributes inherited from Component

#context

Instance Method Summary collapse

Methods included from Rave::Mixins::Controller

#call

Methods included from Rave::Mixins::Logger

#logger

Methods included from Rave::Mixins::DataFormat

#capabilities_xml, #parse_json_body, #profile_json

Methods inherited from User

#generated?, #image_url, #name, #profile_url, #robot?, #to_json, #to_s

Methods inherited from Component

#generated?, #id, #to_s, #unique_id

Constructor Details

#initializeRobot

:nodoc:



19
20
21
22
23
24
25
26
# File 'lib/models/robot.rb', line 19

def initialize() # :nodoc:
  config = config_from_file
  super(config)
  @handlers = {}
  @cron_jobs = []
  @version = config[:version] || '1'
  register_default_handlers
end

Instance Method Details

#config_from_fileObject

Read options from user-edited yaml config file.



29
30
31
32
33
34
# File 'lib/models/robot.rb', line 29

def config_from_file # :nodoc:
  config = YAML::load(File.open(CONFIG_FILE))
  hash = {}
  config['robot'].each_pair { |k, v| hash[k.to_sym] = v }
  hash
end

#create_wavelet(participants) ⇒ Object

Creates a new wave with initial participants set.

participants

Humans and/or robots to start in the new wave [Array of String/User]

Returns: The new wave, which contains a root wavelet which itself contains a root blip [Wave]



63
64
65
# File 'lib/models/robot.rb', line 63

def create_wavelet(participants)
  @context.create_wavelet(participants)
end

#handle_event(event, context) ⇒ Object

Dispatches events to the appropriate handler



46
47
48
49
50
51
52
53
# File 'lib/models/robot.rb', line 46

def handle_event(event, context) # :nodoc:
  #Ignore unhandled events
  if (handlers = @handlers[event.type])
    handlers.each do |handler|
      self.send(handler, event, context)
    end
  end
end

#register_cron_job(handler, seconds) ⇒ Object

Registers a cron job



56
57
58
# File 'lib/models/robot.rb', line 56

def register_cron_job(handler, seconds)
  @cron_jobs << { :path => "/_wave/cron/#{handler}", :handler => handler, :seconds => seconds }
end

#register_handler(event_type, handler) ⇒ Object

Register a handler. Multiple handlers may be applied to a single event.

event_type

Must be one of Rave::Models::Event::*::TYPE [String]



38
39
40
41
42
43
# File 'lib/models/robot.rb', line 38

def register_handler(event_type, handler)
  raise Rave::InvalidEventException.new("Unknown event: #{event_type}") unless Rave::Models::Event.valid_type?(event_type)
  raise Rave::InvalidHandlerException.new("Unknown handler: #{handler}") unless self.respond_to?(handler)
  @handlers[event_type] ||= []
  @handlers[event_type] << handler unless @handlers[event_type].include?(handler)
end

#versionObject

Version of the robot, as in the yaml config [String]



15
16
17
# File 'lib/models/robot.rb', line 15

def version # :nodoc:
  @version.dup
end