Class: DataMetaParse::Uri

Inherits:
Object
  • Object
show all
Defined in:
lib/dataMetaParse/uriDataMeta.rb

Overview

DataMeta URI with all the parts.

The user story:

  • DataMeta URIs are used in DataMeta Scripts to specify all aspects of a data set identity and location.

  • For physical access, a URI may be disassembled using this grammar and parser, the parts obtained so may be used to access concrete physical resources.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proto, user, pwd, host, port, path, props) ⇒ Uri

Creates an instance of the object.

Parameters:

  • proto (String)

    see the property #proto

  • user (String)

    see the property #user

  • pwd (String)

    see the property #pwd

  • host (String)

    see the property #host

  • port (String)

    see the property #port

  • path (String)

    see the property #path

  • props (String)

    see the property #props

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
# File 'lib/dataMetaParse/uriDataMeta.rb', line 54

def initialize(proto, user, pwd, host, port, path, props)
    raise ArgumentError, 'Password specified but user not' if !user && pwd
    raise ArgumentError, 'For file protocol, only path can be specified' if proto == 'file' && (
        user || pwd || host || port || !props.empty?)

    @proto, @user, @pwd, @host, @port, @path, @props = proto, user, pwd, host, port, path, props
end

Instance Attribute Details

#hostString (readonly)

Returns the host part of the URI.

Returns:

  • (String)

    the host part of the URI



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/dataMetaParse/uriDataMeta.rb', line 40

class Uri
    attr_reader :proto, :user, :pwd, :host, :port, :path, :props

=begin rdoc
Creates an instance of the object.

@param [String] proto see the property {#proto}
@param [String] user see the property {#user}
@param [String] pwd see the property {#pwd}
@param [String] host see the property {#host}
@param [String] port see the property {#port}
@param [String] path see the property {#path}
@param [String] props see the property {#props}
=end
    def initialize(proto, user, pwd, host, port, path, props)
        raise ArgumentError, 'Password specified but user not' if !user && pwd
        raise ArgumentError, 'For file protocol, only path can be specified' if proto == 'file' && (
            user || pwd || host || port || !props.empty?)

        @proto, @user, @pwd, @host, @port, @path, @props = proto, user, pwd, host, port, path, props
    end

=begin rdoc
Equality to the other
=end
    def ==(other)
        @proto == other.proto && @user == other.user && @pwd == other.pwd && @host == other.host &&
                @port == other.port && @path == other.path && @props.eql?(other.props)
    end

=begin rdoc
Same as the {#==}
=end
    def eql?(other); self == other end

=begin rdoc
Loads the grammar - has to be done only once per RVM start. Relies on loading the basics.
=end
    def self.loadRulz
        Treetop.load(File.join(File.dirname(__FILE__), 'urlDataMeta'))
    end

=begin rdoc
Instance to textual.
=end
    def to_s
        if @proto == 'file'
            "file://#{@path}"
        else
            result = ''
            result << @proto << '://'
            result << URI.encode_www_form_component(@user) if @user
            result << ':' << URI.encode_www_form_component(@pwd) if @user && @pwd
            result << '@' if @user
            result << @host
            result << ':' << @port.to_s if @port
            result << '/' if @path || !@props.empty?
            result << @path if @path

            result << '?' << @props.keys.map { |k|
                v=@props[k]; v ? "#{k}=#{URI.encode_www_form_component(v)}" : "#{k}"
            }.join('&') unless @props.empty?

            result
        end
    end

=begin rdoc
Parses the source into the instance of the object.
@param [String] source the source, the URI specification to parse into the instance of this class
=end
    def self.parse(source)
        fileSignature = 'file://'
        if source.start_with?(fileSignature)
            Uri.new('file', nil, nil, nil, nil, source[fileSignature.length..-1], {})
        else
            parser = DataMetaUrlParser.new
            ast = parser.parse(source)
            return nil unless ast
            proto = ast.proto.text_value
            user = ast.user? ? ast.userPwd.user.text_value : nil
            pwd = ast.pwd? ? URI.decode_www_form_component(ast.userPwd.password) : nil
            host = ast.host.text_value
            port = ast.port? ? ast.port.number : nil
            path = ast.path? ? ast.uTail.path : nil
            query = ast.query? ? ast.uTail.query : nil
            props = {}
            if query
                pairs = query.split('&')
                pairs.each { |pairString|
                    key, val = pairString.split('=')
                    props[key] = val ? URI.decode_www_form_component(val) : nil # this is caused by &paramA&paramB=b, in which case paramA will be nil
                }
            end
            Uri.new(proto, user, pwd, host, port, path, props)
        end
    end

end

#pathString (readonly)

Returns for the file protocol, path as specified, full or relative. For any other URI, the part between the closing ‘/’ after the host:port part and the query part starting with ‘?’. This means, for all other protocols except file, the path part will never have an initial slash.

Returns:

  • (String)

    for the file protocol, path as specified, full or relative. For any other URI, the part between the closing ‘/’ after the host:port part and the query part starting with ‘?’. This means, for all other protocols except file, the path part will never have an initial slash.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/dataMetaParse/uriDataMeta.rb', line 40

class Uri
    attr_reader :proto, :user, :pwd, :host, :port, :path, :props

=begin rdoc
Creates an instance of the object.

@param [String] proto see the property {#proto}
@param [String] user see the property {#user}
@param [String] pwd see the property {#pwd}
@param [String] host see the property {#host}
@param [String] port see the property {#port}
@param [String] path see the property {#path}
@param [String] props see the property {#props}
=end
    def initialize(proto, user, pwd, host, port, path, props)
        raise ArgumentError, 'Password specified but user not' if !user && pwd
        raise ArgumentError, 'For file protocol, only path can be specified' if proto == 'file' && (
            user || pwd || host || port || !props.empty?)

        @proto, @user, @pwd, @host, @port, @path, @props = proto, user, pwd, host, port, path, props
    end

=begin rdoc
Equality to the other
=end
    def ==(other)
        @proto == other.proto && @user == other.user && @pwd == other.pwd && @host == other.host &&
                @port == other.port && @path == other.path && @props.eql?(other.props)
    end

=begin rdoc
Same as the {#==}
=end
    def eql?(other); self == other end

=begin rdoc
Loads the grammar - has to be done only once per RVM start. Relies on loading the basics.
=end
    def self.loadRulz
        Treetop.load(File.join(File.dirname(__FILE__), 'urlDataMeta'))
    end

=begin rdoc
Instance to textual.
=end
    def to_s
        if @proto == 'file'
            "file://#{@path}"
        else
            result = ''
            result << @proto << '://'
            result << URI.encode_www_form_component(@user) if @user
            result << ':' << URI.encode_www_form_component(@pwd) if @user && @pwd
            result << '@' if @user
            result << @host
            result << ':' << @port.to_s if @port
            result << '/' if @path || !@props.empty?
            result << @path if @path

            result << '?' << @props.keys.map { |k|
                v=@props[k]; v ? "#{k}=#{URI.encode_www_form_component(v)}" : "#{k}"
            }.join('&') unless @props.empty?

            result
        end
    end

=begin rdoc
Parses the source into the instance of the object.
@param [String] source the source, the URI specification to parse into the instance of this class
=end
    def self.parse(source)
        fileSignature = 'file://'
        if source.start_with?(fileSignature)
            Uri.new('file', nil, nil, nil, nil, source[fileSignature.length..-1], {})
        else
            parser = DataMetaUrlParser.new
            ast = parser.parse(source)
            return nil unless ast
            proto = ast.proto.text_value
            user = ast.user? ? ast.userPwd.user.text_value : nil
            pwd = ast.pwd? ? URI.decode_www_form_component(ast.userPwd.password) : nil
            host = ast.host.text_value
            port = ast.port? ? ast.port.number : nil
            path = ast.path? ? ast.uTail.path : nil
            query = ast.query? ? ast.uTail.query : nil
            props = {}
            if query
                pairs = query.split('&')
                pairs.each { |pairString|
                    key, val = pairString.split('=')
                    props[key] = val ? URI.decode_www_form_component(val) : nil # this is caused by &paramA&paramB=b, in which case paramA will be nil
                }
            end
            Uri.new(proto, user, pwd, host, port, path, props)
        end
    end

end

#portFixnum (readonly)

Returns the port number specified in the URI, can be nil.

Returns:

  • (Fixnum)

    the port number specified in the URI, can be nil



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/dataMetaParse/uriDataMeta.rb', line 40

class Uri
    attr_reader :proto, :user, :pwd, :host, :port, :path, :props

=begin rdoc
Creates an instance of the object.

@param [String] proto see the property {#proto}
@param [String] user see the property {#user}
@param [String] pwd see the property {#pwd}
@param [String] host see the property {#host}
@param [String] port see the property {#port}
@param [String] path see the property {#path}
@param [String] props see the property {#props}
=end
    def initialize(proto, user, pwd, host, port, path, props)
        raise ArgumentError, 'Password specified but user not' if !user && pwd
        raise ArgumentError, 'For file protocol, only path can be specified' if proto == 'file' && (
            user || pwd || host || port || !props.empty?)

        @proto, @user, @pwd, @host, @port, @path, @props = proto, user, pwd, host, port, path, props
    end

=begin rdoc
Equality to the other
=end
    def ==(other)
        @proto == other.proto && @user == other.user && @pwd == other.pwd && @host == other.host &&
                @port == other.port && @path == other.path && @props.eql?(other.props)
    end

=begin rdoc
Same as the {#==}
=end
    def eql?(other); self == other end

=begin rdoc
Loads the grammar - has to be done only once per RVM start. Relies on loading the basics.
=end
    def self.loadRulz
        Treetop.load(File.join(File.dirname(__FILE__), 'urlDataMeta'))
    end

=begin rdoc
Instance to textual.
=end
    def to_s
        if @proto == 'file'
            "file://#{@path}"
        else
            result = ''
            result << @proto << '://'
            result << URI.encode_www_form_component(@user) if @user
            result << ':' << URI.encode_www_form_component(@pwd) if @user && @pwd
            result << '@' if @user
            result << @host
            result << ':' << @port.to_s if @port
            result << '/' if @path || !@props.empty?
            result << @path if @path

            result << '?' << @props.keys.map { |k|
                v=@props[k]; v ? "#{k}=#{URI.encode_www_form_component(v)}" : "#{k}"
            }.join('&') unless @props.empty?

            result
        end
    end

=begin rdoc
Parses the source into the instance of the object.
@param [String] source the source, the URI specification to parse into the instance of this class
=end
    def self.parse(source)
        fileSignature = 'file://'
        if source.start_with?(fileSignature)
            Uri.new('file', nil, nil, nil, nil, source[fileSignature.length..-1], {})
        else
            parser = DataMetaUrlParser.new
            ast = parser.parse(source)
            return nil unless ast
            proto = ast.proto.text_value
            user = ast.user? ? ast.userPwd.user.text_value : nil
            pwd = ast.pwd? ? URI.decode_www_form_component(ast.userPwd.password) : nil
            host = ast.host.text_value
            port = ast.port? ? ast.port.number : nil
            path = ast.path? ? ast.uTail.path : nil
            query = ast.query? ? ast.uTail.query : nil
            props = {}
            if query
                pairs = query.split('&')
                pairs.each { |pairString|
                    key, val = pairString.split('=')
                    props[key] = val ? URI.decode_www_form_component(val) : nil # this is caused by &paramA&paramB=b, in which case paramA will be nil
                }
            end
            Uri.new(proto, user, pwd, host, port, path, props)
        end
    end

end

#propsHash (readonly)

Returns hash of properties keyed by the property name and pointing to a value if any.

Returns:

  • (Hash)

    hash of properties keyed by the property name and pointing to a value if any



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/dataMetaParse/uriDataMeta.rb', line 40

class Uri
    attr_reader :proto, :user, :pwd, :host, :port, :path, :props

=begin rdoc
Creates an instance of the object.

@param [String] proto see the property {#proto}
@param [String] user see the property {#user}
@param [String] pwd see the property {#pwd}
@param [String] host see the property {#host}
@param [String] port see the property {#port}
@param [String] path see the property {#path}
@param [String] props see the property {#props}
=end
    def initialize(proto, user, pwd, host, port, path, props)
        raise ArgumentError, 'Password specified but user not' if !user && pwd
        raise ArgumentError, 'For file protocol, only path can be specified' if proto == 'file' && (
            user || pwd || host || port || !props.empty?)

        @proto, @user, @pwd, @host, @port, @path, @props = proto, user, pwd, host, port, path, props
    end

=begin rdoc
Equality to the other
=end
    def ==(other)
        @proto == other.proto && @user == other.user && @pwd == other.pwd && @host == other.host &&
                @port == other.port && @path == other.path && @props.eql?(other.props)
    end

=begin rdoc
Same as the {#==}
=end
    def eql?(other); self == other end

=begin rdoc
Loads the grammar - has to be done only once per RVM start. Relies on loading the basics.
=end
    def self.loadRulz
        Treetop.load(File.join(File.dirname(__FILE__), 'urlDataMeta'))
    end

=begin rdoc
Instance to textual.
=end
    def to_s
        if @proto == 'file'
            "file://#{@path}"
        else
            result = ''
            result << @proto << '://'
            result << URI.encode_www_form_component(@user) if @user
            result << ':' << URI.encode_www_form_component(@pwd) if @user && @pwd
            result << '@' if @user
            result << @host
            result << ':' << @port.to_s if @port
            result << '/' if @path || !@props.empty?
            result << @path if @path

            result << '?' << @props.keys.map { |k|
                v=@props[k]; v ? "#{k}=#{URI.encode_www_form_component(v)}" : "#{k}"
            }.join('&') unless @props.empty?

            result
        end
    end

=begin rdoc
Parses the source into the instance of the object.
@param [String] source the source, the URI specification to parse into the instance of this class
=end
    def self.parse(source)
        fileSignature = 'file://'
        if source.start_with?(fileSignature)
            Uri.new('file', nil, nil, nil, nil, source[fileSignature.length..-1], {})
        else
            parser = DataMetaUrlParser.new
            ast = parser.parse(source)
            return nil unless ast
            proto = ast.proto.text_value
            user = ast.user? ? ast.userPwd.user.text_value : nil
            pwd = ast.pwd? ? URI.decode_www_form_component(ast.userPwd.password) : nil
            host = ast.host.text_value
            port = ast.port? ? ast.port.number : nil
            path = ast.path? ? ast.uTail.path : nil
            query = ast.query? ? ast.uTail.query : nil
            props = {}
            if query
                pairs = query.split('&')
                pairs.each { |pairString|
                    key, val = pairString.split('=')
                    props[key] = val ? URI.decode_www_form_component(val) : nil # this is caused by &paramA&paramB=b, in which case paramA will be nil
                }
            end
            Uri.new(proto, user, pwd, host, port, path, props)
        end
    end

end

#protoString (readonly)

Returns the protocol part, such as http, ftp, socparc etc.

Returns:

  • (String)

    the protocol part, such as http, ftp, socparc etc



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/dataMetaParse/uriDataMeta.rb', line 40

class Uri
    attr_reader :proto, :user, :pwd, :host, :port, :path, :props

=begin rdoc
Creates an instance of the object.

@param [String] proto see the property {#proto}
@param [String] user see the property {#user}
@param [String] pwd see the property {#pwd}
@param [String] host see the property {#host}
@param [String] port see the property {#port}
@param [String] path see the property {#path}
@param [String] props see the property {#props}
=end
    def initialize(proto, user, pwd, host, port, path, props)
        raise ArgumentError, 'Password specified but user not' if !user && pwd
        raise ArgumentError, 'For file protocol, only path can be specified' if proto == 'file' && (
            user || pwd || host || port || !props.empty?)

        @proto, @user, @pwd, @host, @port, @path, @props = proto, user, pwd, host, port, path, props
    end

=begin rdoc
Equality to the other
=end
    def ==(other)
        @proto == other.proto && @user == other.user && @pwd == other.pwd && @host == other.host &&
                @port == other.port && @path == other.path && @props.eql?(other.props)
    end

=begin rdoc
Same as the {#==}
=end
    def eql?(other); self == other end

=begin rdoc
Loads the grammar - has to be done only once per RVM start. Relies on loading the basics.
=end
    def self.loadRulz
        Treetop.load(File.join(File.dirname(__FILE__), 'urlDataMeta'))
    end

=begin rdoc
Instance to textual.
=end
    def to_s
        if @proto == 'file'
            "file://#{@path}"
        else
            result = ''
            result << @proto << '://'
            result << URI.encode_www_form_component(@user) if @user
            result << ':' << URI.encode_www_form_component(@pwd) if @user && @pwd
            result << '@' if @user
            result << @host
            result << ':' << @port.to_s if @port
            result << '/' if @path || !@props.empty?
            result << @path if @path

            result << '?' << @props.keys.map { |k|
                v=@props[k]; v ? "#{k}=#{URI.encode_www_form_component(v)}" : "#{k}"
            }.join('&') unless @props.empty?

            result
        end
    end

=begin rdoc
Parses the source into the instance of the object.
@param [String] source the source, the URI specification to parse into the instance of this class
=end
    def self.parse(source)
        fileSignature = 'file://'
        if source.start_with?(fileSignature)
            Uri.new('file', nil, nil, nil, nil, source[fileSignature.length..-1], {})
        else
            parser = DataMetaUrlParser.new
            ast = parser.parse(source)
            return nil unless ast
            proto = ast.proto.text_value
            user = ast.user? ? ast.userPwd.user.text_value : nil
            pwd = ast.pwd? ? URI.decode_www_form_component(ast.userPwd.password) : nil
            host = ast.host.text_value
            port = ast.port? ? ast.port.number : nil
            path = ast.path? ? ast.uTail.path : nil
            query = ast.query? ? ast.uTail.query : nil
            props = {}
            if query
                pairs = query.split('&')
                pairs.each { |pairString|
                    key, val = pairString.split('=')
                    props[key] = val ? URI.decode_www_form_component(val) : nil # this is caused by &paramA&paramB=b, in which case paramA will be nil
                }
            end
            Uri.new(proto, user, pwd, host, port, path, props)
        end
    end

end

#pwdString (readonly)

Returns the password part of the URI, can be nil and for some URIs may be in properties.

Returns:

  • (String)

    the password part of the URI, can be nil and for some URIs may be in properties



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/dataMetaParse/uriDataMeta.rb', line 40

class Uri
    attr_reader :proto, :user, :pwd, :host, :port, :path, :props

=begin rdoc
Creates an instance of the object.

@param [String] proto see the property {#proto}
@param [String] user see the property {#user}
@param [String] pwd see the property {#pwd}
@param [String] host see the property {#host}
@param [String] port see the property {#port}
@param [String] path see the property {#path}
@param [String] props see the property {#props}
=end
    def initialize(proto, user, pwd, host, port, path, props)
        raise ArgumentError, 'Password specified but user not' if !user && pwd
        raise ArgumentError, 'For file protocol, only path can be specified' if proto == 'file' && (
            user || pwd || host || port || !props.empty?)

        @proto, @user, @pwd, @host, @port, @path, @props = proto, user, pwd, host, port, path, props
    end

=begin rdoc
Equality to the other
=end
    def ==(other)
        @proto == other.proto && @user == other.user && @pwd == other.pwd && @host == other.host &&
                @port == other.port && @path == other.path && @props.eql?(other.props)
    end

=begin rdoc
Same as the {#==}
=end
    def eql?(other); self == other end

=begin rdoc
Loads the grammar - has to be done only once per RVM start. Relies on loading the basics.
=end
    def self.loadRulz
        Treetop.load(File.join(File.dirname(__FILE__), 'urlDataMeta'))
    end

=begin rdoc
Instance to textual.
=end
    def to_s
        if @proto == 'file'
            "file://#{@path}"
        else
            result = ''
            result << @proto << '://'
            result << URI.encode_www_form_component(@user) if @user
            result << ':' << URI.encode_www_form_component(@pwd) if @user && @pwd
            result << '@' if @user
            result << @host
            result << ':' << @port.to_s if @port
            result << '/' if @path || !@props.empty?
            result << @path if @path

            result << '?' << @props.keys.map { |k|
                v=@props[k]; v ? "#{k}=#{URI.encode_www_form_component(v)}" : "#{k}"
            }.join('&') unless @props.empty?

            result
        end
    end

=begin rdoc
Parses the source into the instance of the object.
@param [String] source the source, the URI specification to parse into the instance of this class
=end
    def self.parse(source)
        fileSignature = 'file://'
        if source.start_with?(fileSignature)
            Uri.new('file', nil, nil, nil, nil, source[fileSignature.length..-1], {})
        else
            parser = DataMetaUrlParser.new
            ast = parser.parse(source)
            return nil unless ast
            proto = ast.proto.text_value
            user = ast.user? ? ast.userPwd.user.text_value : nil
            pwd = ast.pwd? ? URI.decode_www_form_component(ast.userPwd.password) : nil
            host = ast.host.text_value
            port = ast.port? ? ast.port.number : nil
            path = ast.path? ? ast.uTail.path : nil
            query = ast.query? ? ast.uTail.query : nil
            props = {}
            if query
                pairs = query.split('&')
                pairs.each { |pairString|
                    key, val = pairString.split('=')
                    props[key] = val ? URI.decode_www_form_component(val) : nil # this is caused by &paramA&paramB=b, in which case paramA will be nil
                }
            end
            Uri.new(proto, user, pwd, host, port, path, props)
        end
    end

end

#userString (readonly)

Returns the user id part of the URI, can be nil and for some URIs may be in properties.

Returns:

  • (String)

    the user id part of the URI, can be nil and for some URIs may be in properties



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/dataMetaParse/uriDataMeta.rb', line 40

class Uri
    attr_reader :proto, :user, :pwd, :host, :port, :path, :props

=begin rdoc
Creates an instance of the object.

@param [String] proto see the property {#proto}
@param [String] user see the property {#user}
@param [String] pwd see the property {#pwd}
@param [String] host see the property {#host}
@param [String] port see the property {#port}
@param [String] path see the property {#path}
@param [String] props see the property {#props}
=end
    def initialize(proto, user, pwd, host, port, path, props)
        raise ArgumentError, 'Password specified but user not' if !user && pwd
        raise ArgumentError, 'For file protocol, only path can be specified' if proto == 'file' && (
            user || pwd || host || port || !props.empty?)

        @proto, @user, @pwd, @host, @port, @path, @props = proto, user, pwd, host, port, path, props
    end

=begin rdoc
Equality to the other
=end
    def ==(other)
        @proto == other.proto && @user == other.user && @pwd == other.pwd && @host == other.host &&
                @port == other.port && @path == other.path && @props.eql?(other.props)
    end

=begin rdoc
Same as the {#==}
=end
    def eql?(other); self == other end

=begin rdoc
Loads the grammar - has to be done only once per RVM start. Relies on loading the basics.
=end
    def self.loadRulz
        Treetop.load(File.join(File.dirname(__FILE__), 'urlDataMeta'))
    end

=begin rdoc
Instance to textual.
=end
    def to_s
        if @proto == 'file'
            "file://#{@path}"
        else
            result = ''
            result << @proto << '://'
            result << URI.encode_www_form_component(@user) if @user
            result << ':' << URI.encode_www_form_component(@pwd) if @user && @pwd
            result << '@' if @user
            result << @host
            result << ':' << @port.to_s if @port
            result << '/' if @path || !@props.empty?
            result << @path if @path

            result << '?' << @props.keys.map { |k|
                v=@props[k]; v ? "#{k}=#{URI.encode_www_form_component(v)}" : "#{k}"
            }.join('&') unless @props.empty?

            result
        end
    end

=begin rdoc
Parses the source into the instance of the object.
@param [String] source the source, the URI specification to parse into the instance of this class
=end
    def self.parse(source)
        fileSignature = 'file://'
        if source.start_with?(fileSignature)
            Uri.new('file', nil, nil, nil, nil, source[fileSignature.length..-1], {})
        else
            parser = DataMetaUrlParser.new
            ast = parser.parse(source)
            return nil unless ast
            proto = ast.proto.text_value
            user = ast.user? ? ast.userPwd.user.text_value : nil
            pwd = ast.pwd? ? URI.decode_www_form_component(ast.userPwd.password) : nil
            host = ast.host.text_value
            port = ast.port? ? ast.port.number : nil
            path = ast.path? ? ast.uTail.path : nil
            query = ast.query? ? ast.uTail.query : nil
            props = {}
            if query
                pairs = query.split('&')
                pairs.each { |pairString|
                    key, val = pairString.split('=')
                    props[key] = val ? URI.decode_www_form_component(val) : nil # this is caused by &paramA&paramB=b, in which case paramA will be nil
                }
            end
            Uri.new(proto, user, pwd, host, port, path, props)
        end
    end

end

Class Method Details

.loadRulzObject

Loads the grammar - has to be done only once per RVM start. Relies on loading the basics.



78
79
80
# File 'lib/dataMetaParse/uriDataMeta.rb', line 78

def self.loadRulz
    Treetop.load(File.join(File.dirname(__FILE__), 'urlDataMeta'))
end

.parse(source) ⇒ Object

Parses the source into the instance of the object.

Parameters:

  • source (String)

    the source, the URI specification to parse into the instance of this class



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/dataMetaParse/uriDataMeta.rb', line 111

def self.parse(source)
    fileSignature = 'file://'
    if source.start_with?(fileSignature)
        Uri.new('file', nil, nil, nil, nil, source[fileSignature.length..-1], {})
    else
        parser = DataMetaUrlParser.new
        ast = parser.parse(source)
        return nil unless ast
        proto = ast.proto.text_value
        user = ast.user? ? ast.userPwd.user.text_value : nil
        pwd = ast.pwd? ? URI.decode_www_form_component(ast.userPwd.password) : nil
        host = ast.host.text_value
        port = ast.port? ? ast.port.number : nil
        path = ast.path? ? ast.uTail.path : nil
        query = ast.query? ? ast.uTail.query : nil
        props = {}
        if query
            pairs = query.split('&')
            pairs.each { |pairString|
                key, val = pairString.split('=')
                props[key] = val ? URI.decode_www_form_component(val) : nil # this is caused by &paramA&paramB=b, in which case paramA will be nil
            }
        end
        Uri.new(proto, user, pwd, host, port, path, props)
    end
end

Instance Method Details

#==(other) ⇒ Object

Equality to the other



65
66
67
68
# File 'lib/dataMetaParse/uriDataMeta.rb', line 65

def ==(other)
    @proto == other.proto && @user == other.user && @pwd == other.pwd && @host == other.host &&
            @port == other.port && @path == other.path && @props.eql?(other.props)
end

#eql?(other) ⇒ Boolean

Same as the #==

Returns:

  • (Boolean)


73
# File 'lib/dataMetaParse/uriDataMeta.rb', line 73

def eql?(other); self == other end

#to_sObject

Instance to textual.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dataMetaParse/uriDataMeta.rb', line 85

def to_s
    if @proto == 'file'
        "file://#{@path}"
    else
        result = ''
        result << @proto << '://'
        result << URI.encode_www_form_component(@user) if @user
        result << ':' << URI.encode_www_form_component(@pwd) if @user && @pwd
        result << '@' if @user
        result << @host
        result << ':' << @port.to_s if @port
        result << '/' if @path || !@props.empty?
        result << @path if @path

        result << '?' << @props.keys.map { |k|
            v=@props[k]; v ? "#{k}=#{URI.encode_www_form_component(v)}" : "#{k}"
        }.join('&') unless @props.empty?

        result
    end
end