Class: OSC::Message
- Inherits:
-
Object
- Object
- OSC::Message
- Defined in:
- lib/ruby-osc/message.rb
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#args ⇒ Object
Returns the value of attribute args.
-
#time ⇒ Object
Returns the value of attribute time.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #encode ⇒ Object
-
#initialize(address = "", *args) ⇒ Message
constructor
A new instance of Message.
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(address = "", *args) ⇒ Message
Returns a new instance of Message.
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
#address ⇒ Object
Returns the value of attribute address.
3 4 5 |
# File 'lib/ruby-osc/message.rb', line 3 def address @address end |
#args ⇒ Object
Returns the value of attribute args.
3 4 5 |
# File 'lib/ruby-osc/message.rb', line 3 def args @args end |
#time ⇒ Object
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
32 33 34 35 36 37 |
# File 'lib/ruby-osc/message.rb', line 32 def self.decode(string) scanner = StringScanner.new string msg = scanner raise DecodeError unless scanner.eos? 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 80 81 |
# File 'lib/ruby-osc/message.rb', line 39 def self.(scanner) pos = scanner.pos begin address, = (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 = [] .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 "d" args.push scanner.scan(/.{8}/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 |
#encode ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ruby-osc/message.rb', line 12 def encode objs, , dirs = @args.collect { |arg| OSC.encoding_directive arg }.transpose dirs ||= [] and objs ||= [] [",#{ and .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_a ⇒ Object
29 |
# File 'lib/ruby-osc/message.rb', line 29 def to_a; @args.dup.unshift(@address) end |
#to_s ⇒ Object
30 |
# File 'lib/ruby-osc/message.rb', line 30 def to_s; "OSC::Message(#{ args.join(', ') })" end |