Class: OSC::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-osc/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address = '', *args) ⇒ Message

Returns a new instance of Message.

Raises:

  • (TypeError)


5
6
7
8
9
10
# File 'lib/ruby-osc/message.rb', line 5

def initialize address = '', *args
  args.collect! { |arg| OSC.coerce_argument arg }
  args.flatten! # won't harm we're not accepting arrays anyway, in case an custom coerced arg coerces to Array eg. Hash
  raise(TypeError, "Expected address to be a string") unless String === address
  @address, @args = address, args
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



3
4
5
# File 'lib/ruby-osc/message.rb', line 3

def address
  @address
end

#argsObject

Returns the value of attribute args.



3
4
5
# File 'lib/ruby-osc/message.rb', line 3

def args
  @args
end

#timeObject

Returns the value of attribute time.



3
4
5
# File 'lib/ruby-osc/message.rb', line 3

def time
  @time
end

Class Method Details

.decode(string) ⇒ Object

Raises:



32
33
34
35
36
37
# File 'lib/ruby-osc/message.rb', line 32

def self.decode string
  scanner        = StringScanner.new string
  msg            = self.decode_message scanner
  raise DecodeError if not scanner.eos?
  return msg
end

.decode_message(scanner) ⇒ Object



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
# File 'lib/ruby-osc/message.rb', line 39

def self.decode_message scanner
  pos = scanner.pos
  begin
    address, tags  = (1..2).map do
      string       = scanner.scan(/[^\000]+\000/)
      raise DecodeError, "no address or tags" if string.nil?
      scanner.pos += OSC.padding_size(string.bytesize)
      string.chomp("\000")
    end

    args = []
    tags.scan(/\w/) do |tag|
      case tag
      when 'i'
        int = scanner.scan(/.{4}/nm).unpack('N').first
        args.push( int > (2**31-1) ? int - 2**32 : int )
      when 'f'
        args.push scanner.scan(/.{4}/nm).unpack('g').first
      when 's'
        str = scanner.scan(/[^\000]+\000/)
        scanner.pos += OSC.padding_size(str.size)
        str = str.respond_to?(:force_encoding) ? str.force_encoding('UTF-8') : str
        args.push str.chomp("\000")
      when 'b'
        size = scanner.scan(/.{4}/).unpack('N').first
        str  = scanner.scan(/.{#{ size }}/nm)
        scanner.pos += OSC.padding_size(size + 4)
        args.push Blob.new(str)
      else
        raise DecodeError, "#{ tag } is not a known tag"
      end
    end
    new address, *args
  end
rescue DecodeError => e
  scanner.pos = pos
  raise e
rescue => e
  scanner.pos = pos
  raise DecodeError, e
end

Instance Method Details

#==(other) ⇒ Object



25
26
27
# File 'lib/ruby-osc/message.rb', line 25

def == other
  self.class == other.class and to_a == other.to_a
end

#encodeObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ruby-osc/message.rb', line 12

def encode
  objs, tags, dirs = @args.collect { |arg| OSC.encoding_directive arg }.transpose
  dirs ||= [] and objs ||= []

  [",#{ tags and tags.join }", @address].each do |str|
    obj, tag, dir = OSC.encoding_directive str
    objs.unshift obj
    dirs.unshift dir
  end

  objs.flatten.compact.pack dirs.join
end

#to_aObject



29
# File 'lib/ruby-osc/message.rb', line 29

def to_a; @args.dup.unshift(@address) end

#to_sObject



30
# File 'lib/ruby-osc/message.rb', line 30

def to_s; "OSC::Message(#{ args.join(', ') })" end