Module: Rave::Mixins::DataFormat

Includes:
Logger
Included in:
Rave::Models::Robot
Defined in:
lib/mixins/data_format.rb

Constant Summary collapse

PROFILE_JAVA_CLASS =
'com.google.wave.api.ParticipantProfile'

Instance Method Summary collapse

Methods included from Logger

#logger

Instance Method Details

#capabilities_xmlObject

Returns this robot’s capabilities in XML



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mixins/data_format.rb', line 10

def capabilities_xml
  xml = Builder::XmlMarkup.new
  xml.instruct!
  xml.tag!("w:robot", "xmlns:w" => "http://wave.google.com/extensions/robots/1.0") do
    xml.tag!("w:version", @version)
    xml.tag!("w:capabilities") do
      @handlers.keys.each do |capability|
        xml.tag!("w:capability", "name" => capability)
      end  
    end
    unless @cron_jobs.empty?
      xml.tag!("w:crons") do
        @cron_jobs.each do |job|
          xml.tag!("w:cron", "path" => job[:path], "timerinseconds" => job[:seconds])
        end
      end
    end
    attrs = { "name" => @name }
    attrs["imageurl"] = @image_url if @image_url
    attrs["profileurl"] = @profile_url if @profile_url
    xml.tag!("w:profile", attrs)
  end
end

#parse_json_body(json) ⇒ Object

Parses context and event info from JSON input



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mixins/data_format.rb', line 45

def parse_json_body(json)
  logger.info("Received:\n#{json.to_s}")
  data = JSON.parse(json)
  #Create Context
  context = context_from_json(data)
  #Create events
  events = events_from_json(data, context)
  logger.info("Structure (before):\n#{context.print_structure}")
  logger.info("Events: #{events.map { |e| e.type }.join(', ')}")
  return context, events
end

#profile_jsonObject

Returns the robot’s profile in json format



35
36
37
38
39
40
41
42
# File 'lib/mixins/data_format.rb', line 35

def profile_json
  {
    'name' => @name,
    'imageUrl' => @image_url,
    'profileUrl' => @profile_url,
    'javaClass' => PROFILE_JAVA_CLASS,
  }.to_json.gsub('\/','/')
end