obsws 0.5.8 → 0.6.2

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: 112d9ab40ebc9219946426e476beaf6c489803049382d16feca972e7e14428d1
4
+ data.tar.gz: 561994b90c57837f4f5a496b22ba017fcb0fd4e4dfd946b7e62cdb91441f7253
5
5
  SHA512:
6
- metadata.gz: 79e9065b4190dd974195079e31dc191fb7412fa143297b1f6dacedb1aae2100ded74abac9c1553b7fc92dce2987f40615d1101dfe5eab987f982a3bd3a7d87dd
7
- data.tar.gz: 5137c5ca260beaedcda8936532bd4d04b6154d2c4b7e4a005e5eb12ab070e5502c7231acf2539a90304170bb75ebdd2fdf96a5a7f9bf49a4eafbb6f3370a945f
6
+ metadata.gz: da20d79e6e27d739ad0df64505e5ca0ed5e770505589f9fe89f05f8b326fec6df3a7aaaf290cf2d841cd5145475f219feadeb79c8e0f633f76f66b2336361777
7
+ data.tar.gz: '09d3781a1e45dd3631a5e0b300fb998cd03798e53bc51e1888850603ab55e1d8f6d3850315452e20178eebe60e11fb0f7d4a2a250cd0cc6eff4709d22eed623e'
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
32
- include Util::String
31
+ module Director
32
+ using Util::CoreExtensions
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[event.snakecase.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 Events::Director
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
@@ -1,16 +1,17 @@
1
1
  module OBSWS
2
2
  module Mixin
3
3
  module Meta
4
- include Util::String
4
+ using Util::CoreExtensions
5
5
 
6
6
  def make_field_methods(*params)
7
7
  params.each do |param|
8
- define_singleton_method(snakecase(param.to_s)) { @resp[param] }
8
+ define_singleton_method(param.to_s.snakecase) { @resp[param] }
9
9
  end
10
10
  end
11
11
  end
12
12
 
13
13
  class MetaObject
14
+ using Util::CoreExtensions
14
15
  include Mixin::Meta
15
16
 
16
17
  def initialize(resp, fields)
@@ -21,14 +22,14 @@ module OBSWS
21
22
 
22
23
  def empty? = @fields.empty?
23
24
 
24
- def attrs = @fields.map { |f| snakecase(f.to_s) }
25
+ def attrs = @fields.map { |f| f.to_s.snakecase }
25
26
  end
26
27
 
27
- class Response < MetaObject; end
28
28
  # Represents a request response object
29
+ class Response < MetaObject; end
29
30
 
30
- class Data < MetaObject; end
31
31
  # Represents an event data object
32
+ class Data < MetaObject; end
32
33
 
33
34
  module TearDown
34
35
  def stop_driver
data/lib/obsws/util.rb CHANGED
@@ -1,15 +1,16 @@
1
1
  module OBSWS
2
2
  module Util
3
- module String
4
- def camelcase(s)
5
- s.split("_").map(&:capitalize).join
6
- end
3
+ module CoreExtensions
4
+ refine String do
5
+ def camelcase
6
+ split("_").map(&:capitalize).join
7
+ end
7
8
 
8
- def snakecase(s)
9
- s
10
- .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
11
- .gsub(/([a-z\d])([A-Z])/, '\1_\2')
12
- .downcase
9
+ def snakecase
10
+ gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
11
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
12
+ .downcase
13
+ end
13
14
  end
14
15
  end
15
16
  end
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
+ 2
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.2
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