obsws 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f23333577b91916057461d9084494c147f63cd3feb90e121ab33c5b795af1566
4
- data.tar.gz: e5b31955a09ace6b520351252034ca8b1bb8932698859b12a3af3dd2b70485d9
3
+ metadata.gz: f55dbfe2267a8b2dec6fc0c347b537b1a9727de641ec762d16128a40ee3397a4
4
+ data.tar.gz: 3ef38f6bfb12db426c31afdb130e25debda7173e135daf9d88677d6002365c55
5
5
  SHA512:
6
- metadata.gz: 14411743ad90540ec041ed46c30f1347512174a876a6891bf1351c7f4025c712cc9f57aad1911adabc1368fb1f907b982f519ad713404d516d88af42a0687120
7
- data.tar.gz: 28ac135781ba0610baa21912d9105b1ebd3762b682693d9866172a0ae7dde9e322de68174f7bf23da8fb6d8faf813128fc1a6c1e3d04557be533d4cded5ef59c
6
+ metadata.gz: c2914cfd19dd1107ecd0d8825ca5386b9e6574983527098801fed5f0ed3c06520490f139d9c3837d45aa2791cd8e20e263b07ada46d46d7957a399aebd629ad2
7
+ data.tar.gz: 1d76544ea453fc16bad1d38b64ff4328ad1665520323112694007377e75959ce78f05027cfb85ef176b74148374f15c60b03b627f92552e6741171919f930c37
data/README.md CHANGED
@@ -28,37 +28,39 @@ bundle install
28
28
 
29
29
  #### Example `main.rb`
30
30
 
31
- pass `host`, `port` and `password` as keyword arguments.
31
+ Pass `host`, `port` and `password` as keyword arguments.
32
32
 
33
33
  ```ruby
34
34
  require "obsws"
35
35
 
36
- def main
37
- OBSWS::Requests::Client
38
- .new(host: "localhost", port: 4455, password: "strongpassword")
39
- .run do |client|
40
- # Toggle the mute state of your Mic input
41
- client.toggle_input_mute("Mic/Aux")
42
- end
36
+ class Main
37
+ def run
38
+ OBSWS::Requests::Client
39
+ .new(host: "localhost", port: 4455, password: "strongpassword")
40
+ .run do |client|
41
+ # Toggle the mute state of your Mic input
42
+ client.toggle_input_mute("Mic/Aux")
43
+ end
44
+ end
43
45
  end
44
46
 
45
- main if $0 == __FILE__
47
+ Main.new.run if $PROGRAM_NAME == __FILE__
46
48
  ```
47
49
 
50
+ Passing OBSWS::Requests::Client.run a block closes the socket once the block returns.
51
+
48
52
  ### Requests
49
53
 
50
- Method names for requests match the API calls but snake cased. `run` accepts a block that closes the socket once you are done.
54
+ Method names for requests match the API calls but snake cased.
51
55
 
52
56
  example:
53
57
 
54
58
  ```ruby
55
- r_client.run do
56
- # GetVersion
57
- resp = r_client.get_version
59
+ # GetVersion
60
+ resp = r_client.get_version
58
61
 
59
- # SetCurrentProgramScene
60
- r_client.set_current_program_scene("BRB")
61
- end
62
+ # SetCurrentProgramScene
63
+ r_client.set_current_program_scene("BRB")
62
64
  ```
63
65
 
64
66
  For a full list of requests refer to [Requests](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requests)
@@ -112,15 +114,12 @@ For a full list of status codes refer to [Codes](https://github.com/obsproject/o
112
114
 
113
115
  ### Logging
114
116
 
115
- To see the raw messages set log level to debug
117
+ To enable logs set an environmental variable `OBSWS_LOG_LEVEL` to the appropriate level.
116
118
 
117
- example:
118
-
119
- ```ruby
120
- require "obsws"
119
+ example in powershell:
121
120
 
122
- OBSWS::LOGGER.debug!
123
- ...
121
+ ```powershell
122
+ $env:OBSWS_LOG_LEVEL="DEBUG"
124
123
  ```
125
124
 
126
125
  ### Tests
data/lib/obsws/base.rb CHANGED
@@ -2,11 +2,11 @@ require "socket"
2
2
  require "websocket/driver"
3
3
  require "digest/sha2"
4
4
  require "json"
5
- require "observer"
6
5
  require "waitutil"
7
6
 
8
7
  require_relative "mixin"
9
8
  require_relative "error"
9
+ require_relative "logger"
10
10
 
11
11
  module OBSWS
12
12
  class Socket
@@ -23,10 +23,11 @@ module OBSWS
23
23
  end
24
24
 
25
25
  class Base
26
- include Observable
26
+ include Logging
27
27
  include Mixin::OPCodes
28
28
 
29
- attr_reader :id, :driver, :closed
29
+ attr_reader :closed
30
+ attr_writer :updater
30
31
 
31
32
  def initialize(**kwargs)
32
33
  host = kwargs[:host] || "localhost"
@@ -38,14 +39,13 @@ module OBSWS
38
39
  @driver =
39
40
  WebSocket::Driver.client(Socket.new("ws://#{host}:#{port}", @socket))
40
41
  @driver.on :open do |msg|
41
- LOGGER.debug("driver socket open")
42
+ logger.debug("driver socket open")
42
43
  end
43
44
  @driver.on :close do |msg|
44
- LOGGER.debug("driver socket closed")
45
+ logger.debug("driver socket closed")
45
46
  @closed = true
46
47
  end
47
48
  @driver.on :message do |msg|
48
- LOGGER.debug("received: #{msg.data}")
49
49
  msg_handler(JSON.parse(msg.data, symbolize_names: true))
50
50
  end
51
51
  start_driver
@@ -56,7 +56,7 @@ module OBSWS
56
56
  ) { @identified }
57
57
  end
58
58
 
59
- def start_driver
59
+ private def start_driver
60
60
  Thread.new do
61
61
  @driver.start
62
62
 
@@ -68,6 +68,12 @@ module OBSWS
68
68
  end
69
69
  end
70
70
 
71
+ public def stop_driver
72
+ @driver.close
73
+ end
74
+
75
+ private
76
+
71
77
  def auth_token(salt:, challenge:)
72
78
  Digest::SHA256.base64digest(
73
79
  Digest::SHA256.base64digest(@password + salt) + challenge
@@ -86,7 +92,7 @@ module OBSWS
86
92
  if @password.empty?
87
93
  raise OBSWSError("auth enabled but no password provided")
88
94
  end
89
- LOGGER.info("initiating authentication")
95
+ logger.info("initiating authentication")
90
96
  payload[:d][:authentication] = auth_token(**auth)
91
97
  end
92
98
  @driver.text(JSON.generate(payload))
@@ -99,12 +105,11 @@ module OBSWS
99
105
  when Mixin::OPCodes::IDENTIFIED
100
106
  @identified = true
101
107
  when Mixin::OPCodes::EVENT, Mixin::OPCodes::REQUESTRESPONSE
102
- changed
103
- notify_observers(data[:op], data[:d])
108
+ @updater.call(data[:op], data[:d])
104
109
  end
105
110
  end
106
111
 
107
- def req(id, type_, data = nil)
112
+ public def req(id, type_, data = nil)
108
113
  payload = {
109
114
  op: Mixin::OPCodes::REQUEST,
110
115
  d: {
@@ -113,8 +118,8 @@ module OBSWS
113
118
  }
114
119
  }
115
120
  payload[:d][:requestData] = data if data
116
- LOGGER.debug("sending request: #{payload}")
117
- queued = @driver.text(JSON.generate(payload))
121
+ logger.debug("sending request: #{payload}")
122
+ @driver.text(JSON.generate(payload))
118
123
  end
119
124
  end
120
125
  end
data/lib/obsws/event.rb CHANGED
@@ -2,6 +2,7 @@ require "json"
2
2
 
3
3
  require_relative "util"
4
4
  require_relative "mixin"
5
+ require_relative "logger"
5
6
 
6
7
  module OBSWS
7
8
  module Events
@@ -62,6 +63,7 @@ module OBSWS
62
63
  end
63
64
 
64
65
  class Client
66
+ include Logging
65
67
  include Callbacks
66
68
  include Mixin::TearDown
67
69
  include Mixin::OPCodes
@@ -69,21 +71,20 @@ module OBSWS
69
71
  def initialize(**kwargs)
70
72
  kwargs[:subs] ||= SUBS::LOW_VOLUME
71
73
  @base_client = Base.new(**kwargs)
72
- LOGGER.info("#{self} succesfully identified with server")
73
- @base_client.add_observer(self)
74
+ logger.info("#{self} succesfully identified with server")
75
+ @base_client.updater = ->(op_code, data) {
76
+ if op_code == Mixin::OPCodes::EVENT
77
+ logger.debug("received: #{data}")
78
+ event = data[:eventType]
79
+ data = data.fetch(:eventData, {})
80
+ notify_observers(event, Mixin::Data.new(data, data.keys))
81
+ end
82
+ }
74
83
  end
75
84
 
76
85
  def to_s
77
86
  self.class.name.split("::").last(2).join("::")
78
87
  end
79
-
80
- def update(op_code, data)
81
- if op_code == Mixin::OPCodes::EVENT
82
- event = data[:eventType]
83
- data = data.key?(:eventData) ? data[:eventData] : {}
84
- notify_observers(event, Mixin::Data.new(data, data.keys))
85
- end
86
- end
87
88
  end
88
89
  end
89
90
  end
@@ -0,0 +1,11 @@
1
+ require "logger"
2
+
3
+ module OBSWS
4
+ module Logging
5
+ def logger
6
+ @logger = Logger.new($stdout, level: ENV.fetch("OBSWS_LOG_LEVEL", "WARN"))
7
+ @logger.progname = instance_of?(::Module) ? name : self.class.name
8
+ @logger
9
+ end
10
+ end
11
+ end
data/lib/obsws/mixin.rb CHANGED
@@ -18,7 +18,7 @@ module OBSWS
18
18
  def initialize(resp, fields)
19
19
  @resp = resp
20
20
  @fields = fields
21
- self.make_field_methods *fields
21
+ make_field_methods(*fields)
22
22
  end
23
23
 
24
24
  def empty? = @fields.empty?
@@ -33,9 +33,11 @@ module OBSWS
33
33
  end
34
34
 
35
35
  module TearDown
36
- def close
37
- @base_client.driver.close
36
+ def stop_driver
37
+ @base_client.stop_driver
38
38
  end
39
+
40
+ alias_method :close, :stop_driver
39
41
  end
40
42
 
41
43
  module OPCodes
data/lib/obsws/req.rb CHANGED
@@ -4,18 +4,23 @@ require_relative "base"
4
4
  require_relative "error"
5
5
  require_relative "util"
6
6
  require_relative "mixin"
7
+ require_relative "logger"
7
8
 
8
9
  module OBSWS
9
10
  module Requests
10
11
  class Client
12
+ include Logging
11
13
  include Error
12
14
  include Mixin::TearDown
13
15
  include Mixin::OPCodes
14
16
 
15
17
  def initialize(**kwargs)
16
18
  @base_client = Base.new(**kwargs)
17
- LOGGER.info("#{self} succesfully identified with server")
18
- @base_client.add_observer(self)
19
+ logger.info("#{self} succesfully identified with server")
20
+ @base_client.updater = ->(op_code, data) {
21
+ logger.debug("response received: #{data}")
22
+ @response = data if op_code == Mixin::OPCodes::REQUESTRESPONSE
23
+ }
19
24
  @response = {requestId: 0}
20
25
  end
21
26
 
@@ -26,7 +31,7 @@ module OBSWS
26
31
  def run
27
32
  yield(self)
28
33
  ensure
29
- close
34
+ stop_driver
30
35
  WaitUtil.wait_for_condition(
31
36
  "driver to close",
32
37
  delay_sec: 0.01,
@@ -34,10 +39,6 @@ module OBSWS
34
39
  ) { @base_client.closed }
35
40
  end
36
41
 
37
- def update(op_code, data)
38
- @response = data if op_code == Mixin::OPCodes::REQUESTRESPONSE
39
- end
40
-
41
42
  def call(req, data = nil)
42
43
  id = rand(1..1000)
43
44
  @base_client.req(id, req, data)
@@ -58,7 +59,7 @@ module OBSWS
58
59
  @response[:responseData]
59
60
  rescue WaitUtil::TimeoutError
60
61
  msg = "no response with matching id received"
61
- LOGGER.error(msg)
62
+ logger.error(msg)
62
63
  raise OBSWSError.new(msg)
63
64
  end
64
65
 
data/lib/obsws/util.rb CHANGED
@@ -2,12 +2,11 @@ module OBSWS
2
2
  module Util
3
3
  class ::String
4
4
  def to_camel
5
- self.split(/_/).map(&:capitalize).join
5
+ split("_").map(&:capitalize).join
6
6
  end
7
7
 
8
8
  def to_snake
9
- self
10
- .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
9
+ gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
11
10
  .gsub(/([a-z\d])([A-Z])/, '\1_\2')
12
11
  .downcase
13
12
  end
data/lib/obsws/version.rb CHANGED
@@ -7,7 +7,7 @@ module OBSWS
7
7
  end
8
8
 
9
9
  def minor
10
- 2
10
+ 3
11
11
  end
12
12
 
13
13
  def patch
data/lib/obsws.rb CHANGED
@@ -1,11 +1,5 @@
1
- require "logger"
2
-
3
1
  require_relative "obsws/req"
4
2
  require_relative "obsws/event"
5
3
 
6
4
  module OBSWS
7
- include Logger::Severity
8
-
9
- LOGGER = Logger.new(STDOUT)
10
- LOGGER.level = WARN
11
5
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: obsws
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.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-19 00:00:00.000000000 Z
11
+ date: 2023-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: observer
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 0.1.1
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: 0.1.1
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: websocket-driver
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +108,7 @@ files:
122
108
  - lib/obsws/base.rb
123
109
  - lib/obsws/error.rb
124
110
  - lib/obsws/event.rb
111
+ - lib/obsws/logger.rb
125
112
  - lib/obsws/mixin.rb
126
113
  - lib/obsws/req.rb
127
114
  - lib/obsws/util.rb