From 15c4baf5d72a1bfdd32de29ca72ca48200edbabc Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 21 Jul 2023 06:37:14 +0100 Subject: [PATCH] raekfile updated rework examples --- README.md | 34 ++++++++++++++++-------------- Rakefile | 2 +- examples/events/Gemfile | 7 ------ examples/events/Gemfile.lock | 25 ---------------------- examples/events/main.rb | 4 ++-- examples/levels/Gemfile | 7 ------ examples/levels/Gemfile.lock | 25 ---------------------- examples/levels/main.rb | 4 ++-- examples/scene_rotate/Gemfile | 7 ------ examples/scene_rotate/Gemfile.lock | 25 ---------------------- examples/scene_rotate/main.rb | 27 +++++++++++++----------- main.rb | 6 ++++-- 12 files changed, 42 insertions(+), 131 deletions(-) delete mode 100644 examples/events/Gemfile delete mode 100644 examples/events/Gemfile.lock delete mode 100644 examples/levels/Gemfile delete mode 100644 examples/levels/Gemfile.lock delete mode 100644 examples/scene_rotate/Gemfile delete mode 100644 examples/scene_rotate/Gemfile.lock diff --git a/README.md b/README.md index 51a0cc8..53257c2 100644 --- a/README.md +++ b/README.md @@ -28,37 +28,39 @@ bundle install #### Example `main.rb` -pass `host`, `port` and `password` as keyword arguments. +Pass `host`, `port` and `password` as keyword arguments. ```ruby require "obsws" -def main - OBSWS::Requests::Client - .new(host: "localhost", port: 4455, password: "strongpassword") - .run do |client| - # Toggle the mute state of your Mic input - client.toggle_input_mute("Mic/Aux") - end +class Main + def run + OBSWS::Requests::Client + .new(host: "localhost", port: 4455, password: "strongpassword") + .run do |client| + # Toggle the mute state of your Mic input + client.toggle_input_mute("Mic/Aux") + end + end end -main if $0 == __FILE__ +Main.new.run if $PROGRAM_NAME == __FILE__ ``` +Passing OBSWS::Requests::Client.run a block closes the socket once the block returns. + ### Requests -Method names for requests match the API calls but snake cased. `run` accepts a block that closes the socket once you are done. +Method names for requests match the API calls but snake cased. example: ```ruby -r_client.run do - # GetVersion - resp = r_client.get_version +# GetVersion +resp = r_client.get_version - # SetCurrentProgramScene - r_client.set_current_program_scene("BRB") -end +# SetCurrentProgramScene +r_client.set_current_program_scene("BRB") ``` For a full list of requests refer to [Requests](https://github.com/obsproject/obs-websocket/blob/master/docs/generated/protocol.md#requests) diff --git a/Rakefile b/Rakefile index b337b7c..c2147fe 100644 --- a/Rakefile +++ b/Rakefile @@ -14,7 +14,7 @@ task :events do ruby filepath end task :levels do - filepath = File.join(HERE, "examples", "levels", "main.rb") + filepath = File.join(HERE, "examples", "events", "main.rb") ruby filepath end task :scene_rotate do diff --git a/examples/events/Gemfile b/examples/events/Gemfile deleted file mode 100644 index 6c9573f..0000000 --- a/examples/events/Gemfile +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -source "https://rubygems.org" - -# gem "rails" - -gem "obsws", path: "../.." diff --git a/examples/events/Gemfile.lock b/examples/events/Gemfile.lock deleted file mode 100644 index 597af39..0000000 --- a/examples/events/Gemfile.lock +++ /dev/null @@ -1,25 +0,0 @@ -PATH - remote: ../.. - specs: - obsws (0.1.3) - observer (~> 0.1.1) - waitutil (~> 0.2.1) - websocket-driver (~> 0.7.5) - -GEM - remote: https://rubygems.org/ - specs: - observer (0.1.1) - waitutil (0.2.1) - websocket-driver (0.7.5) - websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.5) - -PLATFORMS - x64-mingw-ucrt - -DEPENDENCIES - obsws! - -BUNDLED WITH - 2.3.22 diff --git a/examples/events/main.rb b/examples/events/main.rb index ab71bb6..7621c2e 100644 --- a/examples/events/main.rb +++ b/examples/events/main.rb @@ -1,4 +1,4 @@ -require "obsws" +require_relative "../../lib/obsws" require "yaml" OBSWS::LOGGER.info! @@ -53,4 +53,4 @@ def conn_from_yaml YAML.load_file("obs.yml", symbolize_names: true)[:connection] end -Main.new(**conn_from_yaml).run if $0 == __FILE__ +Main.new(**conn_from_yaml).run if $PROGRAM_NAME == __FILE__ diff --git a/examples/levels/Gemfile b/examples/levels/Gemfile deleted file mode 100644 index 6c9573f..0000000 --- a/examples/levels/Gemfile +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -source "https://rubygems.org" - -# gem "rails" - -gem "obsws", path: "../.." diff --git a/examples/levels/Gemfile.lock b/examples/levels/Gemfile.lock deleted file mode 100644 index e7af631..0000000 --- a/examples/levels/Gemfile.lock +++ /dev/null @@ -1,25 +0,0 @@ -PATH - remote: ../.. - specs: - obsws (0.1.3) - observer (~> 0.1.1) - waitutil (~> 0.2.1) - websocket-driver (~> 0.7.5) - -GEM - remote: https://rubygems.org/ - specs: - observer (0.1.1) - waitutil (0.2.1) - websocket-driver (0.7.5) - websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.5) - -PLATFORMS - x64-mingw-ucrt - -DEPENDENCIES - obsws! - -BUNDLED WITH - 2.4.15 diff --git a/examples/levels/main.rb b/examples/levels/main.rb index 189254a..7e71736 100644 --- a/examples/levels/main.rb +++ b/examples/levels/main.rb @@ -1,4 +1,4 @@ -require "obsws" +require_relative "../../lib/obsws" require "yaml" OBSWS::LOGGER.info! @@ -45,4 +45,4 @@ def conn_from_yaml YAML.load_file("obs.yml", symbolize_names: true)[:connection] end -Main.new(**conn_from_yaml).run if $0 == __FILE__ +Main.new(**conn_from_yaml).run if $PROGRAM_NAME == __FILE__ diff --git a/examples/scene_rotate/Gemfile b/examples/scene_rotate/Gemfile deleted file mode 100644 index 6c9573f..0000000 --- a/examples/scene_rotate/Gemfile +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -source "https://rubygems.org" - -# gem "rails" - -gem "obsws", path: "../.." diff --git a/examples/scene_rotate/Gemfile.lock b/examples/scene_rotate/Gemfile.lock deleted file mode 100644 index 597af39..0000000 --- a/examples/scene_rotate/Gemfile.lock +++ /dev/null @@ -1,25 +0,0 @@ -PATH - remote: ../.. - specs: - obsws (0.1.3) - observer (~> 0.1.1) - waitutil (~> 0.2.1) - websocket-driver (~> 0.7.5) - -GEM - remote: https://rubygems.org/ - specs: - observer (0.1.1) - waitutil (0.2.1) - websocket-driver (0.7.5) - websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.5) - -PLATFORMS - x64-mingw-ucrt - -DEPENDENCIES - obsws! - -BUNDLED WITH - 2.3.22 diff --git a/examples/scene_rotate/main.rb b/examples/scene_rotate/main.rb index a631015..9134418 100644 --- a/examples/scene_rotate/main.rb +++ b/examples/scene_rotate/main.rb @@ -1,21 +1,24 @@ -require "obsws" +require_relative "../../lib/obsws" require "yaml" OBSWS::LOGGER.info! -def conn_from_yaml - YAML.load_file("obs.yml", symbolize_names: true)[:connection] -end +class Main + def conn_from_yaml + YAML.load_file("obs.yml", symbolize_names: true)[:connection] + end -def main - OBSWS::Requests::Client.new(**conn_from_yaml).run do |client| - resp = client.get_scene_list - resp.scenes.reverse_each do |scene| - puts "Switching to scene #{scene[:sceneName]}" - client.set_current_program_scene(scene[:sceneName]) - sleep(0.5) + def run + OBSWS::Requests::Client.new(**conn_from_yaml).run do |client| + resp = client.get_scene_list + resp.scenes.reverse_each do |scene| + puts "Switching to scene #{scene[:sceneName]}" + client.set_current_program_scene(scene[:sceneName]) + sleep(0.5) + end end end end -main if $0 == __FILE__ + +Main.new.run if $PROGRAM_NAME == __FILE__ diff --git a/main.rb b/main.rb index 0a6f259..b360913 100644 --- a/main.rb +++ b/main.rb @@ -1,6 +1,7 @@ require_relative "lib/obsws" -def main +class Main + def run OBSWS::Requests::Client.new( host: "localhost", port: 4455, @@ -9,6 +10,7 @@ def main # Toggle the mute state of your Mic input client.toggle_input_mute("Mic/Aux") end + end end -main if $0 == __FILE__ +Main.new.run if $PROGRAM_NAME == __FILE__