Class: Webtube::Location
- Inherits:
-
Object
- Object
- Webtube::Location
- Defined in:
- lib/webtube.rb
Overview
Represents a parsed WebSocket URL.
Instance Attribute Summary collapse
-
#default_port ⇒ Object
readonly
Returns the value of attribute default_port.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#requestee ⇒ Object
readonly
Returns the value of attribute requestee.
Instance Method Summary collapse
-
#host_and_maybe_port ⇒ Object
Returns the hostname and, if non-default, the port number separated by colon.
-
#initialize(url) ⇒ Location
constructor
A new instance of Location.
- #ssl? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(url) ⇒ Location
Returns a new instance of Location.
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/webtube.rb', line 484 def initialize url super() # force into a single-byte encoding so urlencoding can # work correctly url = url.dup.force_encoding Encoding::ASCII_8BIT # ensure that any whitespace, ASCII on-printables, and # some popular text delimiters (parens, brokets, brackets, # and broken bar) in [[url]] are properly urlencoded url.gsub! ' ', '+' url.gsub!(/[^\x21-\x7E]/){'%%%02X' % $&.ord} url.gsub!(/[()<>\[\]\|]/){'%%%02X' % $&.ord} # We'll replace the WebSocket protocol prefix with an # HTTP-based one so [[URI::parse]] would know how to # parse the rest of the URL. case url when /\A(ws|http):/ then http_url = 'http:' + $' @ssl = false @default_port = 80 when /\A(wss|https):/ then http_url = 'https:' + $' @ssl = true @default_port = 443 else raise "unknown URI scheme; use ws: or wss: instead" end http_uri = URI.parse http_url @host = http_uri.host @port = http_uri.port @requestee = http_uri.path if @requestee.empty? then @requestee = '/' end @requestee += '?' + http_uri.query \ if http_uri.query return end |
Instance Attribute Details
#default_port ⇒ Object (readonly)
Returns the value of attribute default_port.
534 535 536 |
# File 'lib/webtube.rb', line 534 def default_port @default_port end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
535 536 537 |
# File 'lib/webtube.rb', line 535 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
536 537 538 |
# File 'lib/webtube.rb', line 536 def port @port end |
#requestee ⇒ Object (readonly)
Returns the value of attribute requestee.
537 538 539 |
# File 'lib/webtube.rb', line 537 def requestee @requestee end |
Instance Method Details
#host_and_maybe_port ⇒ Object
Returns the hostname and, if non-default, the port number separated by colon. This combination is used in HTTP 1.1
- [Host]
-
header fields but also in URIs.
542 543 544 545 546 547 |
# File 'lib/webtube.rb', line 542 def host_and_maybe_port h = @host h += ":#@port" \ unless @port == @default_port return h end |
#ssl? ⇒ Boolean
530 531 532 |
# File 'lib/webtube.rb', line 530 def ssl? return @ssl end |
#to_s ⇒ Object
522 523 524 525 526 527 528 |
# File 'lib/webtube.rb', line 522 def to_s s = !ssl? ? 'ws:' : 'wss:' s += '//' + host_and_maybe_port s += @requestee \ unless @requestee == '/' return s end |