Module: ERPC::URLs
- Defined in:
- lib/erpc_sdk/config.rb
Class Method Summary collapse
- .escape_path(value) ⇒ Object
- .normalize_endpoint(value, local_http_only: false) ⇒ Object
- .websocket(endpoint, api_key, path = "") ⇒ Object
- .with_path(endpoint, path) ⇒ Object
Class Method Details
.escape_path(value) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/erpc_sdk/config.rb', line 50 def escape_path(value) value.to_s.b.each_byte.map do |byte| character = byte.chr character.match?(/[A-Za-z0-9_.~-]/) ? character : format("%%%02X", byte) end.join end |
.normalize_endpoint(value, local_http_only: false) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/erpc_sdk/config.rb', line 14 def normalize_endpoint(value, local_http_only: false) uri = URI.parse(value.to_s) unless uri.absolute? && uri.host && %w[http https].include?(uri.scheme) raise ConfigError, "endpoint must be an absolute HTTP(S) URL" end local = %w[127.0.0.1 ::1 localhost].include?(uri.hostname) if local_http_only && uri.scheme != "https" && !(uri.scheme == "http" && local) raise ConfigError, "endpoint must use HTTPS except on localhost" end uri.query = nil uri.fragment = nil uri.path = uri.path.sub(%r{/+\z}, "") uri.path = "/" if uri.path.empty? uri.to_s rescue URI::InvalidURIError, ArgumentError raise ConfigError, "endpoint must be an absolute HTTP(S) URL" end |
.websocket(endpoint, api_key, path = "") ⇒ Object
43 44 45 46 47 48 |
# File 'lib/erpc_sdk/config.rb', line 43 def websocket(endpoint, api_key, path = "") uri = URI.parse(with_path(endpoint, path)) uri.scheme = uri.scheme == "https" ? "wss" : "ws" uri.query = URI.encode_www_form("api-key" => api_key) uri.to_s end |
.with_path(endpoint, path) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/erpc_sdk/config.rb', line 34 def with_path(endpoint, path) uri = URI.parse(endpoint) base = uri.path.sub(%r{/+\z}, "") uri.path = "#{base}/#{path.sub(%r{\A/+}, "")}" uri.query = nil uri.fragment = nil uri.to_s end |