Compare commits

23 Commits

Author SHA1 Message Date
3c892623fa Merge pull request #6 from onyx-and-iris/dependabot/bundler/rexml-3.4.2
Bump rexml from 3.3.9 to 3.4.2
2026-03-17 21:16:57 +00:00
dependabot[bot]
92b36ecce4 Bump rexml from 3.3.9 to 3.4.2
Bumps [rexml](https://github.com/ruby/rexml) from 3.3.9 to 3.4.2.
- [Release notes](https://github.com/ruby/rexml/releases)
- [Changelog](https://github.com/ruby/rexml/blob/master/NEWS.md)
- [Commits](https://github.com/ruby/rexml/compare/v3.3.9...v3.4.2)

---
updated-dependencies:
- dependency-name: rexml
  dependency-version: 3.4.2
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-03-17 21:15:05 +00:00
1dc8bf10f7 Merge pull request #5 from onyx-and-iris/dependabot/bundler/rexml-3.3.9
Bump rexml from 3.3.6 to 3.3.9
2025-03-27 15:46:02 +00:00
dependabot[bot]
426a7d6628 Bump rexml from 3.3.6 to 3.3.9
Bumps [rexml](https://github.com/ruby/rexml) from 3.3.6 to 3.3.9.
- [Release notes](https://github.com/ruby/rexml/releases)
- [Changelog](https://github.com/ruby/rexml/blob/master/NEWS.md)
- [Commits](https://github.com/ruby/rexml/compare/v3.3.6...v3.3.9)

---
updated-dependencies:
- dependency-name: rexml
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-03-27 15:45:47 +00:00
e1146913f9 Merge pull request #4 from onyx-and-iris/dependabot/bundler/rexml-3.3.6
Bump rexml from 3.3.3 to 3.3.6
2024-08-24 14:37:10 +01:00
dependabot[bot]
78ca03e610 Bump rexml from 3.3.3 to 3.3.6
Bumps [rexml](https://github.com/ruby/rexml) from 3.3.3 to 3.3.6.
- [Release notes](https://github.com/ruby/rexml/releases)
- [Changelog](https://github.com/ruby/rexml/blob/master/NEWS.md)
- [Commits](https://github.com/ruby/rexml/compare/v3.3.3...v3.3.6)

---
updated-dependencies:
- dependency-name: rexml
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-08-24 13:37:01 +00:00
03174b9e40 Merge pull request #3 from onyx-and-iris/dependabot/bundler/rexml-3.3.3
Bump rexml from 3.3.0 to 3.3.3
2024-08-06 11:37:52 +01:00
dependabot[bot]
63dd131d61 Bump rexml from 3.3.0 to 3.3.3
Bumps [rexml](https://github.com/ruby/rexml) from 3.3.0 to 3.3.3.
- [Release notes](https://github.com/ruby/rexml/releases)
- [Changelog](https://github.com/ruby/rexml/blob/master/NEWS.md)
- [Commits](https://github.com/ruby/rexml/compare/v3.3.0...v3.3.3)

---
updated-dependencies:
- dependency-name: rexml
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-08-06 10:37:37 +00:00
6ffae8e6c7 Merge pull request #2 from onyx-and-iris/dependabot/bundler/rexml-3.3.0
Bump rexml from 3.2.5 to 3.3.0
2024-06-21 08:32:05 +01:00
dependabot[bot]
2878c31819 Bump rexml from 3.2.5 to 3.3.0
Bumps [rexml](https://github.com/ruby/rexml) from 3.2.5 to 3.3.0.
- [Release notes](https://github.com/ruby/rexml/releases)
- [Changelog](https://github.com/ruby/rexml/blob/master/NEWS.md)
- [Commits](https://github.com/ruby/rexml/compare/v3.2.5...v3.3.0)

---
updated-dependencies:
- dependency-name: rexml
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-06-21 07:31:42 +00:00
2e774ef185 Merge pull request #1 from onyx-and-iris/dependabot/bundler/rake-13.0.6
Update rake requirement from ~> 11.2, >= 11.2.2 to >= 11.2.2, ~> 13.0
2023-09-03 16:41:03 +01:00
dependabot[bot]
3e661b23a3 Update rake requirement from ~> 11.2, >= 11.2.2 to >= 11.2.2, ~> 13.0
Updates the requirements on [rake](https://github.com/ruby/rake) to permit the latest version.
- [Release notes](https://github.com/ruby/rake/releases)
- [Changelog](https://github.com/ruby/rake/blob/master/History.rdoc)
- [Commits](https://github.com/ruby/rake/compare/v11.3.0...v13.0.6)

---
updated-dependencies:
- dependency-name: rake
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-09-03 15:40:27 +00:00
fac83a0dde upd Gemfile.lock 2023-08-30 22:35:02 +01:00
4c4746fe8a now using String refinement
Events Director module renamed

patch bump
2023-08-29 22:55:11 +01:00
3a0f298045 upd gemfile 2023-08-29 20:36:51 +01:00
a4b70b6a98 patch bump 2023-08-29 15:46:00 +01:00
7c20063866 add error class docstrings 2023-08-29 15:45:42 +01:00
7cf05fbe08 use Kernel#Array 2023-08-29 15:45:23 +01:00
587e861bbe upd Gemfile.lock 2023-08-18 21:51:22 +01:00
3f255d6b45 upd attribute ssection in readme 2023-08-17 23:31:26 +01:00
9bd2b53b3d udpate attributes section in README 2023-08-17 23:28:37 +01:00
61c9a7b365 reword in readme.
remove brackets
2023-08-17 23:26:34 +01:00
daa8c6ada1 adds Events::Client.on method
allows registering blocks to be called back later

examples, readme updated

minor bump
2023-08-17 23:09:32 +01:00
10 changed files with 93 additions and 113 deletions

View File

@@ -1,7 +1,7 @@
PATH
remote: .
specs:
obsws (0.5.8)
obsws (0.6.2)
waitutil (~> 0.2.1)
websocket-driver (~> 0.7.5)
@@ -19,9 +19,9 @@ GEM
racc
racc (1.7.1)
rainbow (3.1.1)
rake (11.3.0)
rake (13.0.6)
regexp_parser (2.8.1)
rexml (3.2.5)
rexml (3.4.2)
rubocop (1.52.1)
json (~> 2.3)
parallel (~> 1.10)
@@ -58,11 +58,12 @@ GEM
PLATFORMS
x64-mingw-ucrt
x86_64-linux
DEPENDENCIES
minitest (~> 5.16, >= 5.16.3)
obsws!
rake (~> 11.2, >= 11.2.2)
rake (~> 13.0, >= 11.2.2)
standard (~> 1.30)
BUNDLED WITH

View File

@@ -67,7 +67,9 @@ For a full list of requests refer to [Requests](https://github.com/obsproject/ob
### Events
Register `on_` callback methods. Method names should match the api event but snake cased.
Register blocks with the Event client using the `on` method. Event tokens should match the event name but snake cased.
The event data will be passed to the block.
example:
@@ -75,23 +77,14 @@ example:
class Observer
def initialize
@e_client = OBSWS::Events::Client.new(host: "localhost", port: 4455, password: "strongpassword")
# register callback methods with the Event client
@e_client.register(
[
method(:on_current_program_scene_changed),
method(:on_input_mute_state_changed)
]
)
# register blocks on event types.
@e_client.on :current_program_scene_changed do |data|
...
end
@e_client.on :input_mute_state_changed do |data|
...
end
end
# define "on_" event methods.
def on_current_program_scene_changed(data)
...
end
def on_input_mute_state_changed(data)
...
end
...
end
```
@@ -104,11 +97,12 @@ For both request responses and event data you may inspect the available attribut
example:
```ruby
resp = cl.get_version
resp = @r_client.get_version
p resp.attrs
def on_scene_created(data):
p data.attrs
@e_client.on :input_mute_state_changed do |data|
p data.attrs
end
```
### Errors

View File

@@ -1,19 +1,28 @@
require_relative "../../lib/obsws"
require "yaml"
class Main
def initialize(**kwargs)
@r_client = OBSWS::Requests::Client.new(**kwargs)
@e_client = OBSWS::Events::Client.new(**kwargs)
@e_client.add_observer(self)
@e_client.on :current_program_scene_changed do |data|
puts "Switched to scene #{data.scene_name}"
end
@e_client.on :scene_created do |data|
puts "scene #{data.scene_name} has been created"
end
@e_client.on :input_mute_state_changed do |data|
puts "#{data.input_name} mute toggled"
end
@e_client.on :exit_started do
puts "OBS closing!"
@r_client.close
@e_client.close
@running = false
end
puts infostring
@running = true
end
def run
sleep(0.1) while @running
end
def infostring
@@ -24,23 +33,9 @@ class Main
].join(" ")
end
def on_current_program_scene_changed(data)
puts "Switched to scene #{data.scene_name}"
end
def on_scene_created(data)
puts "scene #{data.scene_name} has been created"
end
def on_input_mute_state_changed(data)
puts "#{data.input_name} mute toggled"
end
def on_exit_started
puts "OBS closing!"
@r_client.close
@e_client.close
@running = false
def run
@running = true
sleep(0.1) while @running
end
end

View File

@@ -13,36 +13,29 @@ class Main
def initialize(**kwargs)
subs = OBSWS::Events::SUBS::LOW_VOLUME | OBSWS::Events::SUBS::INPUTVOLUMEMETERS
@e_client = OBSWS::Events::Client.new(subs:, **kwargs)
@e_client.register(
[
method(:on_input_mute_state_changed),
method(:on_input_volume_meters)
]
)
@e_client.on :input_mute_state_changed do |data|
if data.input_name == DEVICE
puts "#{DEVICE} mute toggled"
end
end
@e_client.on :input_volume_meters do |data|
fget = ->(x) { (x > 0) ? (20 * Math.log(x, 10)).round(1) : -200.0 }
data.inputs.each do |d|
name = d[:inputName]
if name == DEVICE && !d[:inputLevelsMul].empty?
left, right = d[:inputLevelsMul]
puts "#{name} [L: #{fget.call(left[LevelTypes::POSTFADER])}, R: #{fget.call(right[LevelTypes::POSTFADER])}]"
end
end
end
end
def run
puts "press <Enter> to quit"
loop { break if gets.chomp.empty? }
end
def on_input_mute_state_changed(data)
if data.input_name == DEVICE
puts "#{DEVICE} mute toggled"
end
end
def on_input_volume_meters(data)
fget = ->(x) { (x > 0) ? (20 * Math.log(x, 10)).round(1) : -200.0 }
data.inputs.each do |d|
name = d[:inputName]
if name == DEVICE && !d[:inputLevelsMul].empty?
left, right = d[:inputLevelsMul]
puts "#{name} [L: #{fget.call(left[LevelTypes::POSTFADER])}, R: #{fget.call(right[LevelTypes::POSTFADER])}]"
end
end
end
end
def conn_from_yaml

View File

@@ -18,10 +18,13 @@ require_relative "obsws/event"
require_relative "obsws/version"
module OBSWS
# Base OBSWS error class
class OBSWSError < StandardError; end
# Raised when a connection fails or times out
class OBSWSConnectionError < OBSWSError; end
# Raised when a request returns an error code
class OBSWSRequestError < OBSWSError
attr_reader :req_name, :code

View File

@@ -28,43 +28,35 @@ module OBSWS
ALL = LOW_VOLUME | HIGH_VOLUME
end
module Callbacks
include Util::String
module Director
using Util::CoreExtensions
def observers
@observers ||= []
@observers ||= {}
end
def add_observer(observer)
observer = [observer] unless observer.respond_to? :each
observer.each { |o| observers << o unless observers.include? o }
def on(event, method = nil, &block)
(observers[event] ||= []) << (block || method)
end
def remove_observer(observer)
observer = [observer] unless observer.respond_to? :each
observers.reject! { |o| observer.include? o }
def register(cbs)
cbs = Array(cbs) unless cbs.respond_to? :each
cbs.each { |cb| on(cb.name[3..].to_sym, cb) }
end
private def notify_observers(event, data)
observers.each do |o|
if o.is_a? Method
if o.name.to_s == "on_#{snakecase(event)}"
data.empty? ? o.call : o.call(data)
end
elsif o.respond_to? "on_#{snakecase(event)}"
data.empty? ? o.send("on_#{snakecase(event)}") : o.send("on_#{snakecase(event)}", data)
end
end
def deregister(cbs)
cbs = Array(cbs) unless cbs.respond_to? :each
cbs.each { |cb| observers[cb.name[3..].to_sym]&.reject! { |o| cbs.include? o } }
end
alias_method :callbacks, :observers
alias_method :register, :add_observer
alias_method :deregister, :remove_observer
def fire(event, data)
observers[event.snakecase.to_sym]&.each { |block| data.empty? ? block.call : block.call(data) }
end
end
class Client
include Logging
include Callbacks
include Events::Director
include Mixin::TearDown
include Mixin::OPCodes
@@ -87,7 +79,7 @@ module OBSWS
logger.debug("received: #{data}")
event = data[:eventType]
data = data.fetch(:eventData, {})
notify_observers(event, Mixin::Data.new(data, data.keys))
fire(event, Mixin::Data.new(data, data.keys))
end
}
end

View File

@@ -1,16 +1,17 @@
module OBSWS
module Mixin
module Meta
include Util::String
using Util::CoreExtensions
def make_field_methods(*params)
params.each do |param|
define_singleton_method(snakecase(param.to_s)) { @resp[param] }
define_singleton_method(param.to_s.snakecase) { @resp[param] }
end
end
end
class MetaObject
using Util::CoreExtensions
include Mixin::Meta
def initialize(resp, fields)
@@ -21,14 +22,14 @@ module OBSWS
def empty? = @fields.empty?
def attrs = @fields.map { |f| snakecase(f.to_s) }
def attrs = @fields.map { |f| f.to_s.snakecase }
end
class Response < MetaObject; end
# Represents a request response object
class Response < MetaObject; end
class Data < MetaObject; end
# Represents an event data object
class Data < MetaObject; end
module TearDown
def stop_driver

View File

@@ -1,15 +1,16 @@
module OBSWS
module Util
module String
def camelcase(s)
s.split("_").map(&:capitalize).join
end
module CoreExtensions
refine String do
def camelcase
split("_").map(&:capitalize).join
end
def snakecase(s)
s
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
.downcase
def snakecase
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
.downcase
end
end
end
end

View File

@@ -7,11 +7,11 @@ module OBSWS
end
def minor
5
6
end
def patch
8
2
end
def to_a

View File

@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "waitutil", "~> 0.2.1"
spec.add_development_dependency "standard", "~> 1.30"
spec.add_development_dependency "minitest", "~> 5.16", ">= 5.16.3"
spec.add_development_dependency "rake", "~> 11.2", ">= 11.2.2"
spec.add_development_dependency "rake", ">= 11.2.2", "~> 13.0"
spec.required_ruby_version = ">= 3.0"
spec.metadata = {
"source_code_uri" => "https://github.com/onyx-and-iris/obsws-ruby"