Module: OBS::WebSocket::Protocol::Types

Defined in:
lib/obs/websocket.rb,
lib/obs/websocket/protocol.rb

Constant Summary collapse

Boolean =
Type.new(name: 'Boolean') do
  def as_ruby(b)
    !!b
  end

  def as_json(b)
    !!b
  end
end
Integer =
Type.new(name: 'Int') do
  def as_ruby(i)
    i.to_i
  end

  def as_json(i)
    i.to_i
  end
end
Float =
Type.new(name: 'Double') do
  def as_ruby(f)
    f.to_f
  end

  def as_json(f)
    f.to_f
  end
end
Numeric =
Float
String =
Type.new(name: 'String') do
  def as_ruby(s)
    s.to_s
  end

  def as_json(s)
    s.to_s
  end
end
Optional =
Class.new do
  def [](element_type)
    Type.new(name: "Optional[#{element_type.name}]", element_type: element_type) do
      def as_ruby(v)
        return unless v
        @element_type.as_ruby(v)
      end

      def as_json(v)
        return unless v
        @element_type.as_json(v)
      end
    end
  end
end.new
Array =
Class.new do
  def [](element_type)
    Type.new(name: "Array[#{element_type.name}]", element_type: element_type) do
      def as_ruby(a)
        a.to_a.map {|v| @element_type.as_ruby(v) }
      end

      def as_json(a)
        a.to_a.map {|v| @element_type.as_json(v) }
      end
    end
  end
end.new
Object =
Type.new(name: 'Object') do
  def as_ruby(o)
    o.to_h
  end

  def as_json(o)
    o.to_h
  end

  def [](fields)
    Type.new(name: "Object[#{fields.keys.map(&:to_s).join(', ')}]", fields: fields) do
      def as_ruby(o)
        @fields.to_h do |name, field|
          json_name = field[:json_name]
          type = field[:type]
          [name, type.as_ruby(o[json_name])]
        end
      end

      def as_json(a)
        @fields.to_h do |name, field|
          json_name = field[:json_name]
          type = field[:type]
          [json_name, type.as_ruby(o[name])]
        end
      end
    end
  end
end
StringOrObject =
Type.new(name: 'Object') do
  def as_ruby(o)
    String === o ? o : o.to_h
  end

  def as_json(o)
    o.respond_to?(:to_str) ? o.to_str : o.to_h
  end

  def [](fields)
    Type.new(name: "Object[#{fields.keys.map(&:to_s).join(', ')}]", fields: fields) do
      def as_ruby(o)
        return o if String === o

        @fields.to_h do |name, field|
          json_name = field[:json_name]
          type = field[:type]
          [name, type.as_ruby(o[json_name])]
        end
      end

      def as_json(a)
        return o.to_str if o.respond_to?(:to_str)

        @fields.to_h do |name, field|
          json_name = field[:json_name]
          type = field[:type]
          [json_name, type.as_ruby(o[name])]
        end
      end
    end
  end
end
SceneItem =
Type.new(name: "SceneItem") do
  def as_ruby(o)
    SceneItem__BASE.as_ruby(o)
  end
  def as_json(o)
    SceneItem__BASE.as_json(o)
  end
end
SceneItemTransform =
Type.new(name: "SceneItemTransform") do
  def as_ruby(o)
    SceneItemTransform__BASE.as_ruby(o)
  end
  def as_json(o)
    SceneItemTransform__BASE.as_json(o)
  end
end
OBSStats =
Type.new(name: "OBSStats") do
  def as_ruby(o)
    OBSStats__BASE.as_ruby(o)
  end
  def as_json(o)
    OBSStats__BASE.as_json(o)
  end
end
Output =
Type.new(name: "Output") do
  def as_ruby(o)
    Output__BASE.as_ruby(o)
  end
  def as_json(o)
    Output__BASE.as_json(o)
  end
end
ScenesCollection =
Type.new(name: "ScenesCollection") do
  def as_ruby(o)
    ScenesCollection__BASE.as_ruby(o)
  end
  def as_json(o)
    ScenesCollection__BASE.as_json(o)
  end
end
Scene =
Type.new(name: "Scene") do
  def as_ruby(o)
    Scene__BASE.as_ruby(o)
  end
  def as_json(o)
    Scene__BASE.as_json(o)
  end
end