Skip to content

Commit

Permalink
Some patches (#49)
Browse files Browse the repository at this point in the history
* Remove unused Hash#symbol_keys

* Don't override Hash#stringify_keys from ActiveSupport

* Don't pollute Object class

* Add frozen_string_literal
  • Loading branch information
amerov authored Apr 10, 2024
1 parent 2d7456f commit 820dee1
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 34 deletions.
2 changes: 2 additions & 0 deletions lib/livekit/auth_mixin.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module LiveKit
# Create authenticated headers when keys are provided
module AuthMixin
Expand Down
4 changes: 3 additions & 1 deletion lib/livekit/egress_service_client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "livekit/proto/livekit_egress_twirp"
require "livekit/auth_mixin"
require 'livekit/utils'
Expand All @@ -9,7 +11,7 @@ class EgressServiceClient < Twirp::Client
attr_accessor :api_key, :api_secret

def initialize(base_url, api_key: nil, api_secret: nil)
super(File.join(to_http_url(base_url), "/twirp"))
super(File.join(Utils.to_http_url(base_url), "/twirp"))
@api_key = api_key
@api_secret = api_secret
end
Expand Down
2 changes: 2 additions & 0 deletions lib/livekit/grants.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module LiveKit
class ClaimGrant
attr_accessor :identity, :name, :metadata, :sha256, :video
Expand Down
2 changes: 1 addition & 1 deletion lib/livekit/ingress_service_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class IngressServiceClient < Twirp::Client
attr_accessor :api_key, :api_secret

def initialize(base_url, api_key: nil, api_secret: nil)
super(File.join(to_http_url(base_url), "/twirp"))
super(File.join(Utils.to_http_url(base_url), "/twirp"))
@api_key = api_key
@api_secret = api_secret
end
Expand Down
4 changes: 3 additions & 1 deletion lib/livekit/room_service_client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "livekit/proto/livekit_room_twirp"
require "livekit/auth_mixin"
require 'livekit/utils'
Expand All @@ -9,7 +11,7 @@ class RoomServiceClient < Twirp::Client
attr_accessor :api_key, :api_secret

def initialize(base_url, api_key: nil, api_secret: nil)
super(File.join(to_http_url(base_url), "/twirp"))
super(File.join(Utils.to_http_url(base_url), "/twirp"))
@api_key = api_key
@api_secret = api_secret
end
Expand Down
58 changes: 27 additions & 31 deletions lib/livekit/utils.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
class ::Hash
# via https://stackoverflow.com/a/25835016/2257038
def stringify_keys
h = self.map do |k, v|
v_str = if v.instance_of? Hash
v.stringify_keys
else
v
end
# frozen_string_literal: true

[k.to_s, v_str]
unless Hash.method_defined?(:stringify_keys)
class Hash
# via https://stackoverflow.com/a/25835016/2257038
def stringify_keys
h = self.map do |k, v|
v_str = if v.instance_of? Hash
v.stringify_keys
else
v
end

[k.to_s, v_str]
end
Hash[h]
end
Hash[h]
end
end

# via https://stackoverflow.com/a/25835016/2257038
def symbol_keys
h = self.map do |k, v|
v_sym = if v.instance_of? Hash
v.symbol_keys
else
v
end

[k.to_sym, v_sym]
module LiveKit
module Utils
def to_http_url(url)
if url.start_with?("ws")
# replace ws prefix to http
return url.sub(/^ws/, "http")
else
return url
end
end
Hash[h]

module_function :to_http_url
end
end

# convert websocket urls to http
def to_http_url(url)
if url.start_with?("ws")
# replace ws prefix to http
return url.sub(/^ws/, "http")
else
return url
end
end

0 comments on commit 820dee1

Please sign in to comment.