Skip to content

Commit

Permalink
use different approach with refine and make sure to address nested keys
Browse files Browse the repository at this point in the history
  • Loading branch information
m-hukic committed May 7, 2024
1 parent 86d1911 commit 44e04b6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/livekit.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

require "livekit/access_token"
require "livekit/utils"
require "livekit/grants"
require "livekit/token_verifier"
require "livekit/utils"
require "livekit/version"

# required since generated protobufs does use `require` instead of `require_relative`
Expand Down
4 changes: 3 additions & 1 deletion lib/livekit/grants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def to_hash
end

class VideoGrant
using LiveKit::Utils::StringifyKeysRefinement

attr_accessor :roomCreate, :roomJoin, :roomList, :roomRecord, :roomAdmin,
:room, :canPublish, :canPublishSources, :canSubscribe, :canPublishData,
:canUpdateOwnMetadata, :hidden, :recorder, :ingressAdmin
Expand Down Expand Up @@ -88,7 +90,7 @@ def initialize(
def self.from_hash(hash)
return nil if hash.nil?

hash = hash.transform_keys(&:to_s)
hash = hash.stringify_keys

VideoGrant.new(
roomCreate: hash["roomCreate"],
Expand Down
21 changes: 15 additions & 6 deletions lib/livekit/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

module LiveKit
module Utils
# Utility function to convert WebSocket URLs to HTTP URLs
def to_http_url(url)
if url.start_with?("ws")
# replace ws prefix to http
return url.sub(/^ws/, "http")
# Replace 'ws' prefix with 'http'
url.sub(/^ws/, "http")
else
return url
url
end
end

module_function :to_http_url

module StringifyKeysRefinement
refine Hash do
def stringify_keys
transform_keys(&:to_s).transform_values do |value|
value.is_a?(Hash) ? value.stringify_keys : value
end
end
end
end
end
end
# convert websocket urls to http
end

0 comments on commit 44e04b6

Please sign in to comment.