obsws 0.4.0 → 0.5.0

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: c275787361111b1b84f2f5023d5d24e45b174540a8e687aaaae2fa647b89eecc
4
- data.tar.gz: 738122bc2be93730ef117240c467ec261ab6918c406dea273eefc134d5f74362
3
+ metadata.gz: c7826cef2f98842479438cf09ac7dec77e7c8e24a8adca3dcf55fce92676c2f4
4
+ data.tar.gz: 0c5dc416a213a344488cf2059d789aa07536d83440b14fd9c2d41918821e5a9c
5
5
  SHA512:
6
- metadata.gz: 121d5bf831969d5bd92244efb4c6ae94fdea7e6a47b16c9b7bbee80626e6739664433bb04e3225aef2eaa759127ee074841e05b430331c3bd987762c8623f15c
7
- data.tar.gz: e67e598037d689469263721e2408eacebd6469e32a5b55e50f43876efe33d5b3b581e99ec2d804ed28549079f6b4855c76e8508e65e46d504bca12e75e32fff9
6
+ metadata.gz: 178671ff1a8f5577694fe4d7f02e0c96f6d37ef50ec36ba6ee802f6999ad5fc470695ade79caf2393b32eb9d0fc325236131490f5348a020d9fb73eb6aaa219d
7
+ data.tar.gz: bb0fe9a35b56699bd64b0f0d55d1f203ceaa7fe1abfafcc5d3ca23cb3207275662f223a1d4a124e1a89f52c724d688fdfaf5f548bec5586155ee732d99bebb08
data/README.md CHANGED
@@ -109,7 +109,13 @@ def on_scene_created(data):
109
109
 
110
110
  ### Errors
111
111
 
112
- If a request fails an `OBSWSError` will be raised with a status code.
112
+ If a general error occurs an `OBSWSError` will be raised.
113
+
114
+ If a connection attempt fails or times out an `OBSWSConnectionError` will be raised.
115
+
116
+ If a request fails an `OBSWSRequestError` will be raised with a status code.
117
+
118
+ - The request name and code are retrievable through attributes {OBSWSRequestError}.name and {OBSWSRequestError}.code
113
119
 
114
120
  For a full list of status codes refer to [Codes](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requeststatus)
115
121
 
data/lib/obsws/base.rb CHANGED
@@ -1,12 +1,3 @@
1
- require "digest/sha2"
2
- require "json"
3
- require "waitutil"
4
-
5
- require_relative "driver"
6
- require_relative "error"
7
- require_relative "logger"
8
- require_relative "mixin"
9
-
10
1
  module OBSWS
11
2
  class Base
12
3
  include Logging
@@ -21,8 +12,7 @@ module OBSWS
21
12
  port = kwargs[:port] || 4455
22
13
  @password = kwargs[:password] || ""
23
14
  @subs = kwargs[:subs] || 0
24
- setup_driver(host, port)
25
- start_driver
15
+ setup_driver(host, port) and start_driver
26
16
  WaitUtil.wait_for_condition(
27
17
  "successful identification",
28
18
  delay_sec: 0.01,
data/lib/obsws/driver.rb CHANGED
@@ -1,6 +1,3 @@
1
- require "socket"
2
- require "websocket/driver"
3
-
4
1
  module OBSWS
5
2
  module Driver
6
3
  class Socket
data/lib/obsws/event.rb CHANGED
@@ -1,9 +1,3 @@
1
- require "json"
2
-
3
- require_relative "util"
4
- require_relative "mixin"
5
- require_relative "logger"
6
-
7
1
  module OBSWS
8
2
  module Events
9
3
  module SUBS
@@ -42,15 +36,16 @@ module OBSWS
42
36
  end
43
37
 
44
38
  def add_observer(observer)
45
- observer = [observer] if !observer.respond_to? :each
39
+ observer = [observer] unless observer.respond_to? :each
46
40
  observer.each { |o| observers << o unless observers.include? o }
47
41
  end
48
42
 
49
43
  def remove_observer(observer)
50
- observers.delete(observer)
44
+ observer = [observer] unless observer.respond_to? :each
45
+ observers.reject! { |o| observer.include? o }
51
46
  end
52
47
 
53
- def notify_observers(event, data)
48
+ private def notify_observers(event, data)
54
49
  observers.each do |o|
55
50
  if o.is_a? Method
56
51
  if o.name.to_s == "on_#{snakecase(event)}"
@@ -62,6 +57,7 @@ module OBSWS
62
57
  end
63
58
  end
64
59
 
60
+ alias_method :callbacks, :observers
65
61
  alias_method :register, :add_observer
66
62
  alias_method :deregister, :remove_observer
67
63
  end
@@ -76,6 +72,11 @@ module OBSWS
76
72
  kwargs[:subs] ||= SUBS::LOW_VOLUME
77
73
  @base_client = Base.new(**kwargs)
78
74
  logger.info("#{self} successfully identified with server")
75
+ rescue Errno::ECONNREFUSED, WaitUtil::TimeoutError => e
76
+ msg = "#{e.class.name}: #{e.message}"
77
+ logger.error(msg)
78
+ raise OBSWSConnectionError.new(msg)
79
+ else
79
80
  @base_client.updater = ->(op_code, data) {
80
81
  if op_code == Mixin::OPCodes::EVENT
81
82
  logger.debug("received: #{data}")
data/lib/obsws/logger.rb CHANGED
@@ -1,9 +1,7 @@
1
- require "logger"
2
-
3
1
  module OBSWS
4
2
  module Logging
5
3
  def logger
6
- @logger = Logger.new($stdout, level: ENV.fetch("OBSWS_LOG_LEVEL", "WARN"))
4
+ @logger ||= Logger.new($stdout, level: ENV.fetch("OBSWS_LOG_LEVEL", "WARN"))
7
5
  @logger.progname = instance_of?(::Module) ? name : self.class.name
8
6
  @logger
9
7
  end
data/lib/obsws/mixin.rb CHANGED
@@ -1,5 +1,3 @@
1
- require_relative "util"
2
-
3
1
  module OBSWS
4
2
  module Mixin
5
3
  module Meta
@@ -26,11 +24,11 @@ module OBSWS
26
24
  def attrs = @fields.map { |f| snakecase(f.to_s) }
27
25
  end
28
26
 
29
- class Response < MetaObject
30
- end
27
+ class Response < MetaObject; end
28
+ # Represents a request response object
31
29
 
32
- class Data < MetaObject
33
- end
30
+ class Data < MetaObject; end
31
+ # Represents an event data object
34
32
 
35
33
  module TearDown
36
34
  def stop_driver
data/lib/obsws/req.rb CHANGED
@@ -1,22 +1,18 @@
1
- require "waitutil"
2
-
3
- require_relative "base"
4
- require_relative "error"
5
- require_relative "util"
6
- require_relative "mixin"
7
- require_relative "logger"
8
-
9
1
  module OBSWS
10
2
  module Requests
11
3
  class Client
12
4
  include Logging
13
- include Error
14
5
  include Mixin::TearDown
15
6
  include Mixin::OPCodes
16
7
 
17
8
  def initialize(**kwargs)
18
9
  @base_client = Base.new(**kwargs)
19
10
  logger.info("#{self} successfully identified with server")
11
+ rescue Errno::ECONNREFUSED, WaitUtil::TimeoutError => e
12
+ msg = "#{e.class.name}: #{e.message}"
13
+ logger.error(msg)
14
+ raise OBSWSConnectionError.new(msg)
15
+ else
20
16
  @base_client.updater = ->(op_code, data) {
21
17
  logger.debug("response received: #{data}")
22
18
  @response = data if op_code == Mixin::OPCodes::REQUESTRESPONSE
@@ -47,20 +43,24 @@ module OBSWS
47
43
  delay_sec: 0.001,
48
44
  timeout_sec: 3
49
45
  ) { @response[:requestId] == id }
50
- if !@response[:requestStatus][:result]
51
- error = [
52
- "Request #{@response[:requestType]} returned code #{@response[:requestStatus][:code]}"
53
- ]
54
- if @response[:requestStatus].key?(:comment)
55
- error += ["With message: #{@response[:requestStatus][:comment]}"]
56
- end
57
- raise OBSWSError.new(error.join("\n"))
46
+ unless @response[:requestStatus][:result]
47
+ raise OBSWSRequestError.new(@response[:requestType], @response[:requestStatus][:code], @response[:requestStatus][:comment])
58
48
  end
59
49
  @response[:responseData]
60
- rescue WaitUtil::TimeoutError
61
- msg = "no response with matching id received"
62
- logger.error(msg)
63
- raise OBSWSError.new(msg)
50
+ rescue OBSWSRequestError => e
51
+ err_msg = [
52
+ "#{e.class.name}: #{e.message}",
53
+ *e.backtrace
54
+ ]
55
+ logger.error(err_msg.join("\n"))
56
+ raise
57
+ rescue WaitUtil::TimeoutError => e
58
+ err_msg = [
59
+ "#{e.class.name}: #{e.message}",
60
+ *e.backtrace
61
+ ]
62
+ logger.error(err_msg)
63
+ raise OBSWSError.new(err_msg)
64
64
  end
65
65
 
66
66
  def get_version
data/lib/obsws/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module OBSWS
2
- module Version
2
+ module VERSION
3
3
  module_function
4
4
 
5
5
  def major
@@ -7,7 +7,7 @@ module OBSWS
7
7
  end
8
8
 
9
9
  def minor
10
- 4
10
+ 5
11
11
  end
12
12
 
13
13
  def patch
@@ -22,6 +22,4 @@ module OBSWS
22
22
  to_a.join(".")
23
23
  end
24
24
  end
25
-
26
- VERSION = Version.to_s
27
25
  end
data/lib/obsws.rb CHANGED
@@ -1,5 +1,42 @@
1
+ require "digest/sha2"
2
+ require "json"
3
+ require "waitutil"
4
+ require "socket"
5
+ require "websocket/driver"
6
+ require "logger"
7
+
8
+ require_relative "obsws/logger"
9
+ require_relative "obsws/driver"
10
+ require_relative "obsws/util"
11
+ require_relative "obsws/mixin"
12
+ require_relative "obsws/base"
13
+
1
14
  require_relative "obsws/req"
2
15
  require_relative "obsws/event"
3
16
 
17
+ require_relative "obsws/version"
18
+
4
19
  module OBSWS
20
+ class OBSWSError < StandardError; end
21
+
22
+ class OBSWSConnectionError < OBSWSError; end
23
+
24
+ class OBSWSRequestError < OBSWSError
25
+ attr_reader :name, :code
26
+
27
+ def initialize(name, code, msg)
28
+ @name = name
29
+ @code = code
30
+ @msg = msg
31
+ super(message)
32
+ end
33
+
34
+ def message
35
+ msg = [
36
+ "Request #{@name} returned code #{@code}."
37
+ ]
38
+ msg << "With message: #{@msg}" if @msg
39
+ msg.join(" ")
40
+ end
41
+ end
5
42
  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.4.0
4
+ version: 0.5.0
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-07-27 00:00:00.000000000 Z
11
+ date: 2023-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: websocket-driver
@@ -107,7 +107,6 @@ files:
107
107
  - lib/obsws.rb
108
108
  - lib/obsws/base.rb
109
109
  - lib/obsws/driver.rb
110
- - lib/obsws/error.rb
111
110
  - lib/obsws/event.rb
112
111
  - lib/obsws/logger.rb
113
112
  - lib/obsws/mixin.rb
data/lib/obsws/error.rb DELETED
@@ -1,6 +0,0 @@
1
- module OBSWS
2
- module Error
3
- class OBSWSError < StandardError
4
- end
5
- end
6
- end