Debian Codegraph @

ruby/lib/core/url.rb

raw
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18
19 module Qpid::Proton
20
21 # @deprecated use {URI} or {String}
22 class URL
23 include Util::Deprecation
24
25 attr_reader :scheme
26 attr_reader :username
27 alias user username
28 attr_reader :password
29 attr_reader :host
30 attr_reader :path
31
32 # Parse a string, return a new URL
33 # @param url [#to_s] the URL string
34 def initialize(url = nil)
35 deprecated self.class, 'URI or String'
36 if url
37 @url = Cproton.pn_url_parse(url.to_s)
38 if @url.nil?
39 raise ::ArgumentError.new("invalid url: #{url}")
40 end
41 else
42 @url = Cproton.pn_url
43 end
44 @scheme = Cproton.pn_url_get_scheme(@url)
45 @username = Cproton.pn_url_get_username(@url)
46 @password = Cproton.pn_url_get_password(@url)
47 @host = Cproton.pn_url_get_host(@url)
48 @port = Cproton.pn_url_get_port(@url)
49 @path = Cproton.pn_url_get_path(@url)
50 defaults
51 end
52
53 def port=(port)
54 if port.nil?
55 Cproton.pn_url_set_port(@url, nil)
56 else
57 Cproton.pn_url_set_port(@url, port)
58 end
59 end
60
61 def port
62 Cproton.pn_url_get_port(@url).to_i
63 end
64
65 # @return [String] Convert to string
66 def to_s
67 "#{@scheme}://#{@username.nil? ? '' : @username}#{@password.nil? ? '' : '@' + @password + ':'}#{@host}:#{@port}/#{@path}"
68 end
69
70 # @return [String] Allow implicit conversion by {String#try_convert}
71 alias to_str to_s
72
73 private
74
75 def defaults
76 @scheme = @scheme || "amqp"
77 @host = @host || "0.0.0.0"
78 @port = @port || 5672
79 end
80 end
81 end