obsws 0.5.8 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e289e262d4b31c03c7df28ba6d638c39d04e362fb0173f2535e0bf60f188bd41
4
- data.tar.gz: '0925f7d4d8695401d27dfb0d01df2c4fe5751d82d04c9bec752c981c52e1be8e'
3
+ metadata.gz: a2b42a23b71da1f893d08f8f1ffba2b178dd48cefa35627afd0bcdbf839fd6c7
4
+ data.tar.gz: 1654fafec480037f48b39eed30fa36e862b1cafe56d5c5af9e6fcaaa76a1e337
5
5
  SHA512:
6
- metadata.gz: 79e9065b4190dd974195079e31dc191fb7412fa143297b1f6dacedb1aae2100ded74abac9c1553b7fc92dce2987f40615d1101dfe5eab987f982a3bd3a7d87dd
7
- data.tar.gz: 5137c5ca260beaedcda8936532bd4d04b6154d2c4b7e4a005e5eb12ab070e5502c7231acf2539a90304170bb75ebdd2fdf96a5a7f9bf49a4eafbb6f3370a945f
6
+ metadata.gz: e54a03b4df1b1e77ab27fe25980ac759ff38c204885e1fdd6d4f07747b1485a8d00bbf77a75a866e2e8fa3d664d44c53cff10e5b9276772894131afbb0c53f8d
7
+ data.tar.gz: 0a9c417863aed8279eca8c838db1ddd635b4bc7eaba4fa2bb49e1141a313f9fa20cfeb281e5de79a21393b368e0f660e6beef526bf57505a01449024202abf61
data/README.md CHANGED
@@ -67,7 +67,9 @@ For a full list of requests refer to [Requests](https://github.com/obsproject/ob
67
67
 
68
68
  ### Events
69
69
 
70
- Register `on_` callback methods. Method names should match the api event but snake cased.
70
+ Register blocks with the Event client using the `on` method. Event tokens should match the event name but snake cased.
71
+
72
+ The event data will be passed to the block.
71
73
 
72
74
  example:
73
75
 
@@ -75,23 +77,14 @@ example:
75
77
  class Observer
76
78
  def initialize
77
79
  @e_client = OBSWS::Events::Client.new(host: "localhost", port: 4455, password: "strongpassword")
78
- # register callback methods with the Event client
79
- @e_client.register(
80
- [
81
- method(:on_current_program_scene_changed),
82
- method(:on_input_mute_state_changed)
83
- ]
84
- )
85
- end
86
-
87
- # define "on_" event methods.
88
- def on_current_program_scene_changed(data)
89
- ...
80
+ # register blocks on event types.
81
+ @e_client.on :current_program_scene_changed do |data|
82
+ ...
83
+ end
84
+ @e_client.on :input_mute_state_changed do |data|
85
+ ...
86
+ end
90
87
  end
91
- def on_input_mute_state_changed(data)
92
- ...
93
- end
94
- ...
95
88
  end
96
89
  ```
97
90
 
@@ -104,11 +97,12 @@ For both request responses and event data you may inspect the available attribut
104
97
  example:
105
98
 
106
99
  ```ruby
107
- resp = cl.get_version
100
+ resp = @r_client.get_version
108
101
  p resp.attrs
109
102
 
110
- def on_scene_created(data):
111
- p data.attrs
103
+ @e_client.on :input_mute_state_changed do |data|
104
+ p data.attrs
105
+ end
112
106
  ```
113
107
 
114
108
  ### Errors
data/lib/obsws/base.rb CHANGED
@@ -29,8 +29,8 @@ module OBSWS
29
29
  port = kwargs[:port] || 4455
30
30
  @password = kwargs[:password] || ""
31
31
  @subs = kwargs[:subs] || 0
32
- setup_driver(host, port) and start_driver
33
32
  @identified = Identified.new
33
+ setup_driver(host, port) and start_driver
34
34
  WaitUtil.wait_for_condition(
35
35
  "successful identification",
36
36
  delay_sec: 0.01,
data/lib/obsws/event.rb CHANGED
@@ -28,43 +28,35 @@ module OBSWS
28
28
  ALL = LOW_VOLUME | HIGH_VOLUME
29
29
  end
30
30
 
31
- module Callbacks
31
+ module EventDirector
32
32
  include Util::String
33
33
 
34
34
  def observers
35
- @observers ||= []
35
+ @observers ||= {}
36
36
  end
37
37
 
38
- def add_observer(observer)
39
- observer = [observer] unless observer.respond_to? :each
40
- observer.each { |o| observers << o unless observers.include? o }
38
+ def on(event, method = nil, &block)
39
+ (observers[event] ||= []) << (block || method)
41
40
  end
42
41
 
43
- def remove_observer(observer)
44
- observer = [observer] unless observer.respond_to? :each
45
- observers.reject! { |o| observer.include? o }
42
+ def register(cbs)
43
+ cbs = Array(cbs) unless cbs.respond_to? :each
44
+ cbs.each { |cb| on(cb.name[3..].to_sym, cb) }
46
45
  end
47
46
 
48
- private def notify_observers(event, data)
49
- observers.each do |o|
50
- if o.is_a? Method
51
- if o.name.to_s == "on_#{snakecase(event)}"
52
- data.empty? ? o.call : o.call(data)
53
- end
54
- elsif o.respond_to? "on_#{snakecase(event)}"
55
- data.empty? ? o.send("on_#{snakecase(event)}") : o.send("on_#{snakecase(event)}", data)
56
- end
57
- end
47
+ def deregister(cbs)
48
+ cbs = Array(cbs) unless cbs.respond_to? :each
49
+ cbs.each { |cb| observers[cb.name[3..].to_sym]&.reject! { |o| cbs.include? o } }
58
50
  end
59
51
 
60
- alias_method :callbacks, :observers
61
- alias_method :register, :add_observer
62
- alias_method :deregister, :remove_observer
52
+ def fire(event, data)
53
+ observers[snakecase(event).to_sym]&.each { |block| data.empty? ? block.call : block.call(data) }
54
+ end
63
55
  end
64
56
 
65
57
  class Client
66
58
  include Logging
67
- include Callbacks
59
+ include EventDirector
68
60
  include Mixin::TearDown
69
61
  include Mixin::OPCodes
70
62
 
@@ -87,7 +79,7 @@ module OBSWS
87
79
  logger.debug("received: #{data}")
88
80
  event = data[:eventType]
89
81
  data = data.fetch(:eventData, {})
90
- notify_observers(event, Mixin::Data.new(data, data.keys))
82
+ fire(event, Mixin::Data.new(data, data.keys))
91
83
  end
92
84
  }
93
85
  end
data/lib/obsws/mixin.rb CHANGED
@@ -24,11 +24,11 @@ module OBSWS
24
24
  def attrs = @fields.map { |f| snakecase(f.to_s) }
25
25
  end
26
26
 
27
- class Response < MetaObject; end
28
27
  # Represents a request response object
28
+ class Response < MetaObject; end
29
29
 
30
- class Data < MetaObject; end
31
30
  # Represents an event data object
31
+ class Data < MetaObject; end
32
32
 
33
33
  module TearDown
34
34
  def stop_driver
data/lib/obsws/version.rb CHANGED
@@ -7,11 +7,11 @@ module OBSWS
7
7
  end
8
8
 
9
9
  def minor
10
- 5
10
+ 6
11
11
  end
12
12
 
13
13
  def patch
14
- 8
14
+ 1
15
15
  end
16
16
 
17
17
  def to_a
data/lib/obsws.rb CHANGED
@@ -18,26 +18,22 @@ require_relative "obsws/event"
18
18
  require_relative "obsws/version"
19
19
 
20
20
  module OBSWS
21
+ # Base OBSWS error class
21
22
  class OBSWSError < StandardError; end
22
23
 
24
+ # Raised when a connection fails or times out
23
25
  class OBSWSConnectionError < OBSWSError; end
24
26
 
27
+ # Raised when a request returns an error code
25
28
  class OBSWSRequestError < OBSWSError
26
29
  attr_reader :req_name, :code
27
30
 
28
- def initialize(req_name, code, msg)
31
+ def initialize(req_name, code, comment)
29
32
  @req_name = req_name
30
33
  @code = code
31
- @msg = msg
34
+ message = "Request #{@req_name} returned code #{@code}."
35
+ message << " With message: #{comment}" if comment
32
36
  super(message)
33
37
  end
34
-
35
- def message
36
- msg = [
37
- "Request #{@req_name} returned code #{@code}."
38
- ]
39
- msg << "With message: #{@msg}" if @msg
40
- msg.join(" ")
41
- end
42
38
  end
43
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: obsws
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.8
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - onyx_online
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-11 00:00:00.000000000 Z
11
+ date: 2023-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: websocket-driver