Class: UU::OS::UESURI

Inherits:
REST::DTO show all
Defined in:
uu_os_framework-0.29.16/lib/uu/os/uesuri.rb

Overview

UESURI - UES Uniform Resource Identifier (URI with ues: scheme).

Defined Under Namespace

Classes: Parameter

Constant Summary

NIL_URI =

Nil object for UESURI. Typical usage is in the uuAPI when it is needed to represent empty/nil value.

UESURI.new('ues:[-1]:[-1]')

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from REST::DTO

#to_json

Constructor Details

- (UESURI) initialize(data = nil)

Creates new instance of UESURI.

Examples:

Create UESURI from a String

uri = UESURI.new('ues:TER:ART')

Create UESURI from another UESURI (i.e. create a duplicate)

defensive_copy = UESURI.new(another_uri)

Parameters:

  • data (String, UESURI) (defaults to: nil)

    UESURI String or instance of UESURI



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
139
140
141
142
143
144
145
146
147
148
149
150
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 90

def initialize(data = nil)
  if data.nil?
    return
  elsif (!data.kind_of?String) && (!data.kind_of?UESURI)
    raise ArgumentError.new("Parameter must be a String or an instance of UESURI, but was #{data.class}.")
  end
  remain = data.to_s
  if remain[/^".*"$/] # It is URI in JSON
    remain = remain[1..-2]
  end
  if !(remain =~ URI_REGEX)
    raise ArgumentError.new("Parameter [#{remain}] is not valid UESURI")
  end
  # get schema
  parts = split_one(remain, ':')
  if parts.size != 2
    raise "UESURI (#{data}) does not define schema"
  end
  @schema = parts[0]
  remain = parts[1]
  # get role (authority)
  parts = split_one(remain, '@')
  if parts.size == 2
    @role = Part.new(parts[0])
    remain = parts[1]
  end
  # get fragment
  parts = split_one(remain, '#', true)
  if parts.size == 2
    @fragment = UESURI.send(:url_decode, parts[1])
    remain = parts[0]
  end
  # get usecase part including parameters
  parts = split_one(remain, '?', true)
  if parts.size == 2
    # separate parameters
    tmp = split_one(parts[1], '&')
    if !tmp[0].empty? # use case exists
      # parse optional action
      uca = split_one(tmp[0], ':', true)
      if (uca.size == 2)
        @action = uca[1] if !uca[1].empty?              
      end
      @use_case = Part.new(uca[0]) if !uca[0].empty?
    end
    if tmp[1] # parameters exists
      @parameters = []
      split_all(tmp[1], '&').each do |param|
        @parameters << Parameter.new(param)
      end
    end
    remain = parts[0]
  end
  # get uri parts
  @parts = []
  parts = split_one(remain, ':')
  while parts[0] do
    @parts << Part.new(parts[0])
    parts = split_one(parts[1]?parts[1]:'', ':')
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class UU::OS::REST::DTO

Class Method Details

+ (UESURI) create(data)

Creates a new UESURI instance with attributes from data.

Examples:

Create UESURI from a Hash

uri = UESURI.new(:schema => 'ues', :territory_code => 'TER', :artifact_code => 'ART')

Parameters:

  • data (Hash)

    UESURI attributes.

Options Hash (data):

  • :role_code (String)

    Role code.

  • :role_id (String)

    Role ID.

  • :territory_code (String)

    Territory code.

  • :territory_id (String)

    Territory ID.

  • :territory_expr (String)

    Territory expression. If an expression is set, code and ID are ignored.

  • :artifact_code (String)

    Artifact code.

  • :artifact_id (String)

    Artifact ID.

  • :artifact_expr (String)

    Artifact expression. If an expression is set, code and ID are ignored.

  • :object_code (String)

    Object code.

  • :object_id (String)

    Object ID.

  • :object_expr (String)

    Object expression. If an expression is set, code and ID are ignored.

  • :use_case_code (String)

    Use case code.

  • :use_case_id (String)

    Use case ID.

  • :action (String)

    Use case action.

  • :parameters (Hash)

    Use case parameters.

  • :fragment (String)

    Fragment.

Returns:

  • (UESURI)

    New instance of UESURI.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 175

def self.create(data)
  unless minimal_uesuri_parts?(data)
    raise ArgumentError.new("The hash does not contain enough data to represent a minimal UESURI (ues:TER:ART): #{data}")
  end

  builder = UESURIBuilder.new

  builder.set_role_code(data[:role_code])
  builder.set_role_id(data[:role_id])

  if data[:territory_expr]
    builder.set_territory_code(data[:territory_expr])
  else
    builder.set_territory_code(data[:territory_code])
    builder.set_territory_id(data[:territory_id])
  end

  if data[:artifact_expr]
    builder.set_artifact_code(data[:artifact_expr])
  else
    builder.set_artifact_code(data[:artifact_code])
    builder.set_artifact_id(data[:artifact_id])
  end

  if data[:object_expr]
    builder.set_object_code(data[:object_expr])
  else
    builder.set_object_code(data[:object_code])
    builder.set_object_id(data[:object_id])
  end

  builder.set_use_case_code(data[:use_case_code])
  builder.set_use_case_id(data[:use_case_id])

  builder.set_action(data[:action])

  if data[:parameters]
    unless data[:parameters].kind_of?(Hash)
      raise ArgumentError.new("Parameters must be represented as a Hash, not #{data[:parameters].class}")
    end
    data[:parameters].each do |k,v|
      builder.add_parameter(k, v)
    end
  end

  builder.set_fragment(data[:fragment])

  builder.to_uesuri
end

+ (Object) get_normalized_uri(uri_to_normalize)

Returns normalized UESURI.

Parameters:



383
384
385
386
387
388
389
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 383

def self.get_normalized_uri(uri_to_normalize)
  svc = UU::OS::REST::RemoteClient.new(UESURI)
  UU::OS::QoS::QoSHandler.auto_retry do
    res = svc.get('getNormalizedUri', uri_to_normalize)
    return UU::OS::UESURI.new(res)
  end
end

+ (TrueClass, FalseClass) same?(first, second)

Compares two UESURI in all their parts. Both UESURI are the same as the corresponding parts contains either the same ID or the same code. UESURI parts aren't comparable when one UESURI part contains only code and the other only ID (or vice versa). When incomparable UESURI parts are compared, exception is thrown.

Parameters:

  • first (String, UESURI)

    UESURI String or instance of first compared UESURI

  • second (String, UESURI)

    UESURI String or instance of second compared UESURI

Returns:

  • (TrueClass, FalseClass)

    True, when both UESURI contains the same UESURI parts at corresponding places, otherwise false.



421
422
423
424
425
426
427
428
429
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 421

def self.same?(first, second)
  if first.nil? || second.nil?
    return (first == second)
  elsif (first.kind_of?(String) && first.empty?) || (second.kind_of?(String) && second.empty?)
    return (first == second)
  end
  first = UESURI.new(first)
  first.same?second
end

+ (TrueClass, FalseClass) same_entity?(first, second)

Compares two UESURI according their territory, artifact and object part. Both UESURI are the same as the corresponding parts contains either the same ID or the same code. UESURI parts aren't comparable when one UESURIPart contain only code and the other only ID (or vice versa). When incomparable UESURI parts are compared, exception is thrown.

Parameters:

  • first (String, UESURI)

    UESURI String or instance of first compared UESURI

  • second (String, UESURI)

    UESURI String or instance of second compared UESURI

Returns:

  • (TrueClass, FalseClass)

    True, when both UESURI contains the same territory, artifact and object, otherwise false.



438
439
440
441
442
443
444
445
446
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 438

def self.same_entity?(first, second)
  if first.nil? || second.nil?
    return (first == second)
  elsif (first.kind_of?(String) && first.empty?) || (second.kind_of?(String) && second.empty?)
    return (first == second)
  end
  first = UESURI.new(first)
  first.same_entity?second
end

Instance Method Details

- (String) action

Use case action.

Returns:

  • (String)


326
327
328
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 326

def action
  @action
end

- (String) artifact_code

Artifact code.

Returns:

  • (String)


265
266
267
268
269
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 265

def artifact_code
  if @parts[1]
    @parts[1].code
  end
end

- (String) artifact_id

Artifact ID.

Returns:

  • (String)


273
274
275
276
277
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 273

def artifact_id
  if @parts[1]
    @parts[1].id
  end
end

- (String) fragment

Fragment.

Returns:

  • (String)


338
339
340
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 338

def fragment
  @fragment
end

- (String) object_code

Object code.

Returns:

  • (String)


281
282
283
284
285
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 281

def object_code
  if @parts[2]
    @parts[2].code
  end
end

- (String) object_id

Object ID.

Returns:

  • (String)


300
301
302
303
304
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 300

def object_id
  if @parts[2]
    @parts[2].id
  end
end

- (Array<UU::OS::UESURI::Parameter>) parameters

Use case parameters.

Returns:



332
333
334
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 332

def parameters
  @parameters
end

- (String) role_code

Role (authority) code.

Returns:

  • (String)


233
234
235
236
237
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 233

def role_code
  if @role
    @role.code
  end
end

- (String) role_id

Role (authority) ID.

Returns:

  • (String)


241
242
243
244
245
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 241

def role_id
  if @role
    @role.id
  end
end

- (TrueClass, FalseClass) same?(uesuri)

Compares instance of UESURI with another UESURI in all their parts. Both UESURI are the same as the corresponding parts contains either the same ID or the same code. UESURI parts aren't comparable when one UESURI part contains only code and the other only ID (or vice versa). When incomparable UESURI parts are compared, exception is thrown.

Parameters:

  • uesuri (String, UESURI)

    UESURI String or instance of UESURI

Returns:

  • (TrueClass, FalseClass)

    True, when both UESURI contains the same UESURI parts at corresponding places, otherwise false.



397
398
399
400
401
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 397

def same?(uesuri)
  return false if uesuri.nil? || (uesuri.kind_of?(String) && uesuri.empty?)
  uesuri = UESURI.new(uesuri) if !uesuri.kind_of?UESURI
  part_equal?(role, uesuri.role) && same_entity?(uesuri) && part_equal?(use_case, uesuri.use_case) && (action == uesuri.action) && parameters_equal?(parameters, uesuri.parameters) && (fragment == uesuri.fragment)
end

- (TrueClass, FalseClass) same_entity?(uesuri)

Compares instance of UESURI with another UESURI according their territory, artifact and object part. Both UESURI are the same as the corresponding parts contains either the same ID or the same code. UESURI parts aren't comparable when one UESURIPart contain only code and the other only ID (or vice versa). When incomparable UESURI parts are compared, exception is thrown.

Parameters:

  • uesuri (String, UESURI)

    UESURI String or instance of UESURI

Returns:

  • (TrueClass, FalseClass)

    True, when both UESURI contains the same territory, artifact and object, otherwise false.



409
410
411
412
413
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 409

def same_entity?(uesuri)
  return false if uesuri.nil? || (uesuri.kind_of?(String) && uesuri.empty?)
  uesuri = UESURI.new(uesuri) if !uesuri.kind_of?UESURI
  (schema == uesuri.schema) && part_equal?(parts[0], uesuri.parts[0]) && part_equal?(parts[1], uesuri.parts[1]) && part_equal?(parts[2], uesuri.parts[2])
end

- (String) schema

Schema.

Returns:

  • (String)


227
228
229
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 227

def schema
  @schema
end

- (String) territory_code

Territory code.

Returns:

  • (String)


249
250
251
252
253
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 249

def territory_code
  if @parts[0]
    @parts[0].code
  end
end

- (String) territory_id

Territory ID.

Returns:

  • (String)


257
258
259
260
261
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 257

def territory_id
  if @parts[0]
    @parts[0].id
  end
end

- (String) use_case_code

Use case code.

Returns:

  • (String)


310
311
312
313
314
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 310

def use_case_code
  if @use_case
    @use_case.code
  end
end

- (String) use_case_id

Use case ID.

Returns:

  • (String)


318
319
320
321
322
# File 'uu_os_framework-0.29.16/lib/uu/os/uesuri.rb', line 318

def use_case_id
  if @use_case
    @use_case.id
  end
end