Class: IRuby::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/iruby/message.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg_dict) ⇒ Message

A Message can be created from a dict and a dict from a Message instance simply by calling dict(msg_obj).“”“



8
9
10
11
12
13
14
15
16
# File 'lib/iruby/message.rb', line 8

def initialize msg_dict
  @dct = {}
  msg_dict.each_pair do |k, v|
    if v.is_a?(Hash)
      v = Message.new(v)
    end
    @dct[k] = v
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



18
19
20
# File 'lib/iruby/message.rb', line 18

def method_missing(m, *args, &block)
  @dct[m.to_s]
end

Class Method Details

.extract_header(msg_or_header) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/iruby/message.rb', line 30

def self.extract_header(msg_or_header)
  # Given a message or header, return the header.
  if msg_or_header.nil?
    return {}
  end
  # See if msg_or_header is the entire message.
  h = msg_or_header['header']
  # See if msg_or_header is just the header
  #h ||= msg_or_header['msg_id']
  h ||= msg_or_header

  return h
end

.msg_header(msg_id, username, session) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/iruby/message.rb', line 22

def self.msg_header(msg_id, username, session)
  return {
    msg_id: msg_id,
    username: username,
    session: session
  }
end